如何设置“自动调整大小”到 Excel 工作表列? (NPOI)
根据列如何在使用 NPOI 创建的 Excel 文档中设置为“自动调整大小”? 我这样做了:
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);
}
但它不起作用。怎样做才对呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一些使用您的循环对我有用的代码:
如果它对您不起作用,那么我们需要查看您正在推出的数据类型,看看是否存在会产生影响的差异。 (我假设我们没有版本差异或类似的情况)。
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).
只是为了在 YellowFog 的答案中添加一点额外的内容。我发现我必须将所有数据添加到工作表中,然后遍历列,设置 AutoSizeColumn(idx) 才能正常工作。
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.