The table is output as an Excel file stream. InstanceCodeAs follows:
Code
Public Static VoidDaochutalbe (StringTableinnerhtml,String Filename)
{
Stringwriter SW= New Stringwriter ();
Sw. writeline (tableinnerhtml );
Sw. Close ();
System. Web. httpcontext. Current. response. addheader ( " Content-Disposition " , " Attachment; filename = " + Filename + " . Xls " );
System. Web. httpcontext. Current. response. contenttype= "Application/MS-Excel" ;
System. Web. httpcontext. Current. response. contentencoding= System. Text. encoding. utf7;
System. Web. httpcontext. Current. response. Write (SW );
System. Web. httpcontext. Current. response. End ();
}
In essence, this method is not a standard Excel format. However, you can save an HTML file as an Excel file and then open it in Excel.
After opening the Excel file, you will find that the exported Excel file has no table lines, which are white and hard to see.
By analyzing the Excel format code, I triedProgramAdd the Excel header code with a table line, and export the excel with the table line smoothly.
The added code is as follows:
Code
/// <Summary>
/// Excel Header
/// </Summary>
/// <Returns> </returns>
Public Static String addexcelhead ()
{< br> stringbuilder Sb = New Stringbuilder ();
SB. append ("<HTML xmlns: x = \"URN: Schemas-Microsoft-COM: Office: Excel \">" );
SB. append ("<Head>" );
SB. append ("<! -- [If gte mso 9]> <XML>" );
SB. append ("<X: excelworkbook>" );
SB. append ("<X: excelworksheets>" );
SB. append ("<X: excelworksheet>" );
SB. append ("<X: Name> </X: Name>" );
SB. append ("<X: worksheetoptions>" );
SB. append ("<X: Print>" );
SB. append ("<X: validprinterinfo/>" );
SB. append ("</X: Print>" );
SB. append ("</X: worksheetoptions>" );
SB. append ("</X: excelworksheet>" );
SB. append ("</X: excelworksheets>" );
SB. append ("</X: excelworkbook>" );
SB. append ("</XML>" );
SB. append ("<! [Endif] -->" );
SB. append ("</Head>" );
SB. append ("<Body>" );
Return SB. tostring ();
}
Code
/// <Summary>
/// Excel tail
/// </Summary>
/// <Returns> </returns>
Public Static String Addexcelbottom ()
{
Stringbuilder sb= New Stringbuilder ();
SB. append ("</Body>" );
SB. append ("</Html>" );
Return Sb. tostring ();
}