在iis中托管时无法打开Excel文件

时间:2019-02-08 13:48:26

标签: c# asp.net 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");
}

请帮助我解决此错误

0 个答案:

没有答案