Apache POI 实现Excel文件单元格合并、冻结和文件导出

来源:互联网 发布:淘宝怎么认证卖家 编辑:程序博客网 时间:2024/05/29 08:03
  1. Workbook wb = new HSSFWorkbook();  
  2.         Sheet sheet = wb.createSheet("sheet1");  
  3.         Row row = null;  
  4.         Cell cell = null;  
  5.         //创建表头单元格样式  
  6.         CellStyle cs_header = wb.createCellStyle();  
  7.         Font boldFont = wb.createFont();  
  8.         boldFont.setFontName("Consolas");  
  9.         boldFont.setFontHeightInPoints((short)14);  
  10.         cs_header.setFont(boldFont);  
  11.         cs_header.setBorderBottom((short)1);  
  12.         cs_header.setBorderLeft((short)1);  
  13.         cs_header.setBorderRight((short)1);  
  14.         cs_header.setBorderTop((short)1);  
  15.         //第一行,时间  
  16.         row = sheet.createRow((short)0);  
  17.         row.setHeightInPoints((short)24);  
  18.         createMultiCell(row,cell,cs_header,0,31,"访问日期:"+time1+" to "+time2);  
  19.         //第二行,大标题  
  20.         row = sheet.createRow((short)1);  
  21.         row.setHeightInPoints((short)24);  
  22.         createMultiCell(row,cell,cs_header,0,6," ");  
  23.         createMultiCell(row,cell,cs_header,6,25,"访问路径");  
  24.         //第三行,列标题  
  25.           
  26.         row = sheet.createRow((short)2);  
  27.         row.setHeightInPoints((short)24);  
  28.         String[] headers = new String[]{"访问时间","地域","来源","关键字","进入页","停留时间","访问页数",  
  29.                 "1时间","1停留","1页面","2时间","2停留","2页面","3时间","3停留","3页面","4时间","4停留","4页面",  
  30.                 "5时间","5停留","5页面","6时间","6停留","6页面","7时间","7停留","7页面","8时间","8停留","8页面"};  
  31.         for(int i=0; i<headers.length; i++){  
  32.             cell = row.createCell((short)i);cell.setCellValue(headers[i]);cell.setCellStyle(cs_header);  
  33.         }  
  34.         //创建文本单元格样式  
  35.         CellStyle cs_text = wb.createCellStyle();  
  36.         Font textFont = wb.createFont();  
  37.         textFont.setFontName("Consolas");  
  38.         textFont.setFontHeightInPoints((short)10);  
  39.         cs_text.setFont(textFont);  
  40.         cs_text.setBorderBottom((short)1);  
  41.         cs_text.setBorderLeft((short)1);  
  42.         cs_text.setBorderRight((short)1);  
  43.         cs_text.setBorderTop((short)1);  
  44.         //将数据写入表格  
  45.         for(int i=0; i<list.size(); i++){  
  46.             row = sheet.createRow((short)(i+3));  
  47.             Object[] rw = list.get(i);  
  48.             for(int j=0; j<rw.length; j++){  
  49.                 cell = row.createCell((short)j);  
  50.                 cell.setCellValue(rw[j].toString());  
  51.                 cell.setCellStyle(cs_text);  
  52.             }  
  53.         }  
  54.         //合并第1行1-32列  
  55.         sheet.addMergedRegion(new CellRangeAddress((short)0, (short)0, (short)0, (short)31));  
  56.         //合并第2行1-6列  
  57.         sheet.addMergedRegion(new CellRangeAddress((short)1, (short)1, (short)0, (short)6));  
  58.         //合并第2行7-32行  
  59.         sheet.addMergedRegion(new CellRangeAddress((short)1, (short)1, (short)7, (short)31));  
  60.         //冻结7X3(宽,高)区域中的单元格  
  61.         sheet.createFreezePane(73);  
  62.         try {  
  63.             //将workbook写到输入流(下载时候,这个输出流可能是ServletOutStream,写入文件是FileOutputStream,等等)  
  64.             wb.write(os);  
  65.         } catch (IOException e) {  
  66.             e.printStackTrace();  
  67.         } 
0 0
原创粉丝点击