Use epplus to export Excel files

Source: Internet
Author: User

The younger brother has just taken up his position and often needs to export excel at work. However, he feels that there are two common export methods (one is through the gridview, the other is through the Com group of Microsoft
Is not ideal, the former exports too low flexibility, and the latter often appears that the process cannot be recycled, so I always want to find. NET platform third-party Excel component, so I found it online
Epplus.

Epplus is an open source component that can read and write Excel 2007/2010 files in the XLSX file format. its official website is http://epplus.codeplex.com /.
The only pity is that I did not find any Chinese information about this component on the Internet. I have never been clear why, is there any major defect in this component? Or is it still not widely used in China? I read the introduction from the official website.
After Shao, I felt that this component was quite good and decided to use it first. (If this component has any major defects, ask people familiar with it to tell me, saving me the time to study it .)

The following is a simple example. You can use the epplus component to export an Excel file (add an epplus reference first ). I put a button on the page. The onclick event processing function is as follows:

// Create a worksheet
Officeopenxml. excelpackage Ep = new officeopenxml. excelpackage ();
Officeopenxml. excelworkbook WB = ep. workbook;
Officeopenxml. excelworksheet Ws = WB. worksheets. Add ("my worksheet ");

// Configuration file attributes
WB. properties. Category = "category ";
WB. properties. Author = "author ";
WB. properties. Comments = "Remarks ";
WB. properties. Company = "company ";
WB. properties. KEYWORDS = "keyword ";
WB. properties. Manager = "manager ";
WB. properties. Status = "content status ";
WB. properties. Subject = "topic ";
WB. properties. Title = "title ";
WB. properties. lastmodifiedby = "Last warranty ";

// Write data
WS. cells [1, 1]. value = "hello ";
WS. cells ["B1"]. value = "world ";
WS. cells [3, 3, 3, 5]. Merge = true;
WS. cells [3, 3]. value = "cells [3, 3, 3, 5 ";
WS. cells ["A4: D5"]. Merge = true;
WS. cells ["A4"]. value = "cells [\" A4: D5 \ "] merge ";

// Write to the client (download)
Response. Clear ();
Response. addheader ("content-disposition", "attachment; filename=fileflow.xlsx ");
Response. contenttype = "application/vnd. openxmlformats-officedocument.spreadsheetml.sheet ";
Response. binarywrite (Ep. getasbytearray ());
// Ep. saveas (response. outputstream); method 2
Response. Flush ();
Response. End ();

Exported Excel file attributes:

Exported Excel file content:

I tested it. This component can also run on 2008 64-bit machines. If you are interested, you can try it. As for learning materials, it seems that only the official website has it, if this plug-in is not defective, I also hope that interested comrades can study it. This is also the purpose of my writing this article. I hope I can learn more Chinese materials in the future, so I don't have to worry about reading English.

Reference to: http://www.cnblogs.com/hecool/archive/2011/09/18/2180615.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.