简体   繁体   中英

How to export a DataTable variable to EXCEL and download the excel

I'm trying to use NPOI like this:

private Stream RenderDataTableToExcel(DataTable SourceTable)
{
    XSSFWorkbook workbook = null;
    MemoryStream ms = null;
    ISheet sheet = null;
    XSSFRow headerRow = null;
    try
    {
        workbook = new XSSFWorkbook();
        ms = new MemoryStream();
        sheet = workbook.CreateSheet();
        headerRow = (XSSFRow)sheet.CreateRow(0);
        foreach(DataColumn column in SourceTable.Columns)
            headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
        int rowIndex = 1;
        foreach(DataRow row in SourceTable.Rows)
        {
            XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
            foreach(DataColumn column in SourceTable.Columns)
                dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
            ++rowIndex;
        }
        for (int i = 0; i <= SourceTable.Columns.Count; ++i)
            sheet.AutoSizeColumn(i);
        workbook.Write(ms);
        ms.Flush();
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
        ms.Close();
        sheet = null;
        headerRow = null;
        workbook = null;
    }
    return ms;
}
private void DownloadExcel(DataTable dt, string reportName)
{
    Stream s = RenderDataTableToExcel(dt);
    if (s != null)
    {
        MemoryStream ms = s as MemoryStream;
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + HttpUtility.UrlEncode(reportName) + DateTime.Now.ToString("yyyyMMdd") + ".xlsx"));
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
        Response.BinaryWrite(ms.ToArray());
        Response.Flush();
        ms.Close();
        ms.Dispose();
    }
    else
        Response.Write("Error!Connot Download");
}

I've got binary stream instead of a ms-excel file.

PS: I really want to know how to generate a file for download, that is, why your code works, the browser generates the file or the server?

NPOI is a helper module to create excel files four you. This is created server-side and in memory. ( workbook.Write(ms) write the excel file in memory ) The excel file goes over the wire as an byte[] and the browser decide based on file and contenttype what to do with it.

When u use classic asp.net then put a link in your aspx page like below

<a target="_blank" href="Handler.ashx" >download...</a>

Create a Handler.ashx and put the code fom DownloadExcel into ProcessRequest from Handler.ashx.

public void ProcessRequest (HttpContext context)
{
    //create dumy data, or in youre case the data form somewhere else
    DataTable table = new DataTable();
    table.Columns.AddRange(new[]
        {
            new DataColumn("Name")
        });
    table.Rows.Add("david");
    table.Rows.Add("Ruud");

    // your code
    Stream s = RenderDataTableToExcel(dt);
    if (s != null)
    {
        MemoryStream ms = s as MemoryStream;
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + HttpUtility.UrlEncode(reportName) + DateTime.Now.ToString("yyyyMMdd") + ".xlsx"));
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
        Response.BinaryWrite(ms.ToArray());
        Response.Flush();
        ms.Close();
        ms.Dispose();
    }
    else
        Response.Write("Error!Connot Download");
    }
}

in MVC its like below.

[HttpGet]
public ActionResult ExportToExcel(string reportName)
{
    byte [] reportDocument = RenderDataTableToExcel().ToArray();

    Response.StatusCode = (int)HttpStatusCode.OK;
    return File(reportDocument, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", reportName);
}

I've used ajax TVT.

window.location.href = "?action=DownloadAll";
    //$.ajax({
    //    url: "?action=DownloadAll",
    //    type: "get",
    //    success: function () { }
    //});

You can turn a DataTable into an Excel worksheet with some very readable code:

XLWorkbook wb = new XLWorkbook();
DataTable dt = GetDataTableOrWhatever();
wb.Worksheets.Add(dt,"WorksheetName");

It will run super-fast too!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM