首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

C# 导出excel在本土测试通过,但是在服务器时就出错

2012-12-19 
C# 导出excel在本地测试通过,但是在服务器时就出错本帖最后由 ghj303155066 于 2012-11-23 14:17:11 编辑

C# 导出excel在本地测试通过,但是在服务器时就出错
本帖最后由 ghj303155066 于 2012-11-23 14:17:11 编辑 这是我C#Webform 导出excel的方法//文件存在时先删除文件后再进行下一步操作        

    public bool CreateExcelFileForDataTable(System.Data.DataTable table, string strFullFilePath, string title)
    {
        //文件存在时先删除文件后再进行下一步操作 
        FileInfo file = new FileInfo(strFullFilePath);
        if (file.Exists)
        {
            file.Delete();
        }
        int rowIndex = 3;      //开始写入数据的单元格行 
        int colIndex = 0;      //开始写入数据的单元格列 
        System.Reflection.Missing miss = System.Reflection.Missing.Value;
        Excel.ApplicationClass mExcel = new Excel.ApplicationClass();
        mExcel.Visible = false;
        Excel.Workbooks mBooks = (Excel.Workbooks)mExcel.Workbooks;
        Excel.Workbook mBook = (Excel.Workbook)(mBooks.Add(miss));
        Excel.Worksheet mSheet = (Excel.Worksheet)mBook.ActiveSheet;
        Excel.Range er = mSheet.get_Range((object)"A1", System.Reflection.Missing.Value); //向Excel文件中写入标题文本 
        er.Value2 = title;
        try
        {
            foreach (DataColumn col in table.Columns)    //将所得到的表的列名,赋值给单元格 
            {
                colIndex++;
                mSheet.Cells[3, colIndex] = col.ColumnName;
            }
            foreach (DataRow row in table.Rows)    //同样方法处理数据 
            {
                rowIndex++;
                colIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    colIndex++;
                    if (colIndex == 2)
                    {


                        mSheet.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();//第二行数据为银行帐号信息转换为字符防止首位0丢失 
                    }
                    else
                    {
                        mSheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                    }
                }
            }
            //保存工作已写入数据的工作表 
            mBook.SaveAs(strFullFilePath, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
            Label_Message.Text = "查询结果已经保存到:" + strFullFilePath+".xlsx ";
            return true;
        }
        catch (Exception ee)
        {
            throw new Exception(ee.Message);
            Label_Message.Text = ee.Message.ToString();
        } 
    finally    //finally中的代码主要用来释放内存和中止进程() 
        {
            mBook.Close(false, miss, miss);
            mBooks.Close();
            mExcel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(er);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mBooks);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mExcel);
            GC.Collect();
            
        }
        return false;
    }
    protected void cmd_Export_Click(object sender, EventArgs e)
    {
        strSql = "select  fl_ID as '编号',fl_IL as '插损',fl_BL as '回损'  from Table_CheckedOfChengpin where DateDiff(dd,dt_Date,getdate())=0 order by fl_ID desc";


        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(strSql, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds);
        sqlcon.Close();

        //

        CreateExcelFileForDataTable(myds.Tables[0], "\\查询的数据", "插回损数据");
    }


在本机(win7 64位、VS2010+office2010)调试运行完全正常,但是放在服务器(win server2003、.net4.0+office2010),在导出excel的时候就会出错。

浏览器出错提示为:

[UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).]
   Checked_WebForm_CheckDataShow.CreateExcelFileForDataTable(DataTable table, String strFullFilePath, String title) +111
   Checked_WebForm_CheckDataShow.cmd_Export_Click(Object sender, EventArgs e) +208
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

[最优解释]
DCOM权限设置。

建议你使用NPOI这样的组件来操作EXCEL,这样服务器就不需要安装OFFICE了。
[其他解释]
在web.config里添加 
<identity impersonate ="true" userName="账号" password="密码"/>
其中账号密码是你计算机的登录账号和密码,然后再试试
[其他解释]
组件服务->dcom配置->excel配置->属性安全->激活权限访问权限加上netservice
[其他解释]
报什么错?
服务器上安装Office了吗?
在服务器上打开源代码看看Office的引用版本是否一致
有的时候是组件没有注册引起的。。
[其他解释]
你的后缀名改成.xls 试试 我之前也是这样 本地没问题
[其他解释]
权限不足,上面说的方法都行,可以参照4楼配置,或者
组件服务->dcom配置->excel配置->属性安全->激活权限访问权限 指定登录用户和密码
[其他解释]
就是权限的问题,excle模版、组件服务->dcom配置->excel配置->属性安全->激活权限访问权限加上netservice 都有可能把

[其他解释]
引用:
权限不足,上面说的方法都行,可以参照4楼配置,或者
组件服务->dcom配置->excel配置->属性安全->激活权限访问权限 指定登录用户和密码



学习了,我遇到过1楼的问题 提示错误不像是这个 所以应排除
------其他解决方案--------------------


配置dcom权限参考
[其他解释]
建议使用第三方类库NPOI,使用NPOI把数据导出Excel,服务器无需安装office,而且性能非常OK,楼主可以试试,不会让你失望的
[其他解释]
就是权限的问题,excle模版、组件服务->dcom配置->excel配置->属性安全->激活权限访问权限加上netservice 

这样操作后,还是出现了错误。。 


Server Error in '/' Application.
--------------------------------------------

?ú′??ò′??ì????2?×?£?Microsoft Excel ?T·¨?ù′?′ò?a?ò±£′?è?o???μμ?£

? òa????μ??ü?àμ??éó??ú′?£???1?±?2??ùê1ó?μ?1¤×÷2??ò3ìDò?£

? òa??êí·?′??ì????£???é?3y?àó|′??ìé?2?Dèòaμ????t?£ 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.COMException: ?ú′??ò′??ì????2?×?£?Microsoft Excel ?T·¨?ù′?′ò?a?ò±£′?è?o???μμ?£

? òa????μ??ü?àμ??éó??ú′?£???1?±?2??ùê1ó?μ?1¤×÷2??ò3ìDò?£

? òa??êí·?′??ì????£???é?3y?àó|′??ìé?2?Dèòaμ????t?£

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[COMException (0x800a03ec): ?ú′??ò′??ì????2?×?£?Microsoft Excel ?T·¨?ù′?′ò?a?ò±£′?è?o???μμ?£

? òa????μ??ü?àμ??éó??ú′?£???1?±?2??ùê1ó?μ?1¤×÷2??ò3ìDò?£

? òa??êí·?′??ì????£???é?3y?àó|′??ìé?2?Dèòaμ????t?£]
   Microsoft.Office.Interop.Excel.Workbooks.Add(Object Template) +0
   Checked_WebForm_CheckDataShow.CreateExcelFileForDataTable(DataTable table, String strFullFilePath, String title) +162
   Checked_WebForm_CheckDataShow.cmd_Export_Click(Object sender, EventArgs e) +208
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

热点排行