DotNetCore NPOI AutoSizeColumn太窄

时间:2018-09-07 00:36:11

标签: c# npoi

我正在使用NPOI和C#,似乎无法获取AutoSizeColumn来适当调整列的大小。我遵循了可以找到的建议,在样式中预先设置了字体,在应用样式后添加了内容,在写入之前自动调整了列的大小(添加了所有数据之后),但列仍然太窄。下面是该问题的演示:

public void ShowBug()
{
    IWorkbook workbook;
    workbook = new XSSFWorkbook();

    // Create a base font
    IFont boldFont = workbook.CreateFont();
    boldFont.FontHeightInPoints = 11;
    boldFont.FontName = "Calibri";
    boldFont.Boldweight = (short)FontBoldWeight.Bold;

    // Create a base style
    ICellStyle boldStyle = workbook.CreateCellStyle();
    boldStyle.SetFont(boldFont);

    // Create a simple cell style using the base style
    ICellStyle simpleStyle = workbook.CreateCellStyle();
    simpleStyle.CloneStyleFrom(boldStyle);

    // Create a sheet in the workbook
    ISheet excelSheet = workbook.CreateSheet("Demo");

    // Create a single row
    IRow row = excelSheet.CreateRow(0);

    // Create a single cell inside the row
    ICell cell = row.CreateCell(0);
    cell.SetCellType(CellType.String);
    cell.CellStyle = simpleStyle;
    cell.SetCellValue("This is an icredibly long text value for this column - and apparently too long");

    // Autosize the column and create the file, after the output is produced
    using (var fs = new FileStream("BugReport.xlsx", FileMode.Create, FileAccess.Write))
    {
        excelSheet.AutoSizeColumn(0, true);
        workbook.Write(fs);
    }
}

此代码在Excel中显示如下:

Result of Code

那么,我想念什么?我在做什么错了?

1 个答案:

答案 0 :(得分:-1)

我正在使用这种方法来自动调整列标题的大小

 XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("Demo");
 sheet.AutoSizeColumn(“header1”); // Remove Boolean value which you are passing