我在下面写了这段代码
string filePath = @"C:\report_data.xlsx";
// Saves the file via a FileInfo
var file = new FileInfo(filePath);
// Creates the package and make sure you wrap it in a using statement
using (var package = new ExcelPackage(file))
{
// Adds a new worksheet to the empty workbook
OfficeOpenXml.ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Report System - " + DateTime.Now.ToShortDateString());
// Starts to get data from database
for (int row = 1; row < 10; row++)
{
// Writes data from sql database to excel's columns
for (int col = 1; col < 10; col++)
{
worksheet.Cells[row, col].Value = row * col;
}// Ends writing data from sql database to excel's columns
}// Ends getting data from database
// Saves new workbook and we are done!
package.Save();
}
当我打开文件时,它什么都没有,页面是空白的。但是当我看到文件大小时,它增加了。为什么文件大小增加时页面为空白,如何向其中写入数据?
备注:当我尝试删除现有文件并运行程序时,程序生成了文件并且文件的值显示在列中。另一方面,我尝试先创建文件,然后运行程序,结果就像我上面所说的那样是空白的。