5

我不明白为什么会发生这种情况,首先我尝试在第一行的列标题中应用粗体文本,然后我想将标题单元格的边框设置为 MEDIUM,但是这种 MEDIUM 边框样式适用于中的所有单元格工作表。下面的相同代码中还有更多问题:

  1. 我的列标题(第一行)中的文本不是我想要的粗体。
  2. 我的列标题中的文本颜色不是我想要的红色。

这是我的代码(使用 NPOI 库处理):

private void CreateATest(string filename)
    {
        FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
        HSSFWorkbook wb = new HSSFWorkbook();
        ISheet sheet = wb.CreateSheet("NPOI");
        IRow row = sheet.CreateRow(0);
        row.RowStyle = wb.CreateCellStyle();
        row.RowStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

        row.RowStyle.VerticalAlignment = VerticalAlignment.CENTER;            
        row.RowStyle.WrapText = true;
        IFont font = wb.CreateFont();
        font.Boldweight = 3;
        font.Color = (short) ColorTranslator.ToWin32(Color.Red);
        font.FontHeight = 30;
        row.RowStyle.SetFont(font);
        int i = 0;
        foreach (string header in new string[] { "ID", "Name", "Age" })
        {
            row.CreateCell(i++).SetCellValue(header);
            row.Cells[i - 1].CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.MEDIUM;
            row.Cells[i - 1].CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.MEDIUM;
            row.Cells[i - 1].CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.MEDIUM;
        }
        row.Cells[i - 1].CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.MEDIUM;
        Random rand = new Random();
        for (i = 1; i < 1000; i++)
        {
            IRow row1 = sheet.CreateRow(i);
            for (int j = 0; j < 3; j++)
            {
                row1.CreateCell(j).SetCellValue(rand.Next(100));
            }
        }
        wb.Write(fs);
        fs.Close();
    }

请为我修复它,我对 NPOI 很陌生,刚刚尝试使用它。您的帮助将不胜感激。谢谢。(<---我不知道为什么这个“谢谢”不能跳到下一行,即使我在输入之前输入了 Enter)

4

3 回答 3

6

格式问题是由于 Excel 格式化插入行的方式造成的。他们从上面的行中获取他们的样式信息。您可以通过将行格式化为粗体,然后在下方插入一行来测试这一点 - 新行也将加粗。您可以尝试先插入其余行,然后再对标题行进行格式化。不幸的是,我没有足够的声誉来发表评论而不是回答,因为我无法帮助您解决其他两个问题。

于 2013-04-10T21:59:42.137 回答
2

这是解决方案 - 我遇到了同样的问题。您需要创建一个离散的 ICellStyle 并将其分配给单元格的 CellStyle,而不是仅在单元格的当前 CellStyle 上调用“SetFont()”。

在将样式应用于所有单元格时,我遇到了同样的问题。我评论了导致它的行,下一行是有效的(和 ICellStyle 部分):

        let workbook:HSSFWorkbook = new HSSFWorkbook()
        let worksheet:ISheet =  workbook.CreateSheet(sheetName)

        let fontbold = workbook.CreateFont()
        fontbold.Boldweight <- (int16 FontBoldWeight.Bold)
        fontbold.FontHeightInPoints <- 10s
        fontbold.FontName <- "Arial"

        let cellstylebold:ICellStyle = workbook.CreateCellStyle()
        cellstylebold.SetFont(fontbold)

        let row:IRow = worksheet.CreateRow(0)

        for h in 0..headers.Length-1 do
            let cell = row.CreateCell(h)
            cell.SetCellValue(headers.[h])
            //cell.CellStyle.SetFont(fontbold)
            cell.CellStyle <- cellstylebold
于 2017-08-02T19:36:42.397 回答
0

Rory 提出的一种可能的解决方案是在创建单元格时设置 CellStyle,然后才使用 GetCell(x) 设置值。

我遇到了同样的错误,因为我在创建 CellStyle 后尝试更改它,并以这种方式解决了更改顺序:

//CREATE STYLE
ICellStyle styleCenter1 = hssfworkbook.CreateCellStyle();
styleCenter1.Alignment = HorizontalAlignment.Center;
styleCenter1.VerticalAlignment = VerticalAlignment.Center;
styleCenter1.WrapText = true;
styleCenter1.SetFont(font1);

//CREATE ROW
IRow row = sheet1.CreateRow(z);

//SETTING CELLSTYLE WHILE CREATING CELL
row.CreateCell(0).CellStyle = styleCenter1;

//SETTING CELLVALUE AFTER
row.GetCell(0).SetCellValue(listaSocieta[x]);

再见,

雷蒙多

于 2018-07-16T14:40:46.277 回答