网页html格式导出Excel.xls
1.创建excel方法
/// <summary>
/// 创建Excel表格
/// </summary>
/// <param name="dt">数据流</param>
/// <param name="FileName">文件名称</param>
public static void CreateExcel(DataTable dt, string FileName)
{
StringBuilder strb = new StringBuilder(); strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\""); strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\""); strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">"); strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>"); strb.Append(" <style>"); strb.Append(".xl26"); strb.Append(" {mso-style-parent:style0;"); strb.Append(" font-family:\"Times New Roman\", serif;"); strb.Append(" mso-font-charset:0;"); strb.Append(" mso-number-format:\"@\";}"); strb.Append(" </style>"); strb.Append(" <xml>"); strb.Append(" <x:ExcelWorkbook>"); strb.Append(" <x:ExcelWorksheets>"); strb.Append(" <x:ExcelWorksheet>"); strb.Append(" <x:Name>Sheet1 </x:Name>"); strb.Append(" <x:WorksheetOptions>"); strb.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>"); strb.Append(" <x:Selected/>"); strb.Append(" <x:Panes>"); strb.Append(" <x:Pane>"); strb.Append(" <x:Number>3 </x:Number>"); strb.Append(" <x:ActiveCol>1 </x:ActiveCol>"); strb.Append(" </x:Pane>"); strb.Append(" </x:Panes>"); strb.Append(" <x:ProtectContents>False </x:ProtectContents>"); strb.Append(" <x:ProtectObjects>False </x:ProtectObjects>"); strb.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>"); strb.Append(" </x:WorksheetOptions>"); strb.Append(" </x:ExcelWorksheet>"); strb.Append(" <x:WindowHeight>6750 </x:WindowHeight>"); strb.Append(" <x:WindowWidth>10620 </x:WindowWidth>"); strb.Append(" <x:WindowTopX>480 </x:WindowTopX>"); strb.Append(" <x:WindowTopY>75 </x:WindowTopY>"); strb.Append(" <x:ProtectStructure>False </x:ProtectStructure>"); strb.Append(" <x:ProtectWindows>False </x:ProtectWindows>"); strb.Append(" </x:ExcelWorkbook>"); strb.Append(" </xml>"); strb.Append(""); strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'> <tr>"); //写列标题 int columncount = dt.Columns.Count; for (int columi = ; columi < columncount; columi++)
{ strb.Append(" <td> <b>" + dt.Columns[columi] + " </b> </td>"); } strb.Append(" </tr>"); //写数据 for (int i = ; i < dt.Rows.Count; i++)
{ strb.Append(" <tr>"); for (int j = ; j < dt.Columns.Count; j++)
{
strb.Append(" <td class='xl26'>" + dt.Rows[i][j].ToString() + " </td>");
} strb.Append(" </tr>");
} strb.Append(" </table>"); strb.Append(" </body> </html>"); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 HttpContext.Current.Response.Write(strb); HttpContext.Current.Response.End(); }
2.List集合转DateTable,根据属性特性[System.ComponentModel.Description("列名称")]指定名称,没有指定名称列则不会导出
#region List集合转DateTable
/// <summary>
/// List集合转DateTable
/// </summary>
/// <typeparam name="T">泛型类</typeparam>
/// <param name="items">参数名</param>
/// <returns></returns>
private DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name); List<PropertyInfo> props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList(); #region MyRegion
//foreach (PropertyInfo prop in props)
//{
// string name = "";
// //获取特性值
// object[] da = prop.GetCustomAttributes(typeof(DescriptionAttribute), false);
// if (da.Length != 0)
// {
// name = (da[0] as DescriptionAttribute).Description;
// Type t = GetCoreType(prop.PropertyType);
// tb.Columns.Add(name, t);
// }
// else
// {
// //name = prop.Name; // props.Remove(prop);
// }
//}
#endregion for (int i = ; i < props.Count; i++)
{
string name = "";
//获取特性值
object[] da = props[i].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (da.Length != )
{
name = (da[] as DescriptionAttribute).Description;
Type t = GetCoreType(props[i].PropertyType);
tb.Columns.Add(name, t);
}
else
{
//name = prop.Name;
props.Remove(props[i]);
i--;
}
} foreach (T item in items)
{
var values = new object[props.Count]; for (int i = ; i < props.Count; i++)
{
values[i] = props[i].GetValue(item, null);
} tb.Rows.Add(values);
} return tb;
}
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}
#endregion
3.创建数据源,调用方法
public void UserWithdrawExcelList()
{
//数据源,根据自己需要来取
List<UserWithdrawQueryViewModel> userList = UserWithdrawBLL.GetUserWithdrawListExcel(paramdic); DataTable dt = ToDataTable<UserWithdrawQueryViewModel>(userList);
#region MyRegion
//DataTable dt = new DataTable();
//dt.Columns.Add("用户名", typeof(String));
//dt.Columns.Add("提现标题", typeof(String));
//dt.Columns.Add("提现总金额", typeof(String));
//dt.Columns.Add("实际到账金额", typeof(String));
//dt.Columns.Add("提现手续费", typeof(String));
//dt.Columns.Add("提现账户", typeof(String));
//dt.Columns.Add("提现状态", typeof(String));
//dt.Columns.Add("创建时间", typeof(DateTime));
//userList.TryForEach(t =>
//{
// dt.Rows.Add(new object[] {
// t.UserName,t.WithTitle,t.WithTotalAmount,t.WithAmount,t.CounterFee,t.BankCode,t.StatusName,t.AddTime
// });
//});
#endregion
DataView dv = dt.DefaultView;//获取表视图
dv.ToTable();//转为表 DataChangeExcel.CreateExcel(dt, DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xls");
}
网页html格式导出Excel.xls的更多相关文章
- 01 UIPath抓取网页数据并导出Excel(非Table表单)
上次转载了一篇<UIPath抓取网页数据并导出Excel>的文章,因为那个导出的是table标签中的数据,所以相对比较简单.现实的网页中,有许多不是通过table标签展示的,那又该如何处理 ...
- 网页表格导入导出Excel
用JS实现网页表格数据导入导出excel. 首先是JS文件中的代码 (function($){ function getRows(target){ var state = $(target).data ...
- java poi导出EXCEL xls文件代码
String _currentPage = request.getParameter("currentPage"); Integer currentPage = 0; if(_cu ...
- XML格式导出Excel
下面介绍一种导出Excel的方法: 此方法不需要在服务器上安装Excel,采用生成xml以excel方式输出到客户端,可能需要客户机安装excel,所以也不会有乱七八糟的权限设定,和莫名其妙的版本问题 ...
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...
- POI导出Excel(xls、xlsx均可以,也支持图片)——(三)
Jar包
- winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
1.通过NUGET管理器下载nopi,在引入命令空间 using System; using System.Collections.Generic; using System.Text; using ...
- asp.net——XML格式导出Excel
下面介绍一种导出Excel的方法: 此方法不需要在服务器上安装Excel,采用生成xml以excel方式输出到客户端,可能需要客户机安装excel,所以也不会有乱七八糟的权限设定,和莫名其妙的版本问题 ...
- NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。
App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> ...
随机推荐
- kibi - join and filter data from multiple Elasticsearch indexes
Kibi extends Kibana 4.6.4 with data intelligence features. The core feature of Kibi is the capabilit ...
- selenium 目录结构解释
common目录 定义了通用的异常类 webdriver目录 android.backberry.chrome.edge.firefox.ie.opera.phantomjs.safa ...
- com.jakewharton:butterknife:7.0.1' 点击无效
需要加上 annotationProcessor 'com.jakewharton:butterknife:7.0.1' dependencies { compile 'com.jakewharton ...
- Sping Cloud项目启动报A component required a bean of type 'com.tianyan.bbc.dao.SecurityUserBaseMapper' that could not be found.
项目构建正常,启动和Debug报以下错误: Error starting ApplicationContext. To display the conditions report re-run you ...
- sc.exe用法详解
sc.exe是帮助开发 WindowsNT 服务的工具,这里我来说说如何使用好sc.exe. 我们打开命令提示符(以管理员身份运行): 输入sc delete ServiceName(服务名) 即可 ...
- 记录Redis使用中遇到的两个问题(原子性及数据完整性)
1.使用Redis作为分布式锁的原子性问题 原方案: ① SETNX $LOCK_BUSI_KEY $REQ_ID ② EXPIRE $LOCK_BUSI_KEY $LOCK_TIME 问题: 使用S ...
- (简单)华为M3青春 CPN-AL10的Usb调试模式在哪里打开的步骤
每次我们使用PC通过数据线连接到安卓手机的时候,如果手机没有开启usb开发者调试模式,PC则没能成功检测到我们的手机,有时候我们使用的一些功能比较强的的应用软件比如以前我们使用的一个应用软件引号精灵, ...
- redis哨兵架构的基础知识及部署和管理
一.前言 1.哨兵的介绍 sentinal,中文名是哨兵 哨兵是redis集群架构中非常重要的一个组件,主要功能如下 ()集群监控,负责监控redis master和slave进程是否正常工作 ()消 ...
- 把JavaScript对象转化成JSON对象
js => jsonvar jsonVar = {key: value}var jsonString = JSON.stringify(jsonVar) //将JS对象转换为JSON字符串va ...
- this 指向详细解析(箭头函数)
前言 this 指向问题是入坑前端必须了解知识点,现在迎来了ES6时代,因为箭头函数的出现,所以感觉有必要对 this 问题梳理一下,遂有此文 在非箭头函数下, this 指向调用其所在函数的对象,而 ...