Actually, it is searched from the Internet... (learning ......)
// Export EXCEL from Dataset
Public void createexcel (Dataset ds, string typeid, string filename)
{
Httpresponse resp;
Resp = page. response;
Resp. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Resp. appendheader ("content-disposition", "attachment; filename =" + filename );
String colheaders = "", ls_item = "";
Int I = 0;
// Define the table object and row object, and use dataset to initialize the value.
Datatable dt = Ds. Tables [0];
Datarow [] myrow = DT. Select ("");
// When typeid = "1", the exported documents are in Excel format; When typeid = "2", the exported documents are in XML format.
If (typeid = "1 ")
{
// Obtain the titles of each column in the data table, separated by \ t, and a carriage return is added after the title of the last column.
For (I = 0; I <dt. Columns. Count; I ++)
{
If (I = dt. Columns. Count-1)
{
Colheaders + = DT. Columns [I]. Caption. tostring () + "\ n ";
}
Else
{
Colheaders + = DT. Columns [I]. Caption. tostring () + "\ t ";
}
}
// Write the obtained data to the HTTP output stream
Resp. Write (colHeaders );
// Process data row by row
Foreach (DataRow row in myRow)
{
// In the current row, data is obtained one by one, separated by \ t, and carriage return \ n is added at the end
For (I = 0; I <DT. Columns. Count; I ++)
{
If (I = DT. Columns. Count-1)
{
Ls_item + = row [I]. tostring () + "\ n ";
}
Else
{
Ls_item + = row [I]. tostring () + "\ t ";
}
}
// Write the data in the current row to the HTTP output stream, and leave ls_item empty for downstream data
Resp. Write (ls_item );
Ls_item = "";
}
}
Else
{
If (typeid = "2 ")
{
// Export XML data directly from dataset and write it to the HTTP output stream
Resp. Write (Ds. getxml ());
}
}
// Write the data in the buffer to the HTTP header document
Resp. End ();
}