Public void outputexcel (dataview DV, string filename)
{
GC. Collect ();
Excel. Application Excel; // = new application ();
Int rowindex = 1;
Int colindex = 0;
Excel. _ workbook xbk;
Excel. _ worksheet XST;
Excel = new excel. applicationclass ();
Xbk = excel. workbooks. Add (true );
XST = (Excel. _ worksheet) xbk. activesheet;
//
// Obtain the title
//
Foreach (datacolumn Col in DV. Table. columns)
{
Colindex ++;
Excel. cells [1, colindex] = col. columnname;
XST. get_range (Excel. cells [1, colindex], Excel. cells [1, colindex]). horizontalalignment = excel. xlvalign. xlvaligncenter; // set the title format to center and align
}
//
// Obtain the data in the table
//
Foreach (datarowview row in DV)
{
Rowindex ++;
Colindex = 0;
Foreach (datacolumn Col in DV. Table. columns)
{
Colindex ++;
Excel. cells [rowindex, colindex] = row [col. columnname]. tostring ();
}
}
//
// Set the background color and field width of the title of the entire report.
//
XST. get_range (Excel. cells [1, 1], Excel. cells [1, colindex]). Font. Bold = true;
XST. get_range (Excel. cells [1, 1], Excel. cells [1, colindex]). interior. color = system. drawing. colortranslator. toole (system. drawing. colortranslator. fromhtml ("#00cc66 "));
XST. get_range (Excel. cells [1, 1], Excel. cells [1, colindex]). Columns. autofit ();
//
// Draw a border
////
// Display Results
//
Excel. Visible = false;
Xbk. savecopyas (filename );
Xbk. Close (false, null, null );
Excel. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (xbk );
System. runtime. interopservices. Marshal. releasecomobject (Excel );
System. runtime. interopservices. Marshal. releasecomobject (XST );
Xbk = NULL;
Excel = NULL;
XST = NULL;
GC. Collect ();
System. Io. fileinfo file = new system. Io. fileinfo (filename );
Response. Clear ();
Response. charset = "gb2312 ";
Response. contentencoding = system. Text. encoding. utf8;
// Add the header information and specify the default file name for the "download/Save as" dialog box
Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode (file. Name ));
// Add header information and specify the file size so that the browser can display the download progress.
Response. addheader ("Content-Length", file. length. tostring ());
// Specify a stream that cannot be read by the client and must be downloaded.
Response. contenttype = "application/MS-excel ";
// Send the file stream to the client
Response. writefile (file. fullname );
// Stop page execution
Response. End ();
}