Export data from the GridView to Excel and Add rows

Source: Internet
Author: User

 

Private void toexcel (string title)
{
Try
{
Encoding encContent = Encoding. GetEncoding ("UTF-8 ");
Int count = GridView1.Columns. Count;
Response. Clear ();
Response. ClearContent ();
Response. ClearHeaders ();
Response. Buffer = true;
Response. Charset = encContent. WebName;
Response. ContentEncoding = encContent;
Response. ContentType = "application/vnd. ms-excel ";
Response. Write (string. Format ("<meta http-equiv = Content-Type content = text/html; charset = {0}>", encContent. WebName ));
String rename = HttpUtility. UrlEncode (title, Encoding. UTF8 );
Response. AppendHeader ("content-disposition", string. Format ("attachment?filename=02.16.xls", rename ));
StringBuilder titleName = new StringBuilder ();
String colspanCount = string. Format ("<table> <tr> <td colspan = \" {0} \ "", count );
TitleName. Append (colspanCount );
TitleName. Append ("style = \" font-family:; font-size: 12.0pt; font-weight: 700 ;");
TitleName. Append ("text-align: center; vertical-align: middle; \"> ");
TitleName. Append (string. Format ("{0} </td> </tr> </table>", title ));
EnableViewState = false;
StringWriter sw = new StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
This. GridView1.RenderControl (htw );
Response. Write (AddExcelHead (title ));
Response. Write (titleName. ToString ());
Response. Write (sw. ToString ());
Response. Write (AddExcelbottom ());
Sw. Close ();
Response. End ();
}
Catch
{

}
}

Protected void btn_toexcel_Click (object sender, EventArgs e)
{
String orderid = "";
If (Request. Params ["id"]! = Null)
{
Orderid = Request. Params ["id"]. ToString ();
}
Toexcel ("order" + orderid + "details ");
}
Public static string AddExcelHead (string title)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("Sb. Append ("Sb. Append ("<! -- [If gte mso 9]> <xml> ");
Sb. Append ("<x: ExcelWorkbook> ");
Sb. Append ("<x: ExcelWorksheets> ");
Sb. Append ("<x: ExcelWorksheet> ");
Sb. Append (string. Format ("<x: Name >{0} </x: Name>", title ));
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 ("Sb. Append ("<body> ");
Return sb. ToString ();
}
Public static string AddExcelbottom ()
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("</body> ");
Sb. Append ("Return sb. ToString ();
}
Public override void VerifyRenderingInServerForm (Control control Control)
{

}

 

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.