现在的位置: 首页 > 综合 > 正文

导出excel的两种方法

2013年12月06日 ⁄ 综合 ⁄ 共 1068字 ⁄ 字号 评论关闭
 

1,直接保存成excel

page_load() 

{

Response.Write(this.GetExcelHTML());
   Response.ContentType="Application/vnd.ms-excel";
   Response.AddHeader("Content-Disposition","attachment;filename=ReqForQuote.xls");
   Response.End();

}

private string GetExcelHTML()

{

string res=“<table><tr ><th colspan=5>aaabbb</th></tr><tr><th>aaa</th>...

<tr><td>....<td></tr><table>“;

return res;

}

 

 

2,用datagrid 输出

page_load() 

{

 entitiesGrid.DataSource = new  StockBR().GetByWhere(null,_exportPage.FilterExpression, _exportPage.SortExpression).Stock .DefaultView;
    entitiesGrid.DataBind();

    //Set the content type to Excel.
    Response.ContentType = "application/vnd.ms-excel";
    // Remove the charset from the Content-Type header.
    Response.Charset = "";
    // Turn off the view state.
    this.EnableViewState = false;
     
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
 
    // Get the HTML for the control.
    entitiesGrid.RenderControl(htmlTextWriter);
    //Write the HTML back to the browser.
    Response.Write(stringWriter.ToString());
    // End the response.
    Response.End();

}

 

抱歉!评论已关闭.