The problem of exporting data from Asp.net to Excel is already a commonplace. I suddenly used it today. I felt a bit messy, so I encapsulated it and pasted the code for reference only.
Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. Text;
Using System. Web;
Using System. Data;
Namespace AK. T. FileDeal
{
Public class Excel
{
/// <Summary>
/// Export to the Excel worksheet with the tititile header. The datatable field must correspond to the title
/// </Summary>
/// <Param name = "ds"> </param>
/// <Param name = "title"> </param>
Public void CreateExcel (DataTable ds, string [] title, System. Web. UI. Page page)
{
HttpResponse resp;
Resp = page. Response;
Resp. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Resp. AppendHeader ("Content-Disposition", "attachment; filename = Cards _" + DateTime. Now. to1_datestring () + ". xls ");
String ls_item = "";
String colHeaders = "";
DataTable dt = ds;
DataRow [] myRow = dt. Select ("");
For (int I = 0; I <title. Length; I ++)
{
If (I = title. Length-1)
{
ColHeaders + = title [I] + "\ n ";
Continue;
}
ColHeaders + = title [I] + "\ t ";
}
Int count = title. Length;
Foreach (DataRow row in myRow)
{
For (int I = 0; I <count; I ++)
{
If (I = count-1)
{
Ls_item + = row [I]. ToString () + "\ n ";
Continue;
}
Ls_item + = row [I]. ToString () + "\ t ";
}
}
Resp. Write (colHeaders + ls_item );
Resp. End ();
}
/// <Summary>
/// Export to Excel. titile is the excel header, and DtRow is the name of the field in ds.
/// </Summary>
/// <Param name = "ds"> </param>
/// <Param name = "title"> </param>
/// <Param name = "DtRow"> </param>
Public void CreateExcel (DataTable ds, string [] title, string [] DtRow, System. Web. UI. Page page)
{
If (DtRow. Length! = Ds. Rows. Count)
{
Return;
}
HttpResponse resp;
Resp = page. Response;
Resp. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Resp. AppendHeader ("Content-Disposition", "attachment; filename = Cards _" + DateTime. Now. to1_datestring () + ". xls ");
String ls_item = "";
String colHeaders = "";
DataTable dt = ds;
DataRow [] myRow = dt. Select ("");
For (int I = 0; I <title. Length; I ++)
{
If (I = title. Length-1)
{
ColHeaders + = title [I] + "\ n ";
Continue;
}
ColHeaders + = title [I] + "\ t ";
}
Foreach (DataRow row in myRow)
{
For (int I = 0; I <DtRow. Length; I ++)
{
If (I = DtRow. Length-1)
{
Ls_item + = row [DtRow [I]. ToString () + "\ n ";
Continue;
}
Ls_item + = row [DtRow [I]. ToString () + "\ t ";
}
}
Resp. Write (colHeaders + ls_item );
Resp. End ();
}
/// <Summary>
/// Export a single row of data
/// </Summary>
/// <Param name = "title"> </param>
/// <Param name = "Content"> </param>
Public void CreateOnceExcel (string [] title, string [] Content, System. Web. UI. Page)
{
HttpResponse resp;
Resp = page. Response;
Resp. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
Resp. AppendHeader ("Content-Disposition", "attachment; filename = Cards _" + DateTime. Now. to1_datestring () + ". xls ");
String ls_item = "";
String colHeaders = "";
For (int I = 0; I <title. Length; I ++)
{
If (I = title. Length-1)
{
ColHeaders + = title [I] + "\ n ";
Continue;
}
ColHeaders + = title [I] + "\ t ";
}
For (int I = 0; I <Content. Length; I ++)
{
If (I = Content. Length-1)
{
Ls_item + = Content [I]. ToString () + "\ n ";
Continue;
}
Ls_item + = Content [I]. ToString () + "\ t ";
}
Resp. Write (colHeaders + ls_item );
Resp. End ();
}
}
}