2017-10-13 12 views
0

私はここで少し問題があります:私はasp.netアプリケーションをC#で構築し、Excelピボットテーブルを生成します。 Visual StudioからIIS Expressを使用してlocalhostで実行すると問題はありません。データソースからファイルを作成し、問題なくダウンロードフォルダにダウンロードします。オブジェクトの冗長 "saveAs"にアクセスできないExcel WorkBook ASP.NET

しかし、私はIISサーバーで私の生産にそれを入れて、アプリケーションをテストしてみたら、私は、エクスポートボタンをクリックしたときに、私は次の例外を取得:私からそれを実行したときに

Description: unhandled exception when executing the current Web request. Review the stack trace for more information about the error and where it originated in the code.

Exception: System.Runtime.InteropServices.COMException: Cannot obtain acces to the SaveAs property of the Workbook class.

Source code error:

Unhandled exception when executing the current Web request. Review the stack trace for more information about the error and where it originated in the code.

Pool tracking:

[COMException (0x800a03ec): Cannot obtain access to the SaveAs property of Workbook class.]
System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message) +145
CallSite.Target(Closure , CallSite , ComObject , String) +702
System.Dynamic.UpdateDelegates.UpdateAndExecute2(CallSite site, T0 arg0, T1 arg1) +491
System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2(CallSite site, T0 arg0, T1 arg1) +657
inicio.pivotTable(DataTable source) +11550
inicio.btnExportar_Click(Object sender, EventArgs e) +153
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11773973
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +150
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5062

をこののみ発生IISサーバー。ここで

は、私は私のExcelファイルを生成するために使用するコードです:

private void pivotTable(DataTable source) { 
    System.Globalization.CultureInfo oldCulture = System.Threading.Thread.CurrentThread.CurrentCulture; 
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us"); 

    //get the type of excel application 
    Type ExcelType = Type.GetTypeFromProgID("Excel.Application"); 
    dynamic xlApp = Activator.CreateInstance(ExcelType); 

    dynamic xlWBook = xlApp.WorkBooks.Add(); 

    DataTable dt = source; 

    int x = dt.Rows.Count; 
    int y = (dt.Columns.Count); 
    int col, row; 

    Object[,] rawData = new Object[x + 1, y]; 

    //filling data 
    for (col = 0; col < y; col++) { 
     rawData[0, col] = dt.Columns[col].ColumnName.ToUpper(); 
    } 

    for (col = 0; col < y; col++) { 
     for (row = 0; row < x; row++) { 
      rawData[row + 1, col] = dt.Rows[row].ItemArray[col]; 
     } 
    } 

    // Excel worksheet 
    dynamic xlWSheet; 
    String uRange = string.Format("A1:{0}{1}", excelColName(dt.Columns.Count), dt.Rows.Count + 1); 

    xlWSheet = xlWBook.WorkSheets.Add(); 
    xlWSheet.name = dt.TableName; 
    //rawData as exactly as the datasource 
    xlWSheet.Range(uRange, Type.Missing).Value2 = rawData; 

    dynamic dataRange = xlWSheet.Range(uRange); 

    xlWBook.Names.Add(Name: dt.TableName + "_Range", RefersTo: dataRange); 

    xlWSheet = xlWBook.WorkSheets.Add(); 
    xlWSheet.name = "Pivot_" + dt.TableName; 

    dynamic ptCache = xlWBook.PivotCaches.Add(SourceType: 1, SourceData: dt.TableName + "_Range"); 
    dynamic ptTable = ptCache.CreatePivotTable(TableDestination: xlWSheet.Range("A3"), TableName: "PT" + dt.TableName); 

    //generating pivot table 
    ptTable.ManualUpdate = true; 
    ptTable.PivotFields("CLIENTE").Orientation = 3; 
    ptTable.PivotFields("CLIENTE").Position = 1; 
    ptTable.PivotFields("CLASE").Orientation = 1; 
    ptTable.PivotFields("CLASE").Position = 1; 
    ptTable.PivotFields("AÑO").Orientation = 2; 
    ptTable.PivotFields("AÑO").Position = 1; 

    ptTable.CalculatedFields().Add("MONTO", "=SUM(TOTAL)", true); 
    ptTable.PivotFields("MONTO").Orientation = 4; 
    ptTable.ManualUpdate = false; 

    ptCache = null; 
    ptTable = null; 
    xlWSheet = null; 
    dataRange = null; 
    //get the path from webconfig 
    string path = ConfigurationManager.AppSettings["path"]; 
    //check if the file is alredy in the path, just to avoid excel asking to replace it 
    if (File.Exists(path + Session["usuario"].ToString().Replace(" ", string.Empty) + ".xlsx")) { 
     File.Delete(path + Session["usuario"].ToString().Replace(" ", string.Empty) + ".xlsx"); 
    } 

    //save the generated file in the path 
    //this is the method mentioned in the iis exception, but it says "property" 
    xlWBook.SaveAs(path + Session["usuario"].ToString().Replace(" ", string.Empty) + ".xlsx"); 
    //quit excel 
    xlWBook.Close(); 
    xlApp.Quit(); 
    xlWBook = null; 
    xlApp = null; 

    GC.Collect(); 
    FileStream fs = null; 
    string FileName = "presupuesto" + Session["usuario"].ToString().Replace(" ", string.Empty) + ".xlsx"; 
    //get the file i saved before 
    fs = System.IO.File.Open(path + Session["usuario"].ToString().Replace(" ", string.Empty) + ".xlsx", System.IO.FileMode.Open);    
    byte[] file = new byte[fs.Length]; 
    //read the file 
    fs.Read(file, 0, Convert.ToInt32(fs.Length)); 
    fs.Close(); 
    //send the file in response 
    Response.AddHeader("Content-disposition", "attachment; filename=" + FileName); 
    //delete de file because i don't want it to be in server anymore 
    System.IO.File.Delete(path + Session["usuario"].ToString().Replace(" ", string.Empty) + ".xlsx"); 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    //send the file in response 
    Response.BinaryWrite(file); 
    Response.End(); 

    System.Threading.Thread.CurrentThread.CurrentCulture = oldCulture; 
} 

私が何をするか分からない、私が使用してアプリケーションを実行してみます任意の助け

+0

これをASP.NETとタグ付けしました。なぜASP.NETでOffice Interopを使用していますか?それはひどい考えです。それをしないでください。 [マイクロソフトは(https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office)には言及していない] EPPlus、NPOI、ClosedXML、AsposeなどのCOMを使用せずにExcelファイルを生成できる優れた方法があります。 Open XML SDKなど – mason

答えて

0

のために感謝しますよサーバー上のWindowsユーザーの資格情報を使用するIISのアプリケーションプール。サーバーにもログオンに使用できます。インタラクティブなWindowsセッションができないユーザーの場合、Officeオートメーションは機能しません。

+0

私はcom権限に問題があったので、私は自分のアプリケーションプールにユーザーを入れて、権限を持つユーザーと一緒にrunnningを実行するので、今この別の例外が発生する –