2012年3月11日星期日

用html形式导出excel

普通的excel导出方式在遇到大数据量的时候经常导致OOM,采用拼接html方式可以解决 
首先设计好要导出的表格样式,将excel另存为为html格式,然后用文本编辑器打开html 
如果需要通用的导出方式,则需要自己拼接标题信息。 
首先定义一个标题定义类,可自定义标题栏的宽度 对齐方式 
Java代码 
  1. public class HtmlExportColumn {  
  2.     public static final String LEFT="xl29";  
  3.     public static final String MIDDLE="xl27";  
  4.     public static final String RIGHT="xl28";  
  5.     public static final String DEFAULT_HEADER_CLASS_TYPE = "xl25";//标题栏的样式  
  6.     private String name = "";// 标题名称  
  7.     private int width = 100;// 列宽度 默认值100  
  8.     private int msoWidthAlt = 80;// mso-width-alt值  
  9.     private int widthPt = 4000;// width值单位为pt  
  10.     private String classType = MIDDLE;// 水平样式xl29/xl27/xl28(左/中/右)  
  11.   
  12.     public HtmlExportColumn() {  
  13.         super();  
  14.     }  
  15.   
  16.     public HtmlExportColumn(String name, int width, int msoWidthAlt,  
  17.             int widthPt, String classType) {  
  18.         super();  
  19.         this.name = name;  
  20.         this.width = width;  
  21.         this.msoWidthAlt = msoWidthAlt;  
  22.         this.widthPt = widthPt;  
  23.         this.classType = classType;  
  24.     }  
  25.         //getter setter...  
  26.   
  27. }  
  
Java代码 
  1.     /** 
  2.      * 写excel顶部信息 <table>标签以前的信息  
  3.      * @param topTemp 头部定义文件 
  4.      * @param os 输出流  
  5.      * @throws Exception  
  6.      */  
  7. public static void writeTop(File topTemp,OutputStream os) throws Exception{    
  8.         byte[] b = new byte[1024];  
  9.         int bytesRead = 0;   
  10.         FileInputStream input = null;   
  11.         try {  
  12.             input = new FileInputStream(topTemp);    
  13.             while ((bytesRead = input.read(b,0,1024)) > 0) {  
  14.                 os.write(b, 0, bytesRead);  
  15.             }  
  16.         } catch (FileNotFoundException e) {  
  17.             logger.error("读取excel头信息文件异常"+topTemp.getAbsolutePath()+"不存在");  
  18.             throw e;  
  19.         } catch (IOException e) {  
  20.             logger.error("读取excel头信息异常"+e);  
  21.             throw e;  
  22.         } finally{  
  23.             try {  
  24.                 input.close();  
  25.                 os.flush();  
  26.             } catch (IOException e) {   
  27.                 logger.error("关闭读取excel头信息异常"+e);  
  28.                 throw e;  
  29.             }    
  30.         }   
  31.           
  32.     }  
  33.   
  34.     /** 
  35.      * 写标题信息  
  36.      * @param columns 列定义数组 
  37.      * @param os 输出流   
  38.      */  
  39.     public static void writeHeader(HtmlExportColumn[] columns,OutputStream os) throws UnsupportedEncodingException, IOException{  
  40.         StringBuilder header = new StringBuilder();  
  41.         for (HtmlExportColumn column : columns) {  
  42.             header.append("<col width=").append(column.getWidth()).append(" style='mso-width-source:userset;mso-width-alt:").append(column.getMsoWidthAlt()).append(";width:").append(column.getWidthPt()).append("pt'>\r\n");  
  43.         }  
  44.         header.append("<tr height=17 style='height:12.75pt'>\r\n");  
  45.         for (HtmlExportColumn column : columns) {  
  46.             header.append("\t<td class=").append(HtmlExportColumn.DEFAULT_HEADER_CLASS_TYPE).append(" style='width:").append(column.getWidthPt()).append("pt'>").append(column.getName()).append("</td>\r\n");  
  47.         }  
  48.         header.append("</tr>\r\n");  
  49.           
  50.         os.write(header.toString().getBytes("gbk"));  
  51.         header = null;  
  52.         os.flush();  
  53.     }   
  54. /** 
  55.      * 写内容信息 数据可以分批读取  
  56.      * @param columns 列定义数组 
  57.      * @param os 输出流 
  58.      * @param data 二维字符数组   
  59.      * */  
  60.     public static void writeData(HtmlExportColumn[] columns,String[][] data,OutputStream os) throws UnsupportedEncodingException, IOException{  
  61.         StringBuilder content = new StringBuilder();  
  62.         // 迭代数据集  
  63.         for (int i = 0; i < data.length; i++) {   
  64.             content.append("<tr height=17 style='height:12.75pt'>\r\n");  
  65.             for (int j = 0; j < data[i].length; j++) {   
  66.                 content.append("\t<td class=").append(columns[j].getClassType()).append(">").append(data[i][j]).append("</td>\r\n");  
  67.             }  
  68.             content.append("</tr>\r\n");  
  69.         }  
  70.         os.write(content.toString().getBytes("gbk"));  
  71.         content = null;  
  72.         os.flush();   
  73.     }   
  74.   
  75. /** 
  76.      * 写尾部信息  
  77.      * @param columns 列定义数组 
  78.      * @param os 输出流   
  79.      */  
  80.     public static void writeTail(HtmlExportColumn[] columns,OutputStream os) throws UnsupportedEncodingException, IOException{  
  81.         StringBuilder tail = new StringBuilder();   
  82.         tail.append("<![if supportMisalignedColumns]>\r\n");  
  83.         tail.append("<tr height=0 style='display:none'>\r\n");  
  84.         for (HtmlExportColumn column : columns) {  
  85.             tail.append("\t<td width=").append(column.getWidth()).append(" style='width:").append(column.getWidthPt()).append("pt'></td>\r\n");  
  86.         }  
  87.         tail.append("</tr>\r\n");  
  88.         tail.append("<![endif]>\r\n");  
  89.         tail.append("</table>\r\n");  
  90.         tail.append("</body>\r\n");  
  91.         tail.append("</html>\r\n");   
  92.         os.write(tail.toString().getBytes());  
  93.         tail = null;  
  94.         os.flush();  
  95.     }   

没有评论:

发表评论