后台运用html格式导出Excel文件
2016-07-15 16:59
609 查看
///
/// 从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(“
”);
///
/// 下载字符串格式内容的文件
///
///
///
///
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)
{
调用方式:
GetExcelFromDataTable(this.Context, dt, true, “TongJiHuiZong”);
/// 从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常用标签的使用方法
- .html(),.text()和.val()的差异总结:
- html元素水平居中、垂直居中、水平垂直居中于其父级元素的方法
- html隐藏 展开DIV
- 【旧代码整理】controller继承Action渲染输出html部分
- Yii净化器CHtmlPurifier用法示例(过滤不良代码)
- C#、filestream、streamwriter,html方式生成excel
- 在学习HTML——form表单中的label标签时的一点小体会
- HTML 教程
- Django输出非HTML内容
- html meta
- HTML-便签、元素、属性
- [html代码] 几种美丽的分割线
- html form
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
- html标签小记录
- html中rem布局后,在背景图片压缩后,根据压缩比例计算代码
- html一些总结
- 关于HTML标签的反转义函数
- Html.ActionLink无法跳转