自己用到的导出到Excel写到这里备忘
private
void
OutExcel()
... {
BuildGridView#regionBuildGridView
//string[]HeadText={"业务类型","产品","客户名称","地址","公司电话","联系人","手机","报备时间","到期天数"};
string[]HeadText=...{"产品类别","客户名称","地址","公司电话","联系人","手机","报备时间","到期天数"};
DataTablenew_DTe=newDataTable();
for(inti=0;i<HeadText.Length;i++)
...{
new_DTe.Columns.Add(HeadText[i],typeof(string));
}
DataGridoutView=newDataGrid();
DataViewDte=this.GetData();
LinkmanBSlinkBs=newLinkmanBS();
foreach(DataRowdrinDte.Table.Rows)
...{
DataRowDw=new_DTe.NewRow();
Dw["产品类别"]=dr["Name"].ToString();
//Dw["产品"]=dr["ProdetName"].ToString();
Dw["客户名称"]=dr["ClientName"].ToString();
Dw["地址"]=dr["Address"].ToString();
Dw["公司电话"]=dr["Tel"].ToString();
DataTablel_DTe=linkBs.GetDataOne(Convert.ToInt32(dr["ClientID"]),Convert.ToInt32(dr["ProductID"]));
if(l_DTe.Rows.Count>0)
...{
Dw["联系人"]=l_DTe.Rows[0]["Name"].ToString();
Dw["手机"]=l_DTe.Rows[0]["MTel"].ToString();
}
else
...{
Dw["联系人"]=string.Empty;
Dw["手机"]=string.Empty;
}
Dw["报备时间"]=dr["ProtectTime"].ToString();
Dw["到期天数"]=dr["DaoQiDay"].ToString();
new_DTe.Rows.Add(Dw);
}
outView.AutoGenerateColumns=true;
//for(inti=0;i<HeadText.Length;i++)
//{
//BoundColumnfield=newBoundColumn();
//field.HeaderText=HeadText[i];
//field.DataField=Data[i];
//outView.Columns.Add(field);
//}
outView.DataSource=new_DTe;
outView.DataBind();
#endregion
OutExcel#regionOutExcel
stringstyle=@"<style>.text{mso-number-format:@;}</script>";
Response.ClearContent();
Response.AddHeader("content-disposition","attachment;filename="+System.DateTime.Now.ToString("yyyyMMddhhmmss")+".xls");
Response.ContentType="application/excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
StringWritersw=newStringWriter();
HtmlTextWriterhtw=newHtmlTextWriter(sw);
outView.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
#endregion
}
... {
BuildGridView#regionBuildGridView
//string[]HeadText={"业务类型","产品","客户名称","地址","公司电话","联系人","手机","报备时间","到期天数"};
string[]HeadText=...{"产品类别","客户名称","地址","公司电话","联系人","手机","报备时间","到期天数"};
DataTablenew_DTe=newDataTable();
for(inti=0;i<HeadText.Length;i++)
...{
new_DTe.Columns.Add(HeadText[i],typeof(string));
}
DataGridoutView=newDataGrid();
DataViewDte=this.GetData();
LinkmanBSlinkBs=newLinkmanBS();
foreach(DataRowdrinDte.Table.Rows)
...{
DataRowDw=new_DTe.NewRow();
Dw["产品类别"]=dr["Name"].ToString();
//Dw["产品"]=dr["ProdetName"].ToString();
Dw["客户名称"]=dr["ClientName"].ToString();
Dw["地址"]=dr["Address"].ToString();
Dw["公司电话"]=dr["Tel"].ToString();
DataTablel_DTe=linkBs.GetDataOne(Convert.ToInt32(dr["ClientID"]),Convert.ToInt32(dr["ProductID"]));
if(l_DTe.Rows.Count>0)
...{
Dw["联系人"]=l_DTe.Rows[0]["Name"].ToString();
Dw["手机"]=l_DTe.Rows[0]["MTel"].ToString();
}
else
...{
Dw["联系人"]=string.Empty;
Dw["手机"]=string.Empty;
}
Dw["报备时间"]=dr["ProtectTime"].ToString();
Dw["到期天数"]=dr["DaoQiDay"].ToString();
new_DTe.Rows.Add(Dw);
}
outView.AutoGenerateColumns=true;
//for(inti=0;i<HeadText.Length;i++)
//{
//BoundColumnfield=newBoundColumn();
//field.HeaderText=HeadText[i];
//field.DataField=Data[i];
//outView.Columns.Add(field);
//}
outView.DataSource=new_DTe;
outView.DataBind();
#endregion
OutExcel#regionOutExcel
stringstyle=@"<style>.text{mso-number-format:@;}</script>";
Response.ClearContent();
Response.AddHeader("content-disposition","attachment;filename="+System.DateTime.Now.ToString("yyyyMMddhhmmss")+".xls");
Response.ContentType="application/excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
StringWritersw=newStringWriter();
HtmlTextWriterhtw=newHtmlTextWriter(sw);
outView.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
#endregion
}