创建asp.net Excel工作簿 [英] Create Excel workbook in asp.net

查看:137
本文介绍了创建asp.net Excel工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成一经按钮点击FL用户的Excel文件。我用的是 Netoffice 之前这对于桌面应用程序工作得很好。
但现在我想要做同样的事情用一个asp.net应用程序。这样,我的服务器code没有到Excel的客户端副本的访问。我应该采取什么办法?

I need to generate an excel file for a user on the fl upon button click. I was using Netoffice before which worked fine for desktop applications. But now I want to do the same thing with an asp.net app. This way my server code doesn't have an access to the client's copy of excel. What approach should I take?

推荐答案

使用 EPPlus 。它可以让你在服务器上创建的Excel US preadsheets。我用它和它的工作太棒了。它支持先进的功能。

Use EPPlus. It allows you to create Excel spreadsheets on the server. I've used it and it worked great. It supports advanced functions.

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();
}

这篇关于创建asp.net Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆