导出到Excel电子表格(XLSX)失败

时间:2014-05-21 16:29:15

标签: c# asp.net oledb

使用ASP.NET网页,我将一些数据导出到Excel电子表格(XLSX)。当我使用Visual Studio运行它时,代码运行正常(它正在导出具有正确数据的XLSX文件),但是当部署到Testing Server时,相同的代码会失败。

它不会抛出任何错误,它只是导出一个空白的XLSX文件。

注意:在测试服务器中进行调试时,我发现正在获取数据并且临时文件也正在被正确创建,但是数据没有被写入临时文件(这很奇怪)事情是它没有通过任何错误。)

稍后添加

在做了一些研究之后,我发现小记录集没有问题(比如1000,2000)。但是当试用~20K记录时,我得到一个空白文件。

过去2天我一直在燃烧自己,有人救了我:) ...

代码

string templateFile = @"C:\Templates\ExportFile.xlsx";
string tempFileName = Path.Combine(@"C:\Temp\", Path.GetRandomFileName());
tempFileName = Path.ChangeExtension(tempFileName, ".xlsx");
File.Copy(templateFile, tempFileName);

List<Customer> customerList = FetchCustomers();

DataTable dataTableObj = new DataTable("Customers$");
dataTableObj.Columns.Add(new DataColumn("CustomerID"));
dataTableObj.Columns.Add(new DataColumn("FirstName"));
dataTableObj.Columns.Add(new DataColumn("LastName"));
dataTableObj.Columns.Add(new DataColumn("CreatedDate"));

foreach (Customer customerObj in customerList)
{
    DataRow dataRowObj = dataTableObj.NewRow();
    dataRowObj["CustomerID"] = customerObj.CustomerID;
    dataRowObj["FirstName"] = customerObj.FirstName;
    dataRowObj["LastName"] = customerObj.LastName;
    dataRowObj["CreatedDate"] = customerObj.CreatedDate;
    dataTableObj.Rows.Add(dataRowObj);
}

using (OleDbConnection oleDbConnectionObj = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tempFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;\""))
{
    OleDbCommand insertCommand = new OleDbCommand();
    insertCommand.Connection = oleDbConnectionObj;
    insertCommand.CommandText = @"INSERT INTO [Customers$] ([CustomerID], [FirstName], [LastName], [CreatedDate]) VALUES (?, ?, ?, ?)";
    insertCommand.Parameters.Add("CustomerID", OleDbType.Numeric, 0, "CustomerID");
    insertCommand.Parameters.Add("FirstName", OleDbType.VarChar, 0, "FirstName");
    insertCommand.Parameters.Add("LastName", OleDbType.VarChar, 0, "LastName");
    insertCommand.Parameters.Add("CreatedDate", OleDbType.Date, 0, "CreatedDate");

    DataSet dataSetObj = new DataSet();
    dataSetObj.Tables.Add(dataTableObj);

    OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
    dataAdapter.InsertCommand = insertCommand;
    dataAdapter.Update(dataSetObj, "Customers$");
}

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.AddHeader("Content-Disposition", "attachment; filename=Customers.xlsx");
Response.WriteFile(tempFileName);
Response.Flush();
Response.End();

1 个答案:

答案 0 :(得分:4)

最后它得到了解决!

在创建大型XLSX文件(大于1MB)时,OLEDB Provider会尝试在 Local Settings\Temporary Internet Files\Content.MSO\ 为APP POOL帐户创建临时文件。如果文件夹不存在或者APP POOL帐户没有适当的权限,那么它会失败而不会抛出错误(不知道它为什么不抛出错误)。


在我的情况下, C:\ Documents and Settings \ Default User \ Local Settings \ Temporary Internet Files 中缺少Content.MSO文件夹。

我创建了文件夹并将修改权限授予了 NETWORK SERVICES .....和“Voila!” - 一切都开始有效了。



感谢以下2个链接,他们节省了我几天...... :)

Microsoft ACE OLEDB connection creating empty Excel when there are 166,110 rows

http://www.rapidsnail.com/developer/topic/2011/109/2/65194/excel-writes-secret-use-oledb-write-data-to-excel-more-than-13571-line-file-for-blank.aspx