標籤:style http color os io for ar 資料 html
1、從 資料庫 中 匯出 excel 表格
定義 一個 gridview1
protected void Button1_Click(object sender, EventArgs e) //倒出資料
{
if (GridView1.Rows.Count == 0)
{
return;
}
else
{
GridView1.AllowPaging = false;//先將資料分頁取消,才能全部匯出資料
bind();
Export("application/ms-excel", "FileName.xls", GridView1);
GridView1.AllowPaging = true;//綁定完資料後,才重新綁定資料
bind();
}
public void Export(string fileType, string fileName, GridView gd)//資料匯出函數
{
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
HttpContext.Current.Response.ContentType = fileType;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF7));
gd.Page.EnableViewState = false;
System.IO.StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gd.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
}
//如果沒有下面方法會報錯類型“GridView”的控制項“GridView1”必須放在具有 runat=server 的表單標記內
public override void VerifyRenderingInServerForm(Control control)
{
}
2、從excel 匯入到 資料庫 中
從SQL Server 2005 中 匯入 匯出 excel 表格