According to How can columns be set to 'autosize' in Excel documents created with NPOI? I did so:
foreach (DataColumn column in dataTable.Columns)
{
int rowIndex = 0;
foreach (DataRow row in dataTable.Rows)
{
HSSFRow dataRow = sheet.CreateRow(rowIndex);
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
rowIndex++;
}
sheet.AutoSizeColumn(column.Ordinal);
}
But it doesn't work. How to do right?
Just to add an extra bit to the answer by YellowFog. I found that I had to add all data to the sheet, then iterate through the columns, setting AutoSizeColumn(idx) for this to work correctly.
Here is some code that is working for me, using your loops:
If it doesn't work for you then we need to look at the kind of data you're pushing out, see if there's a difference that makes a difference there. (I'm assuming that we don't have a version discrepancy or anything like that).