HRESULT异常:0x800A03EC仅在部署到IIS 10后发生

时间:2018-01-06 06:02:38

标签: c# asp.net-mvc excel iis

我正在尝试创建一个excel文件并将其保存在文件系统中。

以下是我该怎么做:

private string CreateExcel(List<ExcelResult> list, string fileName) {
    // Create the file in the server
    string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Media/Temp/Excel/" + fileName + ".xls");
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add("SerialNo", typeof(int));
    dt.Columns.Add("Code", typeof(string));

    foreach (ExcelResult item in list) {
        dt.Rows.Add(item.SerialNo, item.Code);
    }

    Application app = new Application();
    Workbook wb = app.Workbooks.Add(Type.Missing);

    Worksheet ws = (Worksheet)wb.ActiveSheet;
    ws.DisplayRightToLeft = false;
    ws.Name = "GiftCards";

    ws.Cells[1, 1] = "SerialNo";
    ws.Cells[1, 2] = "Code";
    int rowcount = 2;

    foreach (DataRow datarow in dt.Rows) {
        rowcount += 1;
        for (int i = 1; i <= dt.Columns.Count; i++)
            ws.Cells[rowcount, i] = datarow[i - 1].ToString();
    }

    try {
        wb.SaveAs(path);
        wb.Close();
        app.Quit();

        return fileName + ".xls";
    } catch (Exception ex) {
        Debug.WriteLine("Exception thrown -> " + ex.Message);
        return ex.Message;
    }
}

只有在IIS上部署应用程序且本地(iis express)没有任何问题时才会出现错误。

任何想法?

1 个答案:

答案 0 :(得分:0)

找到Answer并阅读Article

提供的Bommer

我发现这是服务器本身的一个问题,通过浏览文章中提供的结构解决了这个问题。