您的位置:首页 > 其它

.net导出Excel

2015-06-24 21:31 344 查看
protected void btnPrint_Click(object sender, EventArgs e)
{
DataTable dt = DbHelperSQL.GetTable("select b.ProNumber as 产品编号,b.Name as 产品名称,b.kucun as 剩余库存,b.Price as 产品价格,a.buynum as 购买数量,(b.price*a.buynum) as 合计金额 from shopcar as a,Product as b where a.Proid=b.id and a.userid=" + Session["UserID"] + "");
if (dt.Rows.Count == 0)
{
MsgBox.Show(this,"没有数据,导出失败");
}
else
{
//导出Excel
ExcelImport(dt, "购物车"+DateTime.Now.ToString("yyyyMMddHHmm"));
}
}

protected void ExcelImport(DataTable dt, string ExportFileName)
{
StringWriter sw = GetStringWriter(dt);
//当前编码
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//把输出的文件名进行编码
string fileName = HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8);
//文件名
string str = "attachment;filename=" + fileName + ".xls";
//把文件头输出,此文件头激活文件下载框
HttpContext.Current.Response.AppendHeader("Content-Disposition", str);//http报头文件
HttpContext.Current.Response.ContentType = "application/ms-excel";
this.Page.EnableViewState = false;
Response.Write(sw);
Response.End();
}
private StringWriter GetStringWriter(DataTable dt)
{
StringWriter sw = new StringWriter();
//读列名
foreach (DataColumn dc in dt.Columns)
sw.Write(dc.ColumnName + "\t");
//读列值
//重新的一行
sw.Write(sw.NewLine);
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dr[i].ToString() + "\t");
}
sw.Write(sw.NewLine);
}
}
sw.Close();

return sw;
}


导出DataTable

对于大量的数据,导出Excel的方式。
上篇博客介绍了导出Excel一种方式:通过DataTable
这篇博客,介绍另一中导出Excel方式-通过DataGrid.

导出Excel的过程:
虽然这次是通过DataGrid导出Excel,但是不是从界面上DataGrid控件导出Excel。因为这次实际项目中的需求是把查询到的信息导出Excel,界面上的DataGrid控件只显示一部分,也就是正如上篇博客中提到控件实现了分页,所以不可以直接从界面控件导出。
但是实现的过程一样,只不过,这次DataGrid是动态生成的。

此导出过程用到StringWriter类(将文本信息写入字符串),HtmlTextWriter类:将标记字符和文本写入到 ASP.NET 服务器控件输出流。(命名空间:System.Web.UI)
Control.RenderControl (HtmlTextWriter) ——将服务器控件的内容输出到所提供的 HtmlTextWriter 对象中。
然后respose输出StringWriter对象。

/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dt"></param>
/// <param name="ExportFileName"></param>
protected void ExcelExport(DataTable dt, string ExportFileName)
{
DataGrid dgExcel = new DataGrid();
dgExcel.DataSource = dt;
dgExcel.DataBind();

HttpContext.Current.Response.Charset = "GB2312";
string fileName = HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8);
string str="attachment;filename="+fileName+".xls";
HttpContext .Current .Response .ContentEncoding =System.Text.Encoding .UTF7;
HttpContext.Current.Response .ContentType ="application/ms-excel";
HttpContext .Current .Response .AppendHeader ("content-disposition",str);

StringWriter sw = new StringWriter();
HtmlTextWriter htmTextWriter = new HtmlTextWriter(sw);
dgExcel .RenderControl(htmTextWriter );
Response .Write(sw);
Response .End();

}


/// <summary>
/// 导出excel按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExport_Click(object sender, EventArgs e)
{
DataTable dt = new SelectClassRoomManager().SelectClassRoomByTypeBuildNo(ddlClassType.SelectedValue, ddlBuildingID.SelectedValue);
if (dt.Rows.Count == 0)
Response.Write("<script>alert('没有数据,没有必要导出啊哈');</script>");
else
{

//导出excel
ExcelExport(dt, "教室");
}
}


其中用DataGrid导出Excel的时候,其导出的文件名编码:

string fileName = HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8);

其导出的当前文件编码:

HttpContext .Current .Response .ContentEncoding =System.Text.Encoding .UTF7;

但是这两种导出的Excel的文件不同,内容是一样的,但是其格式不同,使用上篇博客DataTable导出Excel,是真正的excel,而使用DataGrid导出的Excel,貌似有点类似记事本。还是贴的图,大家看看吧。

DataTable导出的Excel 如下:



原博地址
http://blog.csdn.net/yuebinghaoyuan/article/details/6703044
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: