2011-03-16 57 views
3

我正在使用来自我的asp.net应用程序中的mssql服务器的数据创建Excel报告。这里是我的方法:HRESULT:0x800A03EC - 创建Excel文件时,它适用于我的计算机问题

[WebMethod] 
public static string ExportToExcel(string sourcetype) 
{ 
    Microsoft.Office.Interop.Excel.Application oXL; 
    Microsoft.Office.Interop.Excel._Workbook oWB; 
    Microsoft.Office.Interop.Excel._Worksheet oSheet; 
    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; 

     List<ExcelReport> dataToExport = APIClient.GetExcelReportData(Utility.getCurrentFilterId(), sourcetype); 

     oSheet.Cells[1, 1] = "Source"; 
     oSheet.Cells[1, 2] = "UserName"; 
     oSheet.Cells[1, 3] = "Name"; 
     oSheet.Cells[1, 4] = "Message"; 
     oSheet.Cells[1, 5] = "Title"; 
     //oSheet.Cells[1, 6] = "Date"; 

     int activeRow = 2; 

     for (int i = 0; i < dataToExport.Count; i++) 
     { 
      oSheet.Cells[activeRow, 1] = dataToExport[i].Source; 
      oSheet.Cells[activeRow, 2] = dataToExport[i].UserName; 
      oSheet.Cells[activeRow, 3] = dataToExport[i].Name; 
      oSheet.Cells[activeRow, 4] = dataToExport[i].Message; 
      oSheet.Cells[activeRow, 5] = dataToExport[i].MessageTitle; 
      //oSheet.Cells[activeRow, 6] = dataToExport[i].EntityDate; 

      activeRow++; 
     } 

     oSheet.get_Range("A1", "Z1").Font.Bold = 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 strFile = "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, null); 
     //oWB.SaveCopyAs(strCurrentDir + strFile); 
     oWB.Close(null, null, null); 
     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); 
     oSheet = null; 
     oWB = null; 
     oXL = null; 
     GC.Collect(); // force final cleanup! 

     //errLabel.Text = "<A href=http://" + strMachineName + "/ExcelGen/" + strFile + ">Download Report</a>"; 
     //string result = "<a href=\"~/ExcelReports/" + strFile + ">Raporu İndir</a>"; 
     string result = "ExcelReports/" + strFile; 
     return result; 
    } 
    catch (Exception theException) 
    { 
     String errorMessage; 
     errorMessage = "Error: "; 
     errorMessage = String.Concat(errorMessage, theException.Message); 
     errorMessage = String.Concat(errorMessage, " Line: "); 
     errorMessage = String.Concat(errorMessage, theException.Source); 

     return errorMessage; 
    } 

} 

它工作正常,在我的机器,并在该应用程序发布到,当我在VS 2010,然后按F5打开源代码服务器。但是,如果我尝试通过IIS访问我的应用程序使用浏览器,我得到HRESULT:0x800A03EC错误。

我尝试下面的命令:

appcmd set config -section:asp -enableParentPaths:true 

我试着写权限授予我的文件夹中。

我试图从组件服务更改我的MS Excel应用程序设置。

但没办法!我无法得到它的工作。你有什么主意吗?我在配置中犯了错误吗?

由于提前,

+3

我想打电话互操作 – Robert 2011-03-16 15:25:07

+0

既然你没有提到它0x800A03EC == ERROR_INVALID_FLAGS时,你应该通过System.Missing.Value而不是null值的“桌面”文件夹中。但你不应该从IIS自动化Excel - 它[不正式支持](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c/151014#151014)因为Excel假定它有一个交互式桌面。有[大量的XLS/XLSX生成库](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c),你可以使用。 – Rup 2011-03-16 18:39:17

回答

12

我复制你的问题..尝试了所有:oWB.SaveAs/oWB._SaveAs/(oXL.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet).SaveAs/oWB.Application.ActiveWorkbook.SaveAs所有抛出异常:HRESULT:0x800A03EC ...

但我看到你试着用:OWB .SaveCopyAs(strCurrentDir + strFile); 和它的作品,如果我下一个设置:

oWB.Saved = true; 
oWB.SaveCopyAs(strCurrentDir + strFile); 

为什么你不使用SaveCopyAs

+1

感谢您的回答。它适用于这个详细的配置。 http://stackoverflow.com/questions/1981395/windows-7-net-excel-saveas-error-exception-from-hresult-0x800a03ec/1986575#1986575 – anilca 2011-03-16 18:42:59

+0

非常好。优秀。解决方法总是美味;)+1。 – 2012-05-13 06:44:54

+0

我有Windows 2008,Office 2003,一个在域用户下运行的Windows服务,在SaveAs行失败。试过SaveCopyAs(没有.Saved = true虽然),并有类似的错误。它在C:\ Users \ [User]文件夹(我知道这并不好,因此我认为我应该只允许该用户访问本地和漫游文件夹)后,它突然开始工作。 – Alex 2015-05-08 12:12:24

-2

让像下面

C:\Windows\SysWOW64\config\systemprofile\Desktop 
C:\Windows\System32\config\systemprofile\Desktop 
+0

这不提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 - [来自评论](/ review/low-quality-posts/10293956) – 2015-11-22 12:59:09

+0

@MuhammedRefaat这可能是一个有效的答案。如果你认为这是错误的或者没有用,那么你可以减少它。 – 2015-11-22 14:52:11

+0

@ ArtjomB。它通过审查低质量的帖子来找到我,我估计它是一个评论,因为有两点,首先,我认为这是一个快速修复问题的尝试或镜头,而不是一个满足的答案,通常在SO中进行修复作为评论,第二我相信他必须澄清一下他的答案如何解决OP的问题。谢谢你的评论。 – 2015-11-22 17:34:09

相关问题