在asp.net中创建Excel工作簿

3

我需要在用户点击按钮时为其生成一个Excel文件。以前我使用的是 Netoffice,它适用于桌面应用程序。但现在我想在asp.net应用程序中做同样的事情。这样我的服务器代码就没有访问客户端Excel副本的权限。我应该采取什么方法?


看起来很像 https://dev59.com/FHVC5IYBdhLWcg3w51lv - Mathias
5个回答

8
使用EPPlus,可以在服务器上创建Excel电子表格。我曾经使用它,并且效果很好。它支持高级功能。
using (ExcelPackage pck = new ExcelPackage())
{
    //Create the worksheet
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");

    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
    ws.Cells["A1"].LoadFromDataTable(tbl, true);

    //Format the header for column 1-3
    using (ExcelRange rng = ws.Cells["A1:C1"])
    {
        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);
    }

    //Example how to Format Column 1 as numeric 
    using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
    {
        col.Style.Numberformat.Format = "#,##0.00";
        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
    }

    //Write it back to the client
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                    
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
}

看起来很酷。但我找不到如何创建一个工作簿并将其下载到客户端而不保存到服务器的示例?你知道有什么简单的方法吗? - user194076
我添加了一些代码,展示如何创建一个带有电子表格的工作簿并将其写入客户端。 - Tyler Treat
EPPlus应该下载为Dll并链接到我的项目引用中吗?我想使用它... - Rick

3

最灵活且最有可能做到你需要的功能需要一些工作,但它是免费的,并且真正有效。使用工具包查看现有文档,了解如何创建所需的功能。

Open XML 2.0 SDK


0
您可以使用DataGrid动态创建Excel文件,而无需使用Excel。
public static void ExportDataSetToExcel(DataSet ds, string filename)
{
    HttpResponse response = HttpContext.Current.Response;

    // first let's clean up the response.object
    response.Clear();
    response.Charset = "";

    // set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader(
        "Content-Disposition",
        "attachment;filename=\"" + filename + "\""
    );

   // create a string writer
   using (StringWriter sw = new StringWriter())
   {
       using (HtmlTextWriter htw = new HtmlTextWriter(sw))
       {
            // instantiate a datagrid
            DataGrid dg = new DataGrid();
            dg.DataSource = ds.Tables[0];
            dg.DataBind();
            dg.RenderControl(htw);
            response.Write(sw.ToString());
            dg.Dispose();
            ds.Dispose();
            response.End();
       }
    }
}

这个解决方案向客户端返回 HTML,但声明它实际上是一个 Excel 文件。问题在于 Excel 发现了你的小伎俩,并向用户抛出一个警告对话框,解释有人试图欺骗他们。 - Ian Boyd

0
你可以尝试使用简单的HTML表格(包括html、head和body标签),然后将其保存为XLS扩展名。

enter image description here


不,我需要进行一些复杂的Excel操作。NetOffice非常适合,但我不明白为什么它不能与ASP.NET一起使用。 - user194076

0

Netoffice需要在执行机器上安装MS Office。您的服务器有吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接