ASP. NET use MyXls to export Excel Data

Source: Internet
Author: User

If you want to export Excel data without calling the Excel component like me, I would like to recommend MyXls to you. MyXls uses C # Open-source projects and can be applied to asp.net or.. net application. It directly generates excel documents in binary format based on Microsoft's Excle file format (BIFF) and supports Excel versions 97-2007. This means that you can output data stored in the database in excle format without installing office on the server. This is useful for many projects.

The homepage of MyXls is described as follows:

Writes and now Reads Excel files quickly and easily, including formatting. generate Excel files for ASP. NET sites or. NET applications. doesn't require Excel on the server or any licensing $. compatible with Excel versions> = 97.

Its powerful functions are not something anyone can grasp at the moment. Here I will mainly introduce how to use it to export data, I have not used it to read and generate an Excel file, so I will not speak too much. You need to google it again!
The first step is, of course, in MyXls,
Step 2: Add reference org. in2bits. MyXls. dll

Step 3: export data. Here we export a able as data. After the export, the content format is the same as that of the DataTable. The Code is as follows:

[Html]
Private void Output (DataTable dt)
{
Org. in2bits. MyXls. XlsDocument doc = new org. in2bits. MyXls. XlsDocument ();
Doc. fileName = DateTime. now. toString (). replace ("-",""). replace (":",""). replace ("", "") + ". xls "; // excel file name
Org. in2bits. MyXls. Worksheet sheet = doc. Workbook. Worksheets. AddNamed ("sheet1"); // Excel Worksheet name
Org. in2bits. MyXls. Cells = sheet. cells;
Int colnum = dt. Columns. Count; // gets the number of Columns in the DataTable.
 
For (int I = 0; I <colnum; I ++)
{
Cells. Add (1, (I + 1), dt. Columns [I]. Caption. ToString (); // export the DataTable column name
}
For (int I = 0; I <dt. Rows. Count; I ++)
{
For (int j = 0; j <colnum; j ++)
{
Cells. Add (I + 2), (j + 1), dt. Rows [I] [j]. ToString ());
}
}
// Doc. Save (@ "D: \"); // Save it to the specified location
Doc. Send (); // output the prepared excel file to the client.
}
Author: wangqiuyun

Related Article

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.