當我在本地機器上運行調試模式時,沒有任何問題。我可以導出到Excel ...運行時錯誤 - 使用C#從IIS導出Excel
private void ExportToExcel(string str)
{
_Excel.Application oXL;
_Excel.Workbook oWB;
_Excel.Worksheet oSheet;
_Excel.Range oRange;
// Start Excel and get Application object.
oXL = new _Excel.Application();
// Set some properties
oXL.Visible = true;
oXL.DisplayAlerts = false;
// Get a new workbook.
oWB = oXL.Workbooks.Add(Missing.Value);
// Get the active sheet
oSheet = (_Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "PO_Status";
// Process the DataTable
int rowCount = 1;
foreach (DataRow dr in dtStatus(str).Rows)
{
rowCount += 1;
for (int i = 1; i < dtStatus(str).Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oSheet.Cells[1, i] = dtStatus(str).Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
// Resize the columns
oRange = oSheet.get_Range(oSheet.Cells[1, 1],
oSheet.Cells[rowCount, dtStatus(str).Columns.Count]);
oRange.EntireColumn.AutoFit();
try
{
// Save the sheet and close
oSheet = null;
oRange = null;
oWB.SaveAs("POStatus.xls", _Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
_Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//oWB.Close(Missing.Value, Missing.Value, Missing.Value);
//oWB = null;
//oXL.Quit();
// Clean up
// NOTE: When in release mode, this does the trick
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
catch (Exception ex)
{
Session["error"] = ex.Message;
Response.Redirect("MessageBoard.aspx");
}
}
private DataTable dtStatus(string str)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(@"Select " + str + ""
+ " From tbl_RFI Full Join"
+ " tbl_RFQ On tbl_RFI.RFINo = tbl_RFQ.RFINo Full Join"
+ " tbl_NNB On tbl_RFQ.RFQNo = tbl_NNB.RFQNo Full Join"
+ " tbl_PO On tbl_PO.NNBNo = tbl_NNB.NNBNo"
+ " Where tbl_RFI.JobNo = '" + ddlJobNo.SelectedValue.ToString().Trim() + "'", connPMis);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
return dt;
}
}
但是當我在IIS上運行,它不工作...我得到這樣的
運行時錯誤 描述錯誤消息:在服務器上出現應用程序錯誤。此應用程序的當前自定義錯誤設置可防止遠程查看應用程序錯誤的細節(出於安全原因)。但是,它可以通過本地服務器上運行的瀏覽器來查看。
詳細信息:若要使此特定錯誤信息的詳細信息,以能夠在遠程計算機上查看,請在位於當前Web應用程序根目錄下的「web.config」配置文件中創建一個標籤。這個標籤應該將其「模式」屬性設置爲「關」。
注意:你所看到的當前錯誤頁可以用自定義錯誤頁替換通過修改應用程序的配置標記的「defaultRedirect」屬性,使之指向自定義錯誤頁URL。
我的服務器爲Windows Server 2008/IIS7 THX對您有所幫助
您的Windows Server 2008/IIS7上是否安裝了Excel? – Jay 2011-06-08 02:36:14
yes alrdy installed office 2007 – CMMaung 2011-06-08 03:32:33
我應該使用HTMLWriter和Response.Write嗎? – CMMaung 2011-06-08 03:37:09