I am creating Excel reports using data coming from mssql server in my ASP.NET application. Here's my method:
[WebMethod] Public static string ExportToExcel (string sourcetype) {Microsoft.Office.Interop.Excel.Application oXL; Microsoft.Office.Interop.Excel._Workbook oWB; Microsoft Office.Interop.Excel._ worksheet OSSet; Microsoft.Office.Interop.Excel.Range oRng; Try {oXL = New Microsoft.Office.Interop.Excel.Application (); OXL.Visible = false; OWB = (Microsoft.Office.Interop.Excel._Workbook) (oXL.Workbooks.Add (missing value)); OSheet = (Microsoft.Office.Interop.Excel._Worksheet) oWB.ActiveSheet; & Lt; ExcelReport & gt; DataToExport = APIClient.GetExcelReportData (utility .getCurrentFilterId (), sourcetype); Ohhett.kel [1, 1] = "source"; OSheet.Cells [1, 2] = "User Name"; OSheet.Cells [1, 3] = "name"; Oshet.kel [1, 4] = "message"; OSheet.Cells [1, 5] = "Title"; //oSheet.Cells[1, 6] = "date"; Int activeRow = 2; (Int i = 0; I & lt; Datatau Export.Count; I ++) {OSIt.Kel [Active Rau, 1] = Datatyxport [I] Source; Osat.kel [active rau, 2] = datatyxport [ii]. Username; Oset Seals [active rau, 3] = datatyxport [ii] .name; Oset Seals [active rau, 4] = datatouxport [ii]. message; Osat.kel [activate, 5] = datatyxport [ii]. Messaging; //oSheet.Cails[activeRow, 6] = Datacayportport [I]. Antitidate; ActiveRow ++; } OSheet.get_Range ("A1", "Z1") Font.bld = true; OSheet.get_Range ("A1", "Z1") VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter. ORng = oSheet.get_Range ("A1", "Z1"); ORng.EntireColumn.AutoFit (); OXL.Visible = false; OXL.UserControl = false; String Struff = "Report" + System.DateTime.Now.Ticks.ToString () + ".xls"; String strCurrentDir = HttpContext.Current.Server.MapPath (".") + "\\ ExcelReports \\"; OWB.SaveAs (strCurrentDir + strFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Null, Null, False, False, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, False, False, Null, Null, Faucet) ; //oWB.SaveCopyAs(strCurrentDir + strFile); OWB Closed (empty, null, void); OXL.Workbooks.Close (); OXL.Quit (); System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng); System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL); System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject (OWB); Oshit = null; OWB = null; OXL = null; GC.Collect (); // Force final cleaning! //errLabel.Text = "& lt; A href = http: //" + strMachineName + "/ ExcelGen /" + strFile + "& gt; Download report & lt; / a & gt;"; // string result = "& lt; a href = \" ~ / ExcelReports / "+ strFile +" & gt; Raporu ° NDIR & lt; / A & gt; "; String result =" ExcelReports / "+ strFile; return result;} hold (exception theException) {string errorMessage; errorMessage =" error: "; errorMessage = String.Concat (errorMessage, theException.Message); errorMessage = String.Concat ErrorMessage = String.Concat (errorMessage, theException .source); Return error message;}} This works fine on my machine and on that server (errorMessage, "Row:"); errorMessage = String.Concat Which was published on when I opened the source code in 2010 and pressed F5. But if I use the browser through IIS, then come Try to use Dan, I'm getting HRESULT: 0x800A03EC error
I tried the following command:
set appcmd Config -section: ASP I have my folder Tried to write permission for. I tried to change my MS Excel application settings from component service. P> But there is no way! I could not worn it. King, do you have any ideas? Am I making a mistake in the configuration?
Thanks in advance,
I try again your problem .. Key: oWB.SaveAs / oWB._SaveAs / (Microsoft.Office.Interop.Excel.Worksheet as oXL.ActiveSheet) .SaveAs / oWB.Application.ActiveWorkbook.SaveAs All throw exceptions: HRESULT: 0x800A03EC ... But I see you tried with: oWB.SaveCopyAs (strCurrentDir + strFile ); And if I set the next, then it works:
oWB.Saved = true; OWB.SaveCopyAs (strCurrentDir + strFile); Why do not you use SaveCopyAs
Comments
Post a Comment