poi最全面导出

来源:互联网 发布:linux虚拟机wifi破解 编辑:程序博客网 时间:2024/05/21 10:32
String sFileName = "采购执行导入模板.xls";
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(sFileName, "UTF-8"))));
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/vnd.ms-excel");

String title = "采购执行导入模板";//名称
   String[] headers = new String[]{"省公司代码","采购订单编号","订单创建时间","组织标识","OU名称","供应商编号","供应商名称","统一供应商编码","币种",
    "采购订单总金额","总部框架协议编码","省公司合同编号","合同名称","行号","统一物料编码","物料说明","数量","计量单位","单价"};//表头
   String[] fields = new String[]{"userId","phoneNum","exchangeIntegral","cardNum","applyDate","exchangeState","exchangeDate"};//字段名字
   
   //List<?> list = this.customerExchangeManageService.queryExportCustomerExchangeList(params);//数值list
   OutputStream out = response.getOutputStream();

   CreateExcel.createExcel(title,headers,fields, new ArrayList(),out,0);



开源代码

  1. /** 
  2.  * 利用开源组件POI3.0.2动态导出EXCEL文档 
  3.  * 转载时请保留以下信息,注明出处! 
  4.  * @author leno 
  5.  * @version v1.0 
  6.  * @param <T> 应用泛型,代表任意一个符合javabean风格的类 
  7.  * 注意这里为了简单起见,boolean型的属性xxx的get器方式为getXxx(),而不是isXxx() 
  8.  * byte[]表jpg格式的图片数据 
  9.  */  
  10. public class ExportExcel<T> {  
  11.   
  12.     public void exportExcel(Collection<T> dataset, OutputStream out) {  
  13.         exportExcel("测试POI导出EXCEL文档"null, dataset, out, "yyyy-MM-dd");  
  14.     }  
  15.   
  16.     public void exportExcel(String[] headers, Collection<T> dataset,  
  17.             OutputStream out) {  
  18.         exportExcel("测试POI导出EXCEL文档", headers, dataset, out, "yyyy-MM-dd");  
  19.     }  
  20.   
  21.     public void exportExcel(String[] headers, Collection<T> dataset,  
  22.             OutputStream out, String pattern) {  
  23.         exportExcel("测试POI导出EXCEL文档", headers, dataset, out, pattern);  
  24.     }  
  25.   
  26.     @SuppressWarnings("unchecked")  
  27.     public void exportExcel(String title, String[] headers,  
  28.             Collection<T> dataset, OutputStream out, String pattern) {  
  29.         // 声明一个工作薄   
  30.         HSSFWorkbook workbook = new HSSFWorkbook();  
  31.         // 生成一个表格   
  32.         HSSFSheet sheet = workbook.createSheet(title);  
  33.         // 设置表格默认列宽度为15个字节   
  34.         sheet.setDefaultColumnWidth((short15);  
  35.         // 生成一个样式   
  36.         HSSFCellStyle style = workbook.createCellStyle();  
  37.         // 设置这些样式   
  38.         style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
  39.         style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  40.         style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  41.         style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  42.         style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  43.         style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  44.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  45.         // 生成一个字体   
  46.         HSSFFont font = workbook.createFont();  
  47.         font.setColor(HSSFColor.VIOLET.index);  
  48.         font.setFontHeightInPoints((short12);  
  49.         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
  50.         // 把字体应用到当前的样式   
  51.         style.setFont(font);  
  52.         // 生成并设置另一个样式   
  53.         HSSFCellStyle style2 = workbook.createCellStyle();  
  54.         style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);  
  55.         style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  56.         style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
  57.         style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
  58.         style2.setBorderRight(HSSFCellStyle.BORDER_THIN);  
  59.         style2.setBorderTop(HSSFCellStyle.BORDER_THIN);  
  60.         style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  61.         style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  62.         // 生成另一个字体   
  63.         HSSFFont font2 = workbook.createFont();  
  64.         font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
  65.         // 把字体应用到当前的样式   
  66.         style2.setFont(font2);  
  67.   
  68.         // 声明一个画图的顶级管理器   
  69.         HSSFPatriarch patriarch = sheet.createDrawingPatriarch();  
  70.         // 定义注释的大小和位置,详见文档   
  71.         HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,  
  72.                 000, (short42, (short65));  
  73.         // 设置注释内容   
  74.         comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));  
  75.         // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.   
  76.         comment.setAuthor("leno");  
  77.   
  78.         //产生表格标题行   
  79.         HSSFRow row = sheet.createRow(0);  
  80.         for (short i = 0; i < headers.length; i++) {  
  81.             HSSFCell cell = row.createCell(i);  
  82.             cell.setCellStyle(style);  
  83.             HSSFRichTextString text = new HSSFRichTextString(headers[i]);  
  84.             cell.setCellValue(text);  
  85.         }  
  86.   
  87.         //遍历集合数据,产生数据行   
  88.         Iterator<T> it = dataset.iterator();  
  89.         int index = 0;  
  90.         while (it.hasNext()) {  
  91.             index++;  
  92.             row = sheet.createRow(index);  
  93.             T t = (T) it.next();  
  94.             //利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值  
  95.             Field[] fields = t.getClass().getDeclaredFields();  
  96.             for (short i = 0; i < fields.length; i++) {  
  97.                 HSSFCell cell = row.createCell(i);  
  98.                 cell.setCellStyle(style2);  
  99.                 Field field = fields[i];  
  100.                 String fieldName = field.getName();  
  101.                 String getMethodName = "get"  
  102.                         + fieldName.substring(01).toUpperCase()  
  103.                         + fieldName.substring(1);  
  104.                 try {  
  105.                     Class tCls = t.getClass();  
  106.                     Method getMethod = tCls.getMethod(getMethodName,  
  107.                             new Class[] {});  
  108.                     Object value = getMethod.invoke(t, new Object[] {});  
  109.                     //判断值的类型后进行强制类型转换   
  110.                     String textValue = null;  
  111.                     if (value instanceof Boolean) {  
  112.                         boolean bValue = (Boolean) value;  
  113.                         textValue = "男";  
  114.                         if (!bValue) {  
  115.                             textValue = "女";  
  116.                         }  
  117.                     } else if (value instanceof Date) {  
  118.                         Date date = (Date) value;  
  119.                         SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  120.                         textValue = sdf.format(date);  
  121.                     } else if (value instanceof byte[]) {  
  122.                         // 有图片时,设置行高为60px;   
  123.                         row.setHeightInPoints(60);  
  124.                         // 设置图片所在列宽度为80px,注意这里单位的一个换算   
  125.                         sheet.setColumnWidth(i, (short) (35.7 * 80));  
  126.                         // sheet.autoSizeColumn(i);   
  127.                         byte[] bsValue = (byte[]) value;  
  128.                         HSSFClientAnchor anchor = new HSSFClientAnchor(00,  
  129.                                 1023255, (short6, index, (short6, index);  
  130.                         anchor.setAnchorType(2);  
  131.                         patriarch.createPicture(anchor, workbook.addPicture(  
  132.                                 bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));  
  133.                     } else {  
  134.                         //其它数据类型都当作字符串简单处理   
  135.                         textValue = value.toString();  
  136.                     }  
  137.                     //如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成  
  138.                     if (textValue != null) {  
  139.                         Pattern p = Pattern.compile("^//d+(//.//d+)?$");  
  140.                         Matcher matcher = p.matcher(textValue);  
  141.                         if (matcher.matches()) {  
  142.                             //是数字当作double处理   
  143.                             cell.setCellValue(Double.parseDouble(textValue));  
  144.                         } else {  
  145.                             HSSFRichTextString richString = new HSSFRichTextString(  
  146.                                     textValue);  
  147.                             HSSFFont font3 = workbook.createFont();  
  148.                             font3.setColor(HSSFColor.BLUE.index);  
  149.                             richString.applyFont(font3);  
  150.                             cell.setCellValue(richString);  
  151.                         }  
  152.                     }  
  153.                 } catch (SecurityException e) {  
  154.                     // TODO Auto-generated catch block   
  155.                     e.printStackTrace();  
  156.                 } catch (NoSuchMethodException e) {  
  157.                     // TODO Auto-generated catch block  
  158.                     e.printStackTrace();  
  159.                 } catch (IllegalArgumentException e) {  
  160.                     // TODO Auto-generated catch block   
  161.                     e.printStackTrace();  
  162.                 } catch (IllegalAccessException e) {  
  163.                     // TODO Auto-generated catch block  
  164.                     e.printStackTrace();  
  165.                 } catch (InvocationTargetException e) {  
  166.                     // TODO Auto-generated catch block   
  167.                     e.printStackTrace();  
  168.                 } finally {  
  169.                     //清理资源   
  170.                 }  
  171.             }  
  172.   
  173.         }  
  174.         try {  
  175.             workbook.write(out);  
  176.         } catch (IOException e) {  
  177.             // TODO Auto-generated catch block  
  178.             e.printStackTrace();  
  179.         }  
  180.   
  181.     }  
  182.   
  183. }  
  1. public class ParseXls {  
  2.       
  3.     public List<PersonVO> readXls() throws IOException, IllegalArgumentException,  
  4.             IllegalAccessException, InvocationTargetException {  
  5.         InputStream is = new FileInputStream("D:\\excel\\xls_test2.xls");  
  6.         HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);  
  7.         List<PersonVO> personList = new ArrayList<PersonVO>();  
  8.   
  9.         // 循环工作表Sheet   
  10.         for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {  
  11.             HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);  
  12.             if (hssfSheet == null) {  
  13.                 continue;  
  14.             }  
  15.   
  16.             // 循环行Row   
  17.             for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {  
  18.                 HSSFRow hssfRow = hssfSheet.getRow(rowNum);  
  19.                 if (hssfRow == null) {  
  20.                     continue;  
  21.                 }  
  22.                 PersonVO personVO = new PersonVO();  
  23.                   
  24.                 // 循环列Cell   
  25.                 for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {  
  26.                     HSSFCell hssfCell = hssfRow.getCell(cellNum);  
  27.                     if (hssfCell == null) {  
  28.                         continue;  
  29.                     }  
  30.                       
  31.                     personVO.setName(getValue(hssfRow.getCell(0)));  
  32.                     personVO.setSex(getValue(hssfRow.getCell(1)));  
  33.                     personVO.setAge(getValue(hssfRow.getCell(2)));  
  34.                     personVO.setPosition(getValue(hssfRow.getCell(3)));  
  35.                     personVO.setDept(getValue(hssfRow.getCell(4)));  
  36.                     System.out.print(" " + getValue(hssfCell));  
  37.                 }  
  38.                 personList.add(personVO);  
  39.                 System.out.println();  
  40.             }  
  41.         }  
  42.         return personList;  
  43.     }  
  44.   
  45.     @SuppressWarnings("static-access")  
  46.     private String getValue(HSSFCell hssfCell) {  
  47.         if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {  
  48.             return String.valueOf(hssfCell.getBooleanCellValue());  
  49.         } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {  
  50.             return String.valueOf(hssfCell.getNumericCellValue());  
  51.         } else {  
  52.             return String.valueOf(hssfCell.getStringCellValue());  
  53.         }  
  54.     }  
  55. }  

0 0