【问题标题】:How to format cells in Aspose如何在 Aspose 中格式化单元格
【发布时间】:2015-05-19 23:00:58
【问题描述】:

我正在尝试在特定单元格中将一段文本加粗,但无法做到。 这是我正在使用的代码:

  Style boldStyle = workBook.CreateStyle();
           boldStyle.BackgroundColor = Color.Red;
            StyleFlag boldStyleFlag = new StyleFlag();
            boldStyleFlag.HorizontalAlignment =true ;
            boldStyleFlag.FontBold = true;
            Cell c = workSheetIntroduction.Cells["B1"];
            c.SetStyle(boldStyle, boldStyleFlag);

【问题讨论】:

    标签: c# aspose aspose-cells


    【解决方案1】:
    Workbook workBook = new Workbook();
    
    Worksheet workSheetIntroduction = workBook.Worksheets[0];
    
    
    //This Style object will make the fill color Red and font Bold
    
    Style boldStyle = workBook.CreateStyle();
    
    boldStyle.ForegroundColor = Color.Red;
    
    boldStyle.Pattern = BackgroundType.Solid;
    
    boldStyle.Font.IsBold = true;
    
    
    //Bold style flag options
    
    StyleFlag boldStyleFlag = new StyleFlag();
    
    boldStyleFlag.HorizontalAlignment = true;
    
    boldStyleFlag.FontBold = true;
    
    
    //Apply this style to row 1
    
    Row row1 = workBook.Worksheets[0].Cells.Rows[0];
    
    row1.ApplyStyle(boldStyle, boldStyleFlag);
    
    
    
    //Now set the style of indvidual cell
    
    Cell c = workSheetIntroduction.Cells["B1"];
    
    
    Style s = c.GetStyle();
    
    s.ForegroundColor = Color.Red;
    
    s.Pattern = BackgroundType.Solid;
    
    s.Font.IsBold = true;
    
    c.SetStyle(s);
    
    
    //Save the workbook
    
    workBook.Save("output.xlsx");
    

    【讨论】:

      【解决方案2】:

      试试下面的代码,

        Workbook workbook = new Workbook("F:\\test.xlsx");
              Worksheet s = workbook.Worksheets.First();
              Cell c = s.Cells.FirstCell;
              if (c != null)
              {
                  Style st = c.GetStyle();
                  st.Font.IsBold = true;
                  c.SetStyle(st);
              }
      
              workbook.Save("F:\\test1.xlsx");
      

      【讨论】:

      • 之前的代码可以运行,我也添加了测试代码,创建一个 test.xlsx,并在第一个单元格中添加一些文本。测试代码并告诉我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多