zoukankan      html  css  js  c++  java
  • 导出excel

    主要就是把下面表格导出

    需要一个aspx页 做request处理

    设置一下response头 write一个table出去就ok了

        public void ExportExcel(ArrayList columns, ArrayList data)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            //Response.Charset = "UTF-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + "grid" + ".xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
            EnableViewState = false;
            Response.Write(ExportTable(data, columns));
            Response.End();
        }
    
        public static string ExportTable(ArrayList data, ArrayList columns)
        {
            StringBuilder sb = new StringBuilder();
            //data = ds.DataSetName + "
    ";
            int count = 0;
    
            //data += tb.TableName + "
    ";
            sb.AppendLine("<meta http-equiv="Content-Type" content="text/html; charset=gb2312">");
            sb.AppendLine("<table cellspacing="0" cellpadding="5" rules="all" border="1">");
            //写出列名
            sb.AppendLine("<tr style="font-weight: bold; white-space: nowrap;">");
            foreach (Hashtable column in columns)
            {
                sb.AppendLine("<td>" + column["header"] + "</td>");
            }
            sb.AppendLine("</tr>");
    
            //写出数据
            foreach (Hashtable row in data)
            {
                sb.Append("<tr>");
                foreach (Hashtable column in columns)
                {
                    if (column["field"] == null) continue;
                    Object value = row[column["field"]];
                    sb.AppendLine("<td>" + value + "</td>");
                }
                sb.AppendLine("</tr>");
                count++;
            }
            sb.AppendLine("</table>");
    
            return sb.ToString();
        }
  • 相关阅读:
    linux中压缩、解压缩命令
    linux中的sed指令
    linux中shell编程(一)
    linux中的正则表达式
    linux中的管道和重定向
    linux中用户、组和权限相关指令
    linux中bash常见的指令
    linux文本操作相关指令
    java.lang.OutOfMemoryError 解决程序启动内存溢出问题
    Java常用排序算法/程序员必须掌握的8大排序算法
  • 原文地址:https://www.cnblogs.com/frog2008/p/6890390.html
Copyright © 2011-2022 走看看