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