Asp.net (C #) Export excel with table lines

Source: Internet
Author: User

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 ();
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.