Asp.net export an Excel report (solving the problem of no grid)

Source: Internet
Author: User
As unit activities increase 4s stores The Excel or Word document export function is particularly important when you want to view the event registration requirements. This greatly reduces the editing time and intensity.
Today, I spent some time writing the Excel export function for major registered brands. The principle is very simple. In fact, I want to output the registration list to the page. The difference is that when I output HTML text, I add something to mark the HTML mark in the head.
Code As follows:

1   String Strexportfilename =   New Syscar. BLL. brandcategory (). GetModel (convert. toint32 ( This . Rbljoinbrand. selectedvalue). Name;
2 Int Actid = Convert. toint32 (request. querystring [ " Actid " ]);
3 Syscar. BLL. Apply apply =   New Bll. Apply ();
4
5 List < Syscar. model. Apply > Appexcel = Apply. datatabletolist (apply. getlist ( " (Activityid = "   + Actid +   " And patindex ('% | "   +   This . Rbljoinbrand. selectedvalue +   " | % ', Brandcollection)> 0) " ). Tables [ 0 ]);
6
7 Response. contenttype =   " Application/vnd. MS-Excel " ;
8 Response. addheader ( " Content-Disposition " , " Inline; filename = "
9 + Httputility. urlencode (strexportfilename +   " _Name.xls " , Encoding. utf8 ));
10
11
12 // If the output is word, modify it to the following code:
13 // Response. contenttype = "application/MS-word"
14 // Response. addheader ("content-disposition", "inline?filename=test.doc ")
15
16 Stringbuilder sb =   New Stringbuilder ();
17 System. Io. stringwriter SW =   New System. Io. stringwriter (SB );
18 System. Web. UI. htmltextwriter HW =   New System. Web. UI. htmltextwriter (SW );
19 SB. append ( " <HTML xmlns: x = urn: Schemas-Microsoft-com: Office: Excel> "   + Strexportfilename +   " </X: Name> <X: worksheetoptions> <X: Print> <X: validprinterinfo/> </X: Print> </X: worksheetoptions> </X: excelworksheet> </X: excelworksheets> </X: excelworkbook> </XML> <! [Endif] --> <meta http-equiv = Content-Type content = \ " Text / HTML; charset = UTF - 8 \ " > " );
20 SB. append ( " <Table> <tr> <TD colspan = 4> "   + Strexportfilename +   " <Font color = 'red'> "   + Appexcel. Count +   " (Person) </font> </TD> </tr> <TD> NO. </TD> <TD> name </TD> <TD> gender </TD> <TD> contact number </TD> </tr> " );
21 For ( Int I =   0 ; I < Appexcel. Count; I ++ )
22 {
23 String Strsex =   " Male " ;
24 If (Convert. toint32 (appexcel [I]. Sex) =   2 )
25 {
26 Strsex =   " Female " ;
27 }
28 SB. append ( " <Tr> <TD> "   + Appexcel [I]. applyid +   " </TD> <TD> "   + Appexcel [I]. Name +   " </TD> <TD> "   + Strsex +   " </TD> <TD> "   + Appexcel [I]. Mobile +   " </TD> </tr> " );
29
30 }
31 SB. append ( " </Table> " );
32 SB. append ( " </Body> " );
33
34
35 Response. Write (sb. tostring ());
36 Response. End ();

Key points:

1
2 <! -- Solved the problem of no table line export in Excel -->
3 < HTML xmlns: x = URN: Schemas - Microsoft - COM: Office: Excel >
4 < Head >
5 <! -- [ If GTE MSO 9 ] >
6 < XML >
7 < X: excelworkbook >
8 < X: excelworksheets > < X: excelworksheet >
9 < X: Name > Table Name </ X: Name >
10 < X: worksheetoptions >
11 < X: Print > < X: validprinterinfo /> </ X: Print >
12 </ X: worksheetoptions >
13 </ X: excelworksheet > </ X: excelworksheets >
14 </ X: excelworkbook >
15 </ XML >
16 <! [Endif] -->
17
18
19
20 <! -- Solve the Problem of exporting Excel Chinese garbled characters -->
21 < Meta HTTP - Equiv = Content - Type content = \ " Text/html; charset = UTF-8 \ " >
22

 

 

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.