能够生成Excel报告并将其推送到客户端计算机而无需在服务器中保存文件

时间:2015-09-23 07:00:58

标签: c# asp.net excel excel-interop

目前我从表中获取数据并将其转换为excel工作簿,然后将其保存在服务器空间中,然后使用Response类将文件推送到客户端计算机。代码工作正常。但我知道不将文件存储在物理存储中,因为它会增加服务器存储并直接将其推送到客户端计算机。如果有可能,请提供建议。提前谢谢。

protected  void GenerateExcelFile()
{
    string data = null;
    int i = 0;
    int j = 0;

    Application xlApp;
    Workbook xlWorkBook;
    Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;

    xlApp = new Application();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
    DataSet ds = new DataSet();
    ds = GetTableData();// Returns dataset
    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
    {
        xlWorkSheet.Cells[1, k + 1] = ds.Tables[0].Columns[k].ColumnName;
    }
    for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
    {
        for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
        {
            data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
            xlWorkSheet.Cells[i + 2, j + 1] = data;
        }
    }

    xlWorkBook.SaveAs("c:\\csharp.net-informations.xls", XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);


    string FileName = "Report.xls";
    string filepath = "c:\\csharp.net-informations.xls";
    WebClient req = new WebClient();
    HttpResponse response = HttpContext.Current.Response;
    FileInfo file = new FileInfo(filepath);
    response.Clear();
    response.ClearContent();
    response.ClearHeaders();
    response.Buffer = true;
    Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = ReturnExtension(file.Extension.ToLower());
    Response.TransmitFile(file.FullName);
    response.End();

3 个答案:

答案 0 :(得分:0)

我使用此功能,直接在客户端PC上下载excel

            System.IO.StringWriter objStringWriter1 = new System.IO.StringWriter();
            System.Web.UI.WebControls.GridView gridView1 = new System.Web.UI.WebControls.GridView();
            System.Web.UI.HtmlTextWriter objHtmlTextWriter1 = new System.Web.UI.HtmlTextWriter(objStringWriter1);

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);

            gridView1.DataSource = tempDataTable;
            gridView1.DataBind();
            gridView1.RenderControl(objHtmlTextWriter1);

            gridView1.Dispose();
            HttpContext.Current.Response.Write(objStringWriter1.ToString());

            HttpContext.Current.Response.End();

答案 1 :(得分:0)

如果要创建服务器端Excel工作表,则必须先将其保存,然后再将其发送给客户端。

如果您不想将其保存在服务器中,那么您只需以列表格式发送响应,然后就可以在客户端创建Excel工作表。 Create Excel sheet in Javascript

答案 2 :(得分:0)

使用OOXML库而不是Microsoft.Office.Interop.Excel。库包称为EPPlus。这是您在项目中进行设置的方式:

  1. 从Visual Studio的程序包管理器控制台中,键入:Install-Package EPPlus(这将根据需要安装库和引用)
  2. 将此语句(using OfficeOpenXml;)添加到您的代码
  3. 以下是使用OOXML库的一些示例代码。您需要创建一个ExcelPackage:

        ExcelPackage pck = new ExcelPackage();
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("exported data");
        // Excel sheet headings
        ws.Cells[1, 1].Value = "Column 1 title";
        ws.Cells[1, 2].Value = "Column 2 title";
    


    现在使用以下代码将excel文件保存在客户端上:

            Byte[] fileBytes = pck.GetAsByteArray(); // convert excel data to bytes
    
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=\"fileName1.xlsx\"");
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
            Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
            Response.BinaryWrite(fileBytes);
            Response.End();