您的位置:首页 > Web前端 > HTML

后台运用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(“
”);
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”);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: