Dmi*_*ryB 10 .net c# 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);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.怎么做对吗?
Yel*_*fog 17
以下是一些使用循环的代码:
HSSFWorkbook spreadsheet = new HSSFWorkbook();
DataSet results = GetSalesDataFromDatabase();
//here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'
HSSFSheet sheet1 = spreadsheet.CreateSheet("Sheet1");
foreach (DataColumn column in results.Tables[0].Columns)
{
int rowIndex = 0;
foreach (DataRow row in results.Tables[0].Rows)
{
HSSFRow dataRow = sheet1.CreateRow(rowIndex);
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
rowIndex++;
}
sheet1.AutoSizeColumn(column.Ordinal);
}
//Write the stream data of workbook to the file 'test.xls' in the temporary directory
FileStream file = new FileStream(Path.Combine(Path.GetTempPath(), "test.xls") , FileMode.Create);
spreadsheet.Write(file);
file.Close();
Run Code Online (Sandbox Code Playgroud)
如果它对您不起作用,那么我们需要查看您要推出的数据类型,看看是否存在差异,从而产生差异.(我假设我们没有版本差异或类似的东西).
| 归档时间: |
|
| 查看次数: |
20618 次 |
| 最近记录: |