0

当我在我的个人电脑上工作时,我下面的代码工作正常,xsl 将保存在指定的路径中。但是当托管应用程序并在保存 xsl 文件期间,我收到错误消息,例如 HRESULT 中的异常:0x800A03EC

请让我知道如何在客户端电脑中保存 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();
        }
    }
4

1 回答 1

0

HRESULT:0x800A03EC 是未知的(对于 VB.Net)COM 错误。这通常发生在 Excel 因您的输入或参数错误或不起作用而引发错误时。

还要仔细检查错误细节。在我的情况下,数据部分有一个项目,它为我提供了有关 Excel 引发的确切错误的线索。

@Erwin 说:IIS 用户帐户必须具有写入文件的权限。在以下文章中搜索 0x800A03EC,如何在 ASP.NET C# 中创建 Excel 文件

于 2013-08-31T01:09:23.153 回答