/// 该方法实现将数据导入到Excel文件中,其中的DataTable dt就是你需要将数据写入到Excel中的数据;
/// </summary>
/// <param name="dt">导出的数据源</param>
/// <param name="w">文件流</param>
public void ExportExcel(DataTable dt, System.IO.StreamWriter w)
{
//HttpResponse resp;
//resp = Page.Response;
//resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
// resp.Write(colHeaders);
w.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
w.Write(ls_item);
ls_item = "";
}
//resp.End();
w.Flush();
w.Close();
}
二、直接从Gridview里导出
这种导出是所见既所得的方式,就是显示在gridview里是什么样导出就是什么样了,这个导出就好看多了,谢谢~!@
this.Panel1.Visible = false;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = "Task"+System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString()+".xls";
Response.AppendHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode(filename));
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter); //读取数据了
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
this.Panel1.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = "Task"+System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString()+".xls";
Response.AppendHeader("Content-Disposition", "attachment;filename="+System.Web.HttpUtility.UrlEncode(filename));
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter); //读取数据了
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
this.Panel1.Visible = true;
由于ReaderControl方法会重调客户端的一个方法验证,如果不重载的话会报错
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
{
//base.VerifyRenderingInServerForm(control);
}