A row and a column. Here is a prompt:
Using Microsoft. Office. Core;
Using Microsoft. Office. Interop. Excel;
...
...
Microsoft. Office. Interop. Excel. Application myExcel = new Microsoft. Office. Interop. Excel. Application ();
MyExcel. Application. Workbooks. Add (true );
MyExcel. Visible = true;
..
..
For (int row = 0; row <= this. ds. Tables [0]. Rows. Count-1; row ++)
{
For (int col = 0; col <= this. ds. Tables [0]. Columns. Count-1; col ++)
{
MyExcel. Cells [row + 4, col + 1] = "'" + ds. Tables [0]. Rows [row]. ItemArray [col]. ToString ();
}
}
This is to bind the data on the able directly to the datagird, and then download the data from it to the excel file.
Private void btnDownLoad_Click (object sender, System. EventArgs e)
{
If (this. dg. Items. Count> 0)
{
Response. Clear ();
Response. Buffer = true;
Response. Charset = "GB2312 ";
Response. AppendHeader ("Content-Disposition", "attachment?filename=filename.xls ");
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312"); // set the output stream to simplified Chinese
Response. ContentType = "application/ms-excel"; // set the output file type to an excel file.
This. EnableViewState = false;
CultureInfo myCItrad = new CultureInfo ("ZH-CN", true );
HtmlGenericControl div = new HtmlGenericControl ();
System. IO. StringWriter oStringWriter = new System. IO. StringWriter (myCItrad );
Div. InnerHtml = oStringWriter. ToString ();
System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );
This. dgRoleMenu. RenderControl (oHtmlTextWriter );
Div. InnerHtml = oStringWriter. ToString ();
Response. Write (div. InnerText );
Response. End ();
} Else return;
}