日期:2014-05-17 浏览次数:20728 次
public class HtmlExportColumn { public static final String LEFT="xl29"; public static final String MIDDLE="xl27"; public static final String RIGHT="xl28"; public static final String DEFAULT_HEADER_CLASS_TYPE = "xl25";//标题栏的样式 private String name = "";// 标题名称 private int width = 100;// 列宽度 默认值100 private int msoWidthAlt = 80;// mso-width-alt值 private int widthPt = 4000;// width值单位为pt private String classType = MIDDLE;// 水平样式xl29/xl27/xl28(左/中/右) public HtmlExportColumn() { super(); } public HtmlExportColumn(String name, int width, int msoWidthAlt, int widthPt, String classType) { super(); this.name = name; this.width = width; this.msoWidthAlt = msoWidthAlt; this.widthPt = widthPt; this.classType = classType; } //getter setter... }
/** * 写excel顶部信息 <table>标签以前的信息 * @param topTemp 头部定义文件 * @param os 输出流 * @throws Exception */ public static void writeTop(File topTemp,OutputStream os) throws Exception{ byte[] b = new byte[1024]; int bytesRead = 0; FileInputStream input = null; try { input = new FileInputStream(topTemp); while ((bytesRead = input.read(b,0,1024)) > 0) { os.write(b, 0, bytesRead); } } catch (FileNotFoundException e) { logger.error("读取excel头信息文件异常"+topTemp.getAbsolutePath()+"不存在"); throw e; } catch (IOException e) { logger.error("读取excel头信息异常"+e); throw e; } finally{ try { input.close(); os.flush(); } catch (IOException e) { logger.error("关闭读取excel头信息异常"+e); throw e; } } } /** * 写标题信息 * @param columns 列定义数组 * @param os 输出流 */ public static void writeHeader(HtmlExportColumn[] columns,OutputStream os) throws UnsupportedEncodingException, IOException{ StringBuilder header = new StringBuilder(); for (HtmlExportColumn column : columns) { 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"); } header.append("<tr height=17 style='height:12.75pt'>\r\n"); for (HtmlExportColumn column : columns) { 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"); } header.append("</tr>\r\n"); os.write(header.toString().getBytes("gbk")); header = null; os.flush(); } /** * 写内容信息 数据可以分批读取 * @param columns 列定义数组 * @param os 输出流 * @param data 二维字符数组 * */ public static void writeData(HtmlExportColumn[] columns,String[][] data,OutputStream os) throws UnsupportedEncodingException, IOException{ StringBuilder content = new StringBuilder(); // 迭代数据集 for (int i = 0; i < data.length; i++) { content.append("<tr height=17 style='height:12.75pt'>\r\n"); for (int j = 0; j < data[i].length; j++) { content.append("\t<td class=").append(columns[j].getClassType()).append(">").append(data[i][j]).append("</td>\r\n"); } content.append("</tr>\r\n"); } os.write(content.toString().getBytes("gbk")); content = null; os.flush(); } /** * 写尾部信息 * @param columns 列定义数组 * @param os 输出流 */ public static void writeTail(HtmlExportColumn[] columns,OutputStream os) throws UnsupportedEncodingException, IOException{ StringBuilder tail = new StringBuilder(); tail.append("<![if supportMisalignedColumns]>\r\n"); tail.append("<tr height=0 style='display:none'>\r\n"); for (HtmlExportColumn column : columns) { tail.append("\t<td width=").append(column.getWidth()).append(" style='width:").append(c