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






What is header1?

– JeppePepp
Jan 10 at 16:15






“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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)