后台运用html格式导出Excel文件
来源:互联网 发布:实况2016优化 编辑:程序博客网 时间:2024/05/21 20:26
///
/// 从DataTable下载HTML标签的EXCEL文件
///
/// 上下文内容
/// DataTable数据
/// 需要添加序号列
/// 用户文件名
public static void GetExcelFromDataTable(System.Web.HttpContext context, DataTable dt, bool needIndex, string customFileName)
{
if (customFileName.Trim() == string.Empty)
{
customFileName = dt.TableName + “.xls”;
}
if (customFileName.IndexOf(“.”) == -1)
{
customFileName = customFileName + “.xls”;
}
DownLoadByteArray(context, getAnalyzeTemplet(dt, needIndex), customFileName);
}
private static string getAnalyzeTemplet(DataTable dt, bool needIndex)
{
System.Text.StringBuilder str = new System.Text.StringBuilder();
str.Append(“”
+ “http://www.w3.org/1999/xhtml\“>”
+ “”
+ “”
+ “无标题文档”
+ “”
+ “”);
str.Append(“
str.Append("<tr style='height:24px;'>"); if (needIndex) { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>序号</td>"); } foreach (DataColumn dc in dt.Columns) { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>" + dc.ColumnName + "</td>"); } str.Append("</tr>"); int count = 1; foreach (DataRow dr in dt.Rows) { str.Append("<tr style='height:24px;'>"); if (needIndex) { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>" + count.ToString() + "</td>"); } foreach (DataColumn dc in dt.Columns) { if (dc.DataType.Name.ToLower() == "decimal" && dr[dc.ColumnName] != System.DBNull.Value) { str.Append("<td style='text-align:right; border:solid thin black; margin:2px;'> " + ((decimal)dr[dc.ColumnName]).ToString("0.00#") + "</td>"); } else { if (dc.ColumnName == "单位") { if (dr[dc.ColumnName].ToString() == "m2") { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>m<sup>2</sup></td>"); } else if (dr[dc.ColumnName].ToString() == "m3") { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>m<sup>3</sup></td>"); } else if (dr[dc.ColumnName].ToString() == "mm2") { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>mm<sup>2</sup></td>"); } else { str.Append("<td style='text-align:center; border:solid thin black; margin:2px;'>" + dr[dc.ColumnName].ToString() + "</td>"); } } else { str.Append("<td style='border:solid thin black; margin:2px;'>" + dr[dc.ColumnName].ToString() + "</td>"); } } } str.Append("</tr>"); count++; } str.Append("</table>"); str.Append("</body></html>"); return str.ToString();}
///
/// 下载字符串格式内容的文件
///
///
///
///
public static void DownLoadByteArray(System.Web.HttpContext context, string Content, string CustomFileName)
{
byte[] byteBuffer = System.Text.Encoding.Default.GetBytes(Content);
DownLoadByteArray(context, byteBuffer, CustomFileName);
}
///
/// 下载byte流格式内容的文件
///
///
///
///
public static void DownLoadByteArray(System.Web.HttpContext context, byte[] byteBuffer, string CustomFileName)
{
context.Response.Clear(); // long fileSize = byteBuffer.Length;//fileStream.Length; context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + CustomFileName + "\""); context.Response.AddHeader("Content-Length", byteBuffer.Length.ToString()); context.Response.BinaryWrite(byteBuffer); context.Response.End();}
调用方式:
GetExcelFromDataTable(this.Context, dt, true, “TongJiHuiZong”);
- 后台运用html格式导出Excel文件
- 按html格式 导出 excel
- 用html格式导出excel
- jsp/html导出excel文件
- Firefox导出Excel报表为Html格式
- Python aiohttp后台导出excel文件
- MFC将ListCtrl列表导出到Excel-采用文件流写Excel兼容Html格式(不需要Excel环境)
- Spring MVC+ Ireport 做统计报表,导出HTML、EXCEL等格式的文件,打印;
- java后台html导出为word文件
- POI---JAVA导出Excel文件<二>Excel格式设置
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- ireport导出excel,html.pdf等格式完整实例
- 在JSP导出EXCEL(HTML格式)出现乱码
- ireport导出各种格式(pdf,excel,word,html,print)
- ireport导出各种格式(pdf,excel,word,html,print)
- NSString之 doubleValue floatValue intValue integerValue longLongValue
- 关于jquery插件的一点使用心得
- Maven学习总结(22)——Maven常用插件介绍
- ubuntu14.04 搭建ftp
- 通过CoordinatorLayout制作高级滚动特效
- 后台运用html格式导出Excel文件
- Android Launcher T9搜索APP实现
- Android ActionBar使用笔记
- 自动循环滚动textview
- 内存溢出问题解决方案与Visualvm监听添加
- USB的VID和PID
- Java获取当前系统时间System.currentTimeMillis()
- java excel通过浏览器下载
- Using Flashback in a Data Guard Environment