2013-08-29 26 views
0

當我在我的個人電腦上工作時,我的下面的代碼工作正常,xsl將保存在指定的路徑中。 但是當應用程序託管和XSL文件保存的過程中,我從HRESULT收到錯誤消息像 例外:0x800A03EC如何在客戶端電腦中保存xsl文件如果我使用Microsoft.Office.Interop.Excel

請讓我知道如何保存在客戶端PC的XSL文件。我需要修改我的代碼???

public void Convertoxsl(DataSet ds) 
     { 
      Application ExcelApp = new Application(); 
      Workbook ExcelWorkBook = null; 
      Worksheet ExcelWorkSheet = null; 
      string FileName = string.Empty; 
      ExcelApp.Visible = true; 
      ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); 

      List<string> SheetNames = new List<string>(); 
      string sn = ddlPageName.SelectedItem.Text; 
      SheetNames.Add(sn); 


      try 
      { 
       int getcol = 0; 
       for (int i = 1; i < ds.Tables.Count; i++) 
        ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook 

       for (int i = 0; i < ds.Tables.Count; i++) 
       { 
        int r = 1; // Initialize Excel Row Start Position = 1 

        ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets[i + 1]; 

        //Writing Columns Name in Excel Sheet 

        for (int col = 1; col <= ds.Tables[i].Columns.Count; col++) 
        { 
         if (ds.Tables[i].Columns.Count != 3) 
         { 
          return; 
         } 
         ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - 1].ColumnName; 
         getcol = col; 
        } 



         r++; 



        //Writing Rows into Excel Sheet 
        for (int row = 0; row < ds.Tables[i].Rows.Count; row++) //r stands for ExcelRow and col for ExcelColumn 
        { 
         // Excel row and column start positions for writing Row=1 and Col=1 
         for (int col = 1; col <= ds.Tables[i].Columns.Count; col++) 
          ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Rows[row][col - 1].ToString(); 


         r++; 
        } 
ExcelWorkSheet.Name = SheetNames[i];//Renaming the ExcelSheets 


      } 


      string dirPageName = D:\projectPath; 
      FileName =dirPageName +abc + ".xlsx"; 


      if (!Directory.Exists(dirPageName)) 
      { 
       Directory.CreateDirectory(dirPageName); 
      } 

      if (File.Exists(FileName)) 
      { 
       File.Delete(FileName); 
      } 


      ExcelWorkBook.SaveAs(FileName); 

      ExcelWorkBook.Close(); 
      ExcelApp.Quit(); 
      Marshal.ReleaseComObject(ExcelWorkSheet); 
      Marshal.ReleaseComObject(ExcelWorkBook); 
      Marshal.ReleaseComObject(ExcelApp); 
      ltMessage.Text ="File Successfully exported"; 
      ltMessage.Visible = true; 




     } 
     catch (Exception exHandle) 
     { 



      ltMessage.Text = exHandle.Message; 
      ltMessage.Visible = true; 
     } 
     finally 
     { 

      foreach (Process process in Process.GetProcessesByName("Excel")) 
       process.Kill(); 
     } 
    } 

回答

0

HRESULT:0x800A03EC是未知的(VB.Net)COM錯誤。當Excel因爲輸入或參數錯誤或無法正常工作而拋出一些錯誤時,通常會發生這種情況。

還仔細檢查錯誤的細節。在我的情況下,數據部分有一個項目,它爲我提供了有關Excel拋出的確切錯誤的線索。

@Erwin說:IIS用戶帳戶必須具有寫入文件的權限。在以下文章中搜索0x800A03EC,How to Create Excel file in ASP.NET C#

相關問題