问题
Unable to create the excel file at particular path when the application is run from IIS. But its created when the code is debuged using visual studio 2008.Even i have tried out using absolute path and using server.mappath() function but in vain.Server throws the error : HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Error.aspx
The code is given below:
private void CreateExcel(DataTable dt)
{
try
{
FilePath = "\\\\192.168.1.252\\GNC Reports\\TallyExport.xls";
Excel.Application oXL = new Excel.Application();
//Get a new workbook.
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(Type.Missing));
// *************** Sheet 1
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.Sheets["Sheet1"];
oSheet.Name = "Journal";
WriteWxcel(oSheet, dt, 3);
//***************** Sheet 2
oSheet = (Excel._Worksheet)oWB.Sheets["Sheet2"];
oSheet.Name = "Payroll";
WriteWxcel(oSheet, dt, 4);
// ************* Sheet 3
oSheet = (Excel._Worksheet)oWB.Sheets["Sheet3"];
oSheet.Name = "Receipt";
WriteWxcel(oSheet, dt, 2);
//Save Excel File
oWB.SaveAs(FilePath, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
oXL.Quit();
}
catch (Exception ex)
{
BussinessLayer.CMSException.Instance.HandleMe(this, ex);
}
}
回答1:
Using Office Interop from server-like scenarios (IIS/ASP.NET/Windows Service...) is NOT supported by MS - see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
Another point is that since windows vista there have been several security-related changed which prevent doing any "desktop-like" things (for example printing, writing to a network share...) from a Windows Service...
Alternative to Interop:
There are many options to read/edit/create Excel files without Interop:
MS provides the free OpenXML SDK V 2.0 - see http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx (XLSX only)
This can read+write MS Office files (including Excel).
Another free option see http://www.codeproject.com/KB/office/OpenXML.aspx (XLSX only)
IF you need more like handling older Excel versions (like XLS, not only XLSX), rendering, creating PDFs, formulas etc. then there are different free and commercial libraries like ClosedXML (free, XLSX only), EPPlus (free, XLSX only), Aspose.Cells, SpreadsheetGear, LibXL and Flexcel etc.
来源:https://stackoverflow.com/questions/8728403/unable-to-create-the-excel-file-when-the-application-asp-net-is-run-from-iis