【问题标题】:How to export data from SQL server database to Excel 2007 in ASP.NET?如何在 ASP.NET 中将数据从 SQL Server 数据库导出到 Excel 2007?
【发布时间】:2011-12-20 17:10:08
【问题描述】:

我正在开发一个应用程序,我需要提供使用 ASP.NET 将我的 SQL 数据库导出为 Excel 2007 格式的功能。

我首先绑定了网格,然后单击按钮将数据导出到 Excel。但是当数据很大时,绑定网格需要时间,因此当我单击按钮时,连接就会丢失。

【问题讨论】:

  • 你目前用什么导出excel?

标签: c# asp.net


【解决方案1】:

Take a look at EPPlus。这是一个谷歌代码托管项目,可以“在服务器上创建高级 Excel 2007/2010 电子表格。EPPlus 是一个 .net 库,它使用 Open Office Xml 格式 (xlsx) 读写 Excel 2007/2010 文件。”

将文件导入我的项目后,我花了一些时间才让它真正工作,所以我会给你一些我的工作示例代码。代码可能不完全是您想要对文件执行的操作,但它会为您提供一个很好的模板。

就页面生命周期而言:代码位于 .ashx 处理程序页面上,因此我在浏览器中打开 domain.com/toexcel.ashx 并下载文件。 p>

这个库对我来说效果很好,文件输出似乎是完全有效/兼容的 Excel 文件。

顺便说一句,我不隶属,只是一个忠实的粉丝:)

<%@ WebHandler Language="C#" Class="excel" %>

using System;
using System.Web;
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Style;
using System.Drawing;
using System.Data;


public class excel : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        using (ExcelPackage pck = new ExcelPackage())
        {
            int id = int.Parse(context.Request.QueryString["id"]);
            DateTime now = DateTime.Now;

            //get and format datatable
            Project proj = new Project(id);
            DataTable items = proj.getItemsDataTable();

            items = PmFunctions.prettyDates(items);
            items = PmFunctions.prettyMoney(items);

            //new worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add(proj.getTitle());

            //load data
            ws.Cells["A1"].Value = proj.getTitle();
            ws.Cells["A1"].Style.Font.Size = 20;

            ws.Cells["A2"].Value = "Report Date:";
            ws.Cells["C2"].Value = now.ToShortDateString();

            ws.Cells["A4"].Value = "Estimate Total:";
            ws.Cells["C4"].Value = String.Format("{0:C}", proj.getProjectEstimate());

            ws.Cells["A5"].Value = "Actual Total:";
            ws.Cells["C5"].Value = String.Format("{0:C}", proj.getProjectTotal());

            ws.Cells["A7"].LoadFromDataTable(items, true);

            //Stylings
            using (ExcelRange rng = ws.Cells["A7:J7"])
            {
                rng.Style.Font.Bold = true;
                rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
                rng.Style.Font.Color.SetColor(Color.White);
            }



            //Write to the response
            context.Response.Clear();
            context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            context.Response.AddHeader("content-disposition", "attachment;  filename=" + proj.getTitle() + " Report - " + now.ToShortDateString() + ".xlsx");
            context.Response.BinaryWrite(pck.GetAsByteArray());
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2020-10-30
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多