在view页面中放置两个按钮,分别设置对应的获取方法
<input type="button" value="导出Excel方式1" onclick="exportToExcelA()" />
<input type="button" value="导出Excel方式2" onclick="exportToExcelB()" />
<script type="text/javascript">
function exportToExcelA() {
window.open("/Home/ExportDataByHtml?curDate=" + new Date(),"导出Excel",null);
}
function exportToExcelB() {
window.open("/Home/ExportDataByGridView?curDate=" + new Date(), "导出Excel", null);
}
</script>
在控制器中创建一个测试List,实际应用中可以改为数据库查询结果
List<TestUser> dataList = createList(10);
private static List<TestUser> createList(int num)
{
List<TestUser> list = new List<TestUser> { };
for (int i = 0; i < num; i++)
{
TestUser user = new TestUser { UID = i + 1, UName = "Name" + (i + 1).ToString(), ULastLogin = DateTime.Now };
list.Add(user);
}
return list;
}
在控制器中设置纯HTML和借助WebForm的GridView控件生成HTML方法
public void ExportDataByHtml()
{
StringBuilder sHtml = new StringBuilder(string.Empty);
//下面这句解决中文乱码
sHtml.Append("<meta http-equiv='content-type' content='application/ms-excel; charset=utf-8' />");
//表头
sHtml.Append("<table border=1 bordercolor='blue'>");
sHtml.Append("<tr height=50><td bgcolor='yellow'><b>编号</b></td><td bgcolor='yellow'><b>姓名</b></td><td bgcolor='yellow'><b>最后登录</b></td></tr>");
//循环读取数据集合
foreach (var item in dataList)
{
sHtml.Append("<tr height=40><td>"+item.UID+"</td><td>"+item.UName+"</td><td>"+item.ULastLogin+"</td></tr>");
}
//表尾
sHtml.Append("</table>");
string filename = "Report"+DateTime.Now.ToString();
//调用输出Excel表的方法
ExportToExcel(filename + ".xls", sHtml.ToString());
}
public void ExportDataByGridView()
{
//创建一个GridView实例
System.Web.UI.WebControls.GridView grid=new System.Web.UI.WebControls.GridView();
//数据绑定,用select查询设定表头标题
grid.DataSource = dataList.Select(u => new { 编号 = u.UID, 姓名 = u.UName, 最后登录 = u.ULastLogin });
grid.DataBind();
//生成Html内容
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
grid.RenderControl(htw);
string filename = "Report" + DateTime.Now.ToString();
//调用输出Excel表的方法
ExportToExcel(filename + ".xls", sw.ToString());
}
public void ExportToExcel(string FileName, string ExcelContent)
{
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.HttpContext.Current.Response.Output.Write(ExcelContent.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
在纯HTML方式中,如果对HTML标记熟悉,可以自由设定表格线条颜色粗细、行高(tr height=50,对应生成的电子表格行高就是25)、列宽、字体样式、对齐方式等,这里要注意不能使用css样式,输出时不能识别,要用最原始的HTML标记。
借助GridView控件,实际还是生成对应的HTML数据页,好处是数据详情部分不用再写Foreach,坏处是GridView的模板设置要相对麻烦点,没有直接写HTML标记方便。
这两种方法都不用NuGet下载第三方控件,直接就能用
本文介绍了如何在view页面中使用JavaScript和控制器操作,通过两种方式实现导出Excel功能:纯HTML手动构建HTML表格和利用GridView控件自动生成。两种方法均无需第三方控件,但HTML手动方式更灵活,GridView则简化了数据绑定过程。
148

被折叠的 条评论
为什么被折叠?



