0

我创建了这个 COM 对象:

using Microsoft.Office.Interop.Excel;

 private Application app = null;
 private Workbook workbook = null;
 private Worksheet worksheet = null;


        worksheet.Cells[1, 1] = "title ";

        worksheet.Cells[2, 1] = "row 1 header";
        worksheet.Cells[2, 2] = "row 1 header";
        worksheet.Cells[2, 3] = "row 1 header";
        worksheet.Cells[2, 4] = "row 1 header";

        var i = 2;
        foreach (var vm in model)
        {

            worksheet.Cells[i, 1] = "value";
            worksheet.Cells[i, 2] = "value";
            worksheet.Cells[i, 3] = "value";
            worksheet.Cells[i, 4] = "value";

            i++;
        }

现在我有了这个 Excel.Application 对象,如何将它发送到浏览器以便用户下载它?

4

1 回答 1

0

在工作簿中插入工作表后,您可以将工作簿和 response.redirect 保存到命运路径。

...

workbook.SaveAs(@"~/YourPathName/ExcelFileName.xls",
                        Microsoft.Office.Interop.Excel.XlFileFormat.xlAddIn8,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing,
                        Type.Missing);

...

System.Web.HttpApplication htt = System.Web.HttpContext.Current.ApplicationInstance;

htt.Response.Redirect(@"~/YourPathName/ExcelFileName.xls");   
于 2013-04-12T17:23:20.377 回答