在我的MVC應用程序中,我使用Microsoft.Office.Interop.Excel
將數據導出到excel。在部署.net C#MVC應用程序後保存/打開文件錯誤
我也啓用了ASP.NET Impersonation
並給出了驗證細節。它將數據寫入excel,但不保存或打開excel文件。它引發了一個例外Exception from HRESULT: 0x800A03EC
。如果有人能幫忙,我會很高興。提前致謝。
我出口的代碼是:
public static void ExportExcel(this DataTable Tbl, string ExcelFilePath = null)
{
ExcelFilePath = "ExportTable.xls";
try
{
if (Tbl == null || Tbl.Columns.Count == 0)
//throw new Exception("ExportToExcel: Null or empty input table!\n");
Console.WriteLine("ExportToExcel: Null or empty input table!\n");
// load excel, and create a new workbook
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Add();
// single worksheet
Excel._Worksheet workSheet = excelApp.ActiveSheet;
// column headings
for (int i = 0; i < Tbl.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
workSheet.Cells[1, (i + 1)].Font.Bold = true;
workSheet.Cells[1, (i + 1)].Font.Size = 12;
}
// rows
for (int i = 0; i < Tbl.Rows.Count; i++)
{
// to do: format datetime values before printing
for (int j = 0; j < Tbl.Columns.Count; j++)
{
workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
}
}
// works fine till here
// check fielpath
if (ExcelFilePath != null && ExcelFilePath != "")
{
try
{
workSheet.SaveAs(ExcelFilePath);
excelApp.Quit();
}
catch (Exception ex)
{
Console.WriteLine("ExportToExcel: Excel file could not be saved! Check filepath.\n"+ ex.Message);
}
}
else // no filepath is given
{
excelApp.Visible = true;
}
}
catch (Exception ex)
{
Console.WriteLine("ExportToExcel: \n" + ex.Message);
}
}