代码如下:    private void ToExcel(DataSet ds)
    {
        object missing = Missing.Value;
        string savePath = Server.MapPath("./") + @"ExcelFile/";
        DirectoryInfo upDir = new DirectoryInfo(savePath);
        if (!upDir.Exists)
        {
            upDir.Create();
        }
        if (ds.Tables[0].Rows.Count != 0)
        {
            savePath += ds.Tables[0].Rows[0]["员工姓名"].ToString().Trim();
            Excel.Application excel = new Excel.ApplicationClass();
            excel.Application.Workbooks.Add(true);               //Sheets xslSheets = Excel.Worksheets;
            //xslSheets.get
            for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    excel.Cells[i,j] = ds.Tables[0].Rows[i][j].ToString();
                }
            }            excel.Visible = false;
            excel.ActiveWorkbook.SaveCopyAs(savePath);
            excel.Workbooks.Close();
            excel.Application.Quit();
            excel.Quit();
        }
    }

解决方案 »

  1.   

    excel.dll 导出 excel 时,异常来自 HRESULT:0x800A03EC 
    麻烦大家帮我看看是什么原因,谢谢!另外:用Response导出Excel时,能不能实现自动保存在服务器的某个目录下面?如下面代码,如果能自动保存的话,应该怎么改?FileStream file = new FileStream(this.Tem_OutPath, FileMode.Open);
                    byte[] buffer = new byte[(int)file.Length];
                    file.Read(buffer, 0, (int)file.Length);
                    file.Close();
                    string temp = string.Format("attachment;filename={0}", "ExportCSV.csv");
                    Response.ClearHeaders();
                    Response.AppendHeader("Content-disposition", temp);
                    Response.ContentType = "application/csv";
                    Response.BinaryWrite(buffer);
                    Response.Flush();
                    Response.End();谢谢!
      

  2.   

    这个问题怎么解决的啊?
    我也遇到了!  发我邮箱[email protected]  谢谢啊
      

  3.   

    怎么解决的,发一份[email protected]
      

  4.   

    怎么解决的,发一份[email protected]
      

  5.   

    楼主,谢谢,我与遇到了.
    excel.ActiveWorkbook.SaveAs(strExcelFileName, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, 
                Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);  我这样保存倒是没错,在 2003下面.但是在 vista 下面就会有问题了,能告诉我你是怎么解决的吗.谢谢了[email protected]
      

  6.   

    怎么解决的!谢谢能发一份吗?[email protected]
      

  7.   

    怎么解决的!我也遇到了这个问题,谢谢,能说一下解决方案吗? [email protected]
      

  8.   

    楼主 也发我一份啊 我跟你错的一样的问题 [email protected]
      

  9.   

    13楼正解!!如下:
    for (int i = 0; i < data.Columns.Count; i++)
            {
                excel.Cells[2, i] = data.Columns[i].ColumnName.ToString();
            }
    》》》》》
    for (int i = 0; i < data.Columns.Count; i++)
            {
                excel.Cells[2, i+1] = data.Columns[i].ColumnName.ToString();
            }