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)
{
}