导出Excel异常

mikelic 2014-09-09 03:14:57
大家好,我通过一下代码导出Excel文件:如果outputPath : @"F:\outputFormDataBase.xls");输出文件:F-outputFormDataBase.xls;如果outputPath: @"导出1.xls");直接提示 response.WriteFile(fullFileName);未能找到文件:导出1.xls文件;请大家帮忙,谢谢!

outputPath:保存路径

dataSet:数据集

deleteOldFile:是否删除已有的老文件
//保存exce

public void DataSetToLocalExcel(DataSet dataSet, string outputPath, bool deleteOldFile)
{
if (deleteOldFile)
{
if (System.IO.File.Exists(outputPath)) { System.IO.File.Delete(outputPath); }
}
// Create the Excel Application object
Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

// Create a new Excel Workbook
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);

int sheetIndex = 0;

// Copy each DataTable
foreach (System.Data.DataTable dt in dataSet.Tables)
{

// Copy the DataTable to an object array
object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];

// Copy the column names to the first row of the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
rawData[0, col] = dt.Columns[col].ColumnName;
}

// Copy the values to the object array
for (int col = 0; col < dt.Columns.Count; col++)
{
for (int row = 0; row < dt.Rows.Count; row++)
{
rawData[row + 1, col] = dt.Rows[row].ItemArray[col];
}
}

// Calculate the final column letter
string finalColLetter = string.Empty;
string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int colCharsetLen = colCharset.Length;

if (dt.Columns.Count > colCharsetLen)
{
finalColLetter = colCharset.Substring(
(dt.Columns.Count - 1) / colCharsetLen - 1, 1);
}

finalColLetter += colCharset.Substring(
(dt.Columns.Count - 1) % colCharsetLen, 1);

// Create a new Sheet
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.Sheets.Add(
excelWorkbook.Sheets.get_Item(++sheetIndex),
Type.Missing, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);

excelSheet.Name = dt.TableName;
Microsoft.Office.Interop.Excel.Range ra = excelSheet.get_Range(excelApp.Cells[3,1],excelApp.Cells[3,1]);
ra.ColumnWidth = 20;
// Fast data export to Excel
string excelRange = string.Format("A1:{0}{1}",
finalColLetter, dt.Rows.Count + 1);

excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;

// Mark the first row as BOLD
((Microsoft.Office.Interop.Excel.Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = true;
}
//excelApp.Application.AlertBeforeOverwriting = false;
excelApp.Application.DisplayAlerts = false;
// Save and Close the Workbook
excelWorkbook.SaveAs(outputPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

excelWorkbook.Close(true, Type.Missing, Type.Missing);


excelWorkbook = null;

// Release the Application object
excelApp.Quit();
excelApp = null;

// Collect the unreferenced objects
GC.Collect();
GC.WaitForPendingFinalizers();
this.DownLoadExcel1(outputPath);
}

//下载

private void DownLoadExcel1(string fullFileName)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.WriteFile(fullFileName);
string httpHeader = "attachment;filename=" + fullFileName;
response.AppendHeader("Content-Disposition", httpHeader);
response.Charset = "utf-8";
response.ContentEncoding = System.Text.Encoding.Default;
response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

response.BufferOutput = true;
response.AppendToLog(DateTime.Now.ToString() + "导出Excel文件");
response.Flush();

System.IO.File.Delete(fullFileName);//删除临时文件
//response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
...全文
495 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2014-09-09
  • 打赏
  • 举报
回复
把导出和下载分开调试 一步步来 别老想一步到位啊 很明显你导出的路径错误,导致下载的时候找不到文件 好好找找你到底导出到哪里去了 然后对应的修改路径
mikelic 2014-09-09
  • 打赏
  • 举报
回复
完整的路径还是找不到
mikelic 2014-09-09
  • 打赏
  • 举报
回复
异常详细信息: System.IO.FileNotFoundException: 未能找到文件“K:\学习\919\StorageSystem\Web\Module\BaseInfo\outputFormDataBase.xls”。
-------_------- 2014-09-09
  • 打赏
  • 举报
回复
你的路径给错了,或者是更加详细,比如 @".\导出1.xls" 要不你在 this.DownLoadExcel1(outputPath);的时候这样写 if(Path.IsPathRootedoutputPath()) this.DownLoadExcel1(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,outputPath));
於黾 2014-09-09
  • 打赏
  • 举报
回复
还有,既然是导出供人下载的,你就直接用当前时间或GUID拼个字符串当临时文件名就好了,不要胡乱起个中文名. 万一有多个用户同时在导出,不是都互相覆盖了么
於黾 2014-09-09
  • 打赏
  • 举报
回复
遇到问题先分解,一步一步测试 你后面的路径跟前面的差的太多 既有中文,又用的相对路径 再做2组测试: 1.绝对路径带中文 2.相对路径不带中文 看到底是中文没认出来,还是相对路径没写对.

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧