1、GridView导出到Excel
//到出Excel
protected void btnExcel_Click(object sender, EventArgs e)
{
ExportGridViewToExcel(Response, GridView1,"MyExcel_Name");
}
//到出excel
public void ExportGridViewToExcel(HttpResponse response, Control gv, String excelFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF-8";
response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", excelFileName));
response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
response.ContentType = "application/ms-excel";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
GridView1.AllowPaging = false;//清除分页
//GridView1.AllowSorting = false; //清除排序
//SetReport();
//控制GridView导出Excel后的样式
this.GridView1.GridLines = GridLines.Both;
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
//GridView1.AllowSorting = true; //恢复排序
GridView1.AllowPaging = true; //恢复排序
//SetReport(); //再次绑定
}
2、GridView导出到Xml
protected void ConvertToXmlButton_Click(object sender, EventArgs e)
{
string cn, cmd, user_id;
cn = "data source=192.168.1.1;User ID=sa;Password=123;database=database_demo";
cmd = "select * from table_demo";
user_id = Session["User_id"].ToString();//只是显示下载时XML文档的名字,例:123_201107140901.xml
ToXml(cn, cmd, user_id);
}
public void ToXml(string cn, string cmd, string user_id)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
writer.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"");
ds.WriteXml(writer, XmlWriteMode.DiffGram);//这个ds是要导出的DataSet
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/xml";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Session["User_id"].ToString() + "_" + string.Format("{0:yyyyMMdd}", DateTime.Today) + string.Format("{0:00}", DateTime.Now.Hour) + string.Format("{0:00}", DateTime.Now.Minute) + ".xml");
HttpContext.Current.Response.Write(sb.ToString());
HttpContext.Current.Response.End();
}
3、导出到CSV
protected void ConvertToCsv_Click(object sender, EventArgs e)
{
string cn, cmd, user_id;
cn = "data source=198.168.1.1;User ID=sa;Password=123;database=database_demo";
cmd = "select * from table_demo";
user_id = Session["User_id"].ToString();
ToCsv(cn, cmd, user_id);
}
public void ToCsv(string cnstring, string cmdstring, string user_id)
{
string csv_FileName = user_id + "_" + string.Format("{0:yyyyMMdd}", DateTime.Today) + string.Format("{0:00}", DateTime.Now.Hour) + string.Format("{0:00}", DateTime.Now.Minute);
string fileContent = String.Empty;
foreach (DataRow dr in ds.Tables[0].Rows)//ds是要导出到CSV文件的DataSet
{
foreach (DataColumn dc in ds.Tables[0].Columns)
{
fileContent += dr[dc].ToString() + ",";
}
fileContent += "\r\n";
}
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(csv_FileName + ".csv"));
Response.ContentType = "csv/plain";
Response.ContentEncoding = Encoding.GetEncoding("BIG5");
Response.Write(fileContent);
Response.End();
}
4、导出到TXT
protected void Button_txtDl_Click(object sender, EventArgs e)
{
string cn, cmd, user_id;
cn = "data source=192.168.1.1;User ID=sa;Password=123;database=database_demo";
cmd = "select * from table_demo";
user_id = Session["User_id"].ToString();
Totxt(cn, cmd, user_id);
}
public void Totxt(string cnstring, string cmdstring, string user_id)
{
string csv_FileName = user_id + "_" + string.Format("{0:yyyyMMdd}", DateTime.Today) + string.Format("{0:00}", DateTime.Now.Hour) + string.Format("{0:00}", DateTime.Now.Minute);
string fileContent = String.Empty;
foreach (DataRow dr in ds.Tables[0].Rows)//ds是DataSet,即要导出的数据
{
foreach (DataColumn dc in ds.Tables[0].Columns)
{
fileContent += dr[dc].ToString() + ",";
}
fileContent += "\r\n";
}
Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(csv_FileName + ".txt"));
Response.ContentType = "txt/plain";
Response.ContentEncoding = Encoding.GetEncoding("BIG5");
Response.Write(fileContent);
Response.End();
}