首页 > 解决方案 > 在 iis 中托管时无法打开 excel 文件

问题描述

我有一个按钮,它在其中打开一个透视格式的 excel 文件,该文件与 localhost 完美配合,但是当我将它托管到 IIS 时出现问题,它会引发以下错误:

操作不可用(HRESULT 异常:0x800401E3 (MK_E_UNAVAILABLE))

我使用的代码是:

using (OleDbConnection conn = new OleDbConnection("Provider=MSOLAP;Data Source=" + DS + ";Initial Catalog=" + Initial_Catalog + ";user id=" + (string)(Session["name"]) + ";password=" + (string)(Session["password"]) + ";"))
{
    string connection = @"OLEDB;Provider=MSOLAP;Data Source=" + DS + ";Initial Catalog=" + Initial_Catalog + ";user id=" + (string)(Session["name"]) + ";password=" + (string)(Session["password"]) + ";";
    conn.Open();
    //Create a command, using this connection
    //AdomdCommand cmd = conn.CreateCommand();
    Microsoft.Office.Interop.Excel.Application app;
    app = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook wkbk = (Microsoft.Office.Interop.Excel.Workbook)app.Workbooks.Add(Type.Missing);
    Microsoft.Office.Interop.Excel.Worksheet sheet = (Excel.Worksheet)(wkbk.ActiveSheet);
    Microsoft.Office.Interop.Excel.Application objAplicacion;
    objAplicacion = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");


    //wkbk.Open(sheet);
    Microsoft.Office.Interop.Excel.PivotCache pivotCache;
    pivotCache = wkbk.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlExternal, Type.Missing);
    //pivotCache.CommandText = "Lempo_Test1";
    pivotCache.Connection = connection;

    pivotCache.MaintainConnection = true;
    pivotCache.CommandText = "Lempo";
    pivotCache.CommandType = Microsoft.Office.Interop.Excel.XlCmdType.xlCmdCube;


    // Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)objAplicacion.ActiveSheet;
    Microsoft.Office.Interop.Excel.PivotTables pivotTables = (Microsoft.Office.Interop.Excel.PivotTables)sheet.PivotTables(Type.Missing);
    Microsoft.Office.Interop.Excel.PivotTable pivotTable = pivotTables.Add(pivotCache, sheet.Cells[1, 1], "PivotTable1", true, Type.Missing);

    pivotTable.SmallGrid = false;
    pivotTable.ShowTableStyleRowStripes = true;
    pivotTable.TableStyle2 = "PivotStyleLight1";
    app.ScreenUpdating = true;
    app.DisplayAlerts = true;
    app.Visible = true;
    app.UserControl = true;
    app.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized;
    // Response.Redirect("Board.aspx");
}

请帮我解决这个错误

标签: c#asp.netexcel

解决方案


推荐阅读