excel导入导出

两种方式

  第一:利用第三方插件JXL实现excel的生成;

  第二:利用apache的poi去生成;

POI的方式:

  一:倒入jar包;

  二:创建实体类;

  三:excel工具类;

    1.先声明一个工作簿

       HSSFWorkbook workbook = new HSSFWorkbook();  

    2.生成一个表格

      HSSFSheet sheet = workbook.createSheet(title); 

    3. 设置表格默认列宽度为15个字节 

       sheet.setDefaultColumnWidth((short) 15); 

    4.生成一个样式

      HSSFCellStyle style = workbook.createCellStyle();

    5.设置这些样式

          style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  

        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  

        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  

        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  

           style.setBorderRight(HSSFCellStyle.BORDER_THIN);  

        style.setBorderTop(HSSFCellStyle.BORDER_THIN);  

         style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 

    6.生成一个字体

         HSSFFont font = workbook.createFont();  

         font.setColor(HSSFColor.VIOLET.index);  

         font.setFontHeightInPoints((short) 12);  

            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 

    7.把字体应用到样式中

        style.setFont(font);

    8.创建表头

         HSSFRow row = sheet.createRow(0);

for (short i = 0; i < headers.length; i++) {  

        HSSFCell cell = row.createCell(i);  

        cell.setCellStyle(style);  

       HSSFRichTextString text = new HSSFRichTextString(headers[i]);  

        cell.setCellValue(text);  

    }  

    9.创建单元格

  1.  // 遍历集合数据,产生数据行  
  2.     Iterator<T> it = dataset.iterator();  
  3.     int index = 0;  
  4.     while (it.hasNext()) {  
  5.         index++;  
  6.         row = sheet.createRow(index);  
  7.         Object t = it.next(); //这里不要使用泛型强制转换  
  8.         // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值  
  9.         Field[] fields = t.getClass().getDeclaredFields();  
  10.         for (short i = 0; i < fields.length; i++) {  
  11.             HSSFCell cell = row.createCell(i);  
  12.             cell.setCellStyle(style2);  
  13.             Field field = fields[i];  
  14.             String fieldName = field.getName();  
  15.             String getMethodName = "get"  
  16.                     + fieldName.substring(0, 1).toUpperCase()  
  17.                     + fieldName.substring(1);  
  18.             try {  
  19.                 Class tCls = t.getClass();  
  20.                 Method getMethod = tCls.getMethod(getMethodName,  
  21.                         new Class[] {});  
  22.                 Object value = getMethod.invoke(t, new Object[] {});  
  23.                 // 判断值的类型后进行强制类型转换  
  24.                 String textValue = "";  
  25.                 if(value == null || "".equals(value)){  
  26.                     value = "";  
  27.                 }else if (value instanceof Integer) {  
  28.                     int intValue = (Integer) value;  
  29.                     cell.setCellValue(intValue);  
  30.                 } else if (value instanceof Float) {  
  31.                     float fValue = (Float) value;  
  32.                     cell.setCellValue(fValue + "");  
  33.                 } else if (value instanceof Double) {  
  34.                     double dValue = (Double) value;  
  35.                     cell.setCellValue(dValue + "");  
  36.                 } else if (value instanceof Long) {  
  37.                     long longValue = (Long) value;  
  38.                     cell.setCellValue(longValue);  
  39.                 }  
  40.                 if (value instanceof Boolean) {  
  41.                     boolean bValue = (Boolean) value;  
  42.                     textValue = "男";  
  43.                     if (!bValue) {  
  44.                         textValue = "女";  
  45.                     }  
  46.                 } else if (value instanceof Date) {  
  47.                     Date date = (Date) value;  
  48.                     SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
  49.                     textValue = sdf.format(date);  
  50.                 } else {  
  51.                     // 其它数据类型都当作字符串简单处理  
  52.                     textValue = value.toString();  
  53.                 }  
  54.                 // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成  
  55.                 if (textValue != null) {  
  56.                     Pattern p = Pattern.compile("^//d+(//.//d+)?{1}");  
  57.                     Matcher matcher = p.matcher(textValue);  
  58.                     if (matcher.matches()) {  
  59.                         // 是数字当作double处理  
  60.                         cell.setCellValue(Double.parseDouble(textValue));  
  61.                     } else {  
  62.                         HSSFRichTextString richString = new HSSFRichTextString(  
  63.                                 textValue);  
  64.                         HSSFFont font3 = workbook.createFont();  
  65.                         font3.setColor(HSSFColor.BLUE.index);  
  66.                         richString.applyFont(font3);  
  67.                         cell.setCellValue(richString);  
  68.                     }  
  69.                 }  
  70.             } catch (SecurityException e) {  
  71.                 e.printStackTrace();  
  72.             } catch (NoSuchMethodException e) {  
  73.                 e.printStackTrace();  
  74.             } catch (IllegalArgumentException e) {  
  75.                 e.printStackTrace();  
  76.             } catch (IllegalAccessException e) {  
  77.                 e.printStackTrace();  
  78.             } catch (InvocationTargetException e) {  
  79.                 e.printStackTrace();  
  80.             } finally {  
  81.                 // 清理资源  
  82.             }  
  83.         }  
  84.     }  
  85.     try {
  86. //默认导出到E盘下
  87.         FileOutputStream out = new FileOutputStream(E://name.xls);  
     
      
  88.         workbook.write(out);  
  89.     } catch (IOException e) {  
  90.         e.printStackTrace();  
  91.     }  
  92. }  

    

  

posted @ 2017-12-12 10:46  duoyun  阅读(134)  评论(0)    收藏  举报