c# - 使用 Microsoft Interop 读取和写入 excel 单元格

标签 c# asp.net excel interop office-automation

我在我的网站部署的网络服务器上有一个 excel 文件。在这种情况下,我有一个屏幕允许用户从 Web 服务器下载 Excel 文件并保存到本地驱动器,然后选择相同的文件路径来导出数据,但我收到以下错误。

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

这是我的代码:

private void btnExportToExcel_Click(object sender, System.EventArgs e)
{
    Microsoft.Office.Interop.Excel.ApplicationClass appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
    DataSet dsResult = new DataSet();
    object missingValue = System.Reflection.Missing.Value;
    decimal wkOpenBal = 0.00m;
    decimal wkAddition = 0.00m; 
    decimal wkDisposal = 0.00m;
    string rptDt = "";

    decimal os_p = 0.00M;
    decimal os_s = 0.00M;
    decimal closs_16_25 = 0.00M;
    decimal closs_26 = 0.00M;
    decimal os_clr_p = 0.00M;
    decimal os_whs_p = 0.00M;
    decimal os_clr_s = 0.00M;
    decimal os_whs_s = 0.00M;           
    decimal mark_to_mkt = 0.00M;
    decimal mark_to_mkt_error = 0.00M;


    if(txtWeeklyFile.Value != "")
    {
        //DATABASE CALL
        objBursaSubmission = (IBursaSubmission) ObjectFactory.Instance.GetObject(ObjectIdentifier.BursaSubmissionManager);                      
        dsResult = objBursaSubmission.GetReportData(txtRptdt.Text);

        try
        {
            if(dsResult != null && dsResult.Tables[0].Rows.Count > 0)
            {
                if(System.IO.File.Exists(txtWeeklyFile.Value.ToString()))
                {
                    wkOpenBal = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["WK_OPENBAL"]);
                    wkAddition = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["WK_ADDITION"]);
                    wkDisposal = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["WK_DISPOSAL"]);
                    rptDt = dsResult.Tables[0].Rows[0]["LOGICAL_DT"].ToString();                

                    os_p = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["OS_P"]);
                    os_s = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["OS_S"]);
                    closs_16_25 = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["CLOSS_16_25"]);
                    closs_26 = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["CLOSS_26"]);
                    os_clr_p = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["OS_CLR_P"]);
                    os_whs_p = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["OS_WHS_P"]);
                    os_clr_s = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["OS_CLR_S"]);
                    os_whs_s = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["OS_WHS_S"]);
                    mark_to_mkt = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["MARK_TO_MKT"]);
                    mark_to_mkt_error = Convert.ToDecimal(dsResult.Tables[0].Rows[0]["mark_to_mkt_error"]);

                    decimal clr_total_p = os_clr_p + os_whs_p;
                    decimal clr_total_s = os_clr_s + os_whs_s;

                    Microsoft.Office.Interop.Excel.Workbook wBook = appExcel.Workbooks.Open(txtWeeklyFile.Value.ToString(),Type.Missing,Type.Missing,Type.Missing,
                        Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,
                        Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);

                    //SHEET MARKET POSITION SHEET 1                                                             
                    Worksheet sheetMarktetPosition1 = (Worksheet)wBook.Sheets[1];                       
                    Range mPositionRange = sheetMarktetPosition1.UsedRange;                 

                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[6,7]).Value2 = rptDt.ToString();

                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[14,3]).Value2 = os_p.ToString();
                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[14,7]).Value2 = mark_to_mkt.ToString();                         
                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[16,3]).Value2 = os_s.ToString();                            
                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[15,5]).Value2 = "";
                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[40,3]).Value2 = clr_total_p.ToString();
                    ((Microsoft.Office.Interop.Excel.Range)mPositionRange.Cells[41,3]).Value2 = clr_total_s.ToString();
                }
            }
        }
        catch (Exception ex)
        {
            //code for any other type of exception
        }
    }
}

最佳答案

您无法从服务器进程安全地使用 Office Automation。查看 Editing an Excel document with Macros in ASP.NET 的答案了解您必须走多远才能确保安全。参见 ASP.NET Web Service using office 2010 COM了解如果您只是尝试从 ASP.NET 调用它会发生什么。

简而言之,问题在于 Office Automation 是为桌面应用程序 Microsoft Office 产品(桌面应用程序)的自动化而设计的。当您从服务器应用程序使用它们时,各种各样的事情都无法正常工作。

关于c# - 使用 Microsoft Interop 读取和写入 excel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530228/

相关文章:

c# - Unity 2D 碰撞检测

c# - 'System.Data.IDbConnection' 到 'System.Data.SqlClient.SqlConnection'

.net - sessionState 超时不工作

javascript - jQuery post 不会将数据发布到 ASP.NET API Controller

excel - 如何在 Microsoft Excel 中的公式后附加多行文本?

sql-server - 从 Sharepoint 到 SQL Server 的 SSIS Excel/CSV

php - 我如何使用 PHP 读取 .xls 文件 (Excel)?

c# - Entity Framework ,获取选中项的周边项

c# - 无法从 Microsoft.Extensions.Logging 解析 ILogger

asp.net - ajax POST 调用 WCF 服务的问题