简体   繁体   English

将数据导出到 asp.net 应用程序中的 Excel 文件

[英]export data to excel file in an asp.net application

有人可以提供有关在 asp.net 网络应用程序中使用 c# 将数据导出到 excel 文件的教程的链接吗?我搜索了互联网,但没有找到任何教程来解释他们是如何做到的。

There is a easy way to use npoi.mapper with just below 2 lines有一种简单的方法来使用npoi.mapper就在 2 行以下

var mapper = new Mapper();
mapper.Save("test.xlsx",  objects, "newSheet");

Pass List to below method, that will convert the list to buffer and then return buffer, a file will be downloaded.将 List 传递给下面的方法,将列表转换为缓冲区,然后返回缓冲区,将下载一个文件。

List<T> resultList = New List<T>();
  byte[] buffer = Write(resultList, true, "AttendenceSummary");
            return File(buffer, "application/excel", reportTitle + ".xlsx");
public static byte[] Write<T>(IEnumerable<T> list, bool xlsxExtension = true, string sheetName = "ExportData")
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            XSSFWorkbook hssfworkbook = new XSSFWorkbook();
            int Rowspersheet = 15000;
            int TotalRows = list.Count();
            int TotalSheets = TotalRows / Rowspersheet;

            for (int i = 0; i <= TotalSheets; i++)
            {
                ISheet sheet1 = hssfworkbook.CreateSheet(sheetName + "_" + i);
                IRow row = sheet1.CreateRow(0);
                int index = 0;
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    ICellStyle cellStyle = hssfworkbook.CreateCellStyle();
                    IFont cellFont = hssfworkbook.CreateFont();

                    cellFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
                    cellStyle.SetFont(cellFont);

                    ICell cell = row.CreateCell(index++);
                    cell.CellStyle = cellStyle;
                    cell.SetCellValue(property.Name);
                }

                int rowIndex = 1;
                // int rowIndex2 = 1;

                foreach (T obj in list.Skip(Rowspersheet * i).Take(Rowspersheet))
                {

                    row = sheet1.CreateRow(rowIndex++);
                    index = 0;

                    foreach (PropertyInfo property in typeof(T).GetProperties())
                    {
                        ICell cell = row.CreateCell(index++);
                        cell.SetCellValue(Convert.ToString(property.GetValue(obj)));
                    }

                }
            }

            MemoryStream file = new MemoryStream();
            hssfworkbook.Write(file);
            return file.ToArray();

        }

I've written a C# class, which lets you write your DataSet, DataTable or List<> data directly into a Excel .xlsx file using the OpenXML libraries.我编写了一个 C# 类,它允许您使用 OpenXML 库将 DataSet、DataTable 或 List<> 数据直接写入 Excel .xlsx 文件。

http://mikesknowledgebase.com/pages/CSharp/ExportToExcel.htmhttp://mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

It's completely free to download, and very ASP.Net friendly.它完全免费下载,并且ASP.Net非常友好。

Just pass my C# function the data to be written, the name of the file you want to create, and your page's "Response" variable, and it'll create the Excel file for you, and write it straight to the Page, ready for the user to Save/Open.只需将要写入的数据、要创建的文件的名称以及页面的“响应”变量传递给我的 C# 函数,它就会为您创建 Excel 文件,并将其直接写入页面,准备好用户保存/打开。

class Employee;
List<Employee> listOfEmployees = new List<Employee>();


// The following ASP.Net code gets run when I click on my "Export to Excel" button.
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
    // It doesn't get much easier than this...
    CreateExcelFile.CreateExcelDocument(listOfEmployees, "Employees.xlsx", Response);
}

(I work for a finanical company, and we'd be lost without this functionality in every one of our apps !!) (我在一家金融公司工作,如果我们的每个应用程序中没有这个功能,我们就会迷失方向!!)

You can use Interop http://www.c-sharpcorner.com/UploadFile/Globalking/datasettoexcel02272006232336PM/datasettoexcel.aspx您可以使用 Interop http://www.c-sharpcorner.com/UploadFile/Globalking/datasettoexcel02272006232336PM/datasettoexcel.aspx

Or if you don't want to install Microsoft Office on a webserver I recommend using CarlosAg.ExcelXmlWriter which can be found here: http://www.carlosag.net/tools/excelxmlwriter/或者,如果您不想在网络服务器上安装 Microsoft Office,我建议使用CarlosAg.ExcelXmlWriter ,可在此处找到: http : //www.carlosag.net/tools/excelxmlwriter/

code sample for ExcelXmlWriter: ExcelXmlWriter 的代码示例:

using CarlosAg.ExcelXmlWriter;

class TestApp {
    static void Main(string[] args) {
        Workbook book = new Workbook();
        Worksheet sheet = book.Worksheets.Add("Sample");
        WorksheetRow row =  sheet.Table.Rows.Add();
        row.Cells.Add("Hello World");
        book.Save(@"c:\test.xls");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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