DataTable 导出到Excel 的简单实现(非NPOI)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28522783/article/details/80428254
        /// <summary>
        /// 导出Excel Datatable版本
        /// </summary>
        public static void ExportByWeb()
        {
            DataTable dt = new DataTable("cart");
            DataColumn dc1 = new DataColumn("prizename");
            DataColumn dc2 = new DataColumn("point");
            DataColumn dc3 = new DataColumn("number");
            DataColumn dc4 = new DataColumn("totalpoint");
            DataColumn dc5 = new DataColumn("prizeid");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5); 
            for (int j = 0; j < 10; j++)
            {
                DataRow dr = dt.NewRow();
                dr["prizename"] = "从飒飒的阿斯达";
                dr["point"] = "从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达从飒飒的阿斯达";
                dr["number"] = "123123123";
                dr["totalpoint"] = "0啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊01";
                dr["prizeid"] = "001";
                dt.Rows.Add(dr);
            }
            HttpResponse resp = System.Web.HttpContext.Current.Response;
            string ExcelName = DateTime.Now.ToString("yyyyMMddHHmmss");//文件名
            resp.ContentEncoding = Encoding.UTF8;
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ExcelName, Encoding.UTF8) + ".xls");
            DataRow[] myRow = dt.Select();
            int k = 0;
            int cl = dt.Columns.Count;
            StringBuilder sb = new StringBuilder();
            sb.Append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>");
            sb.Append("<table><tr style='Gray 1px solid;text-align:center'>");
            for (int x = 0; x < cl; x++)
            {
                sb.Append("<td   bgcolor='#ABCDC1' style='vnd.ms-excel.numberformat:@;width:200px' >" + dt.Columns[x].ColumnName + "</td>");//列名
            }
            sb.Append("</tr>");

            foreach (DataRow row in myRow)
            {
                sb.Append("<tr>");
                for (k = 0; k < cl; k++)
                {
                    if (k == (cl - 1))
                        sb.Append("<td style='vnd.ms-excel.numberformat:@;width:200px'>" + row[k].ToString() + "</td></tr>");
                    else
                    {
                        if (k == 2)
                            sb.Append("<td style='vnd.ms-excel.numberformat:@;width:200px'>" + " " + row[k].ToString() + "</td>");
                        else
                            sb.Append("<td style='vnd.ms-excel.numberformat:@;width:200px' >" + row[k].ToString() + "</td>");
                    }
                }
            }
            sb.Append("</table></body></html>");
            resp.Write(sb.ToString());
            resp.End();
        }
//vnd.ms-excel.numberformat 导出Ecxel 格式
<td style="vnd.ms-excel.numberformat:@;"><s:property value="accountCode" /></td>

//1)文本:vnd.ms-excel.numberformat:@
//2)日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//3)数字:vnd.ms-excel.numberformat:#,##0.00
//4)货币:vnd.ms-excel.numberformat:¥#,##0.00
//5)百分比:vnd.ms-excel.numberformat: #0.00%

<fmt:formatNumber value="${(money / lastMoney - 1) * 100 }" pattern="#.##%" minFractionDigits="2" />

<fmt:formatNumber value="${n_balance}" pattern="#.##" minFractionDigits="2"/>

导出的简单效果图,可用作平时测试用,实际项目中,推荐使用NPOI进行操作
导出的简单效果图,可用作平时测试用,实际项目中,推荐使用NPOI进行操作

没有更多推荐了,返回首页