后台运用html格式导出Excel文件

///
/// 从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;'>&nbsp;" + ((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”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双叶红于二月花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值