DotNetCore NPOI AutoSizeColumn Too Narrow
DotNetCore NPOI AutoSizeColumn Too Narrow
I'm using NPOI and C# and cannot seem to get AutoSizeColumn to size the columns appropriately. I've followed the suggestions I can find, set the font up front in the styles, added content after the styles are applied, auto-sized the columns before write (after all data is added) and still the columns are too narrow. Below is a demonstration of the problem:
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);
This code shows up in Excel like this:
Result of Code
So, what am I missing? What am I doing wrong?
1 Answer
1
I am using this approach to auto size the column header
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("Demo");
sheet.AutoSizeColumn(“header1”); // Remove Boolean value which you are passing
“Header1” is header name. Either you can pass header name as string or index. Please see the code in question and my code reference is for that.
– kumar chandraketu
Jan 10 at 17:17
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is header1?
– JeppePepp
Jan 10 at 16:15