e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

以下代码亲测可以使用。以下代码可以直接运行查看效果。

jar 下载地址:http://download.csdn.net/detail/qw0907/9741548

用的jar如下:

poi-3.8-20120326.jar

poi-ooxml-3.8-20120326.jar

poi-ooxml-schemas-3.8-20120326.jar

poi-scratchpad-3.8-20120326.jar

 

 

 读取.xls

 

  1.  
    import java.io.File;
  2.  
    import java.io.FileInputStream;
  3.  
    import java.util.List;
  4.  
     
  5.  
    import org.apache.poi.hssf.usermodel.HSSFPicture;
  6.  
    import org.apache.poi.hssf.usermodel.HSSFPictureData;
  7.  
    import org.apache.poi.hssf.usermodel.HSSFShape;
  8.  
    import org.apache.poi.hssf.usermodel.HSSFSheet;
  9.  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  10.  
    import org.apache.poi.ss.usermodel.Cell;
  11.  
    import org.apache.poi.ss.usermodel.DateUtil;
  12.  
    import org.apache.poi.ss.usermodel.Row;
  13.  
     
  14.  
    public final class TestImportExcel {
  15.  
     
  16.  
    public static void main(String[] args) throws Exception {
  17.  
     
  18.  
    File excelFile = new File("F:\\B5204645.xls");
  19.  
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
  20.  
    HSSFSheet sheet = wb.getSheetAt(0);
  21.  
     
  22.  
    for (Row row : sheet) {
  23.  
    for (Cell cell : row) {
  24.  
    switch (cell.getCellType()) {
  25.  
    case Cell.CELL_TYPE_STRING:
  26.  
    System.out.print(cell.getRichStringCellValue().getString());
  27.  
    System.out.print("|");
  28.  
    break;
  29.  
    case Cell.CELL_TYPE_NUMERIC:
  30.  
    if (DateUtil.isCellDateFormatted(cell)) {
  31.  
    System.out.print(String.valueOf(cell.getDateCellValue()));
  32.  
    } else {
  33.  
    System.out.print(cell.getNumericCellValue());
  34.  
    }
  35.  
    System.out.print("|");
  36.  
    break;
  37.  
    case Cell.CELL_TYPE_BOOLEAN:
  38.  
    System.out.print(cell.getBooleanCellValue());
  39.  
    System.out.print("|");
  40.  
    break;
  41.  
    default:
  42.  
    }
  43.  
    }
  44.  
    System.out.println();
  45.  
    }
  46.  
     
  47.  
    //读取图片
  48.  
    List<HSSFPictureData> pictures = wb.getAllPictures();
  49.  
    for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
  50.  
    if (shape instanceof HSSFPicture) {
  51.  
    HSSFPicture pic = (HSSFPicture) shape;
  52.  
    int pictureIndex = pic.getPictureIndex()-1;
  53.  
    HSSFPictureData picData = pictures.get(pictureIndex);
  54.  
    System.out.println("image-size:" + picData.getData().length);
  55.  
    }
  56.  
    }
  57.  
     
  58.  
    System.out.println(wb.getSheetName(0));
  59.  
    }
  60.  
    }
  61.  
     

 读取.xlsx

 

  1.  
    package com.sae.ecds.ct.excel;
  2.  
     
  3.  
    import java.io.File;
  4.  
    import java.io.FileInputStream;
  5.  
    import java.util.List;
  6.  
     
  7.  
    import org.apache.poi.ss.usermodel.Cell;
  8.  
    import org.apache.poi.ss.usermodel.DateUtil;
  9.  
    import org.apache.poi.ss.usermodel.Row;
  10.  
    import org.apache.poi.xssf.usermodel.XSSFPictureData;
  11.  
    import org.apache.poi.xssf.usermodel.XSSFSheet;
  12.  
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  13.  
     
  14.  
    public final class TestImportXlsx {
  15.  
     
  16.  
    public static void main(String[] args) throws Exception {
  17.  
     
  18.  
    File excelFile = new File("F:\\B52046450056.xlsx");
  19.  
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile));
  20.  
    XSSFSheet sheet = wb.getSheetAt(0);
  21.  
     
  22.  
    for (Row row : sheet) {
  23.  
    for (Cell cell : row) {
  24.  
    switch (cell.getCellType()) {
  25.  
    case Cell.CELL_TYPE_STRING:
  26.  
    System.out.print(cell.getRichStringCellValue().getString());
  27.  
    System.out.print("|");
  28.  
    break;
  29.  
    case Cell.CELL_TYPE_NUMERIC:
  30.  
    if (DateUtil.isCellDateFormatted(cell)) {
  31.  
    System.out.print(String.valueOf(cell.getDateCellValue()));
  32.  
    } else {
  33.  
    System.out.print(cell.getNumericCellValue());
  34.  
    }
  35.  
    System.out.print("|");
  36.  
    break;
  37.  
    case Cell.CELL_TYPE_BOOLEAN:
  38.  
    System.out.print(cell.getBooleanCellValue());
  39.  
    System.out.print("|");
  40.  
    break;
  41.  
    default:
  42.  
    }
  43.  
    }
  44.  
    System.out.println();
  45.  
    }
  46.  
     
  47.  
    //读取图片
  48.  
    List<XSSFPictureData> pictures = wb.getAllPictures();
  49.  
    for (int i = 0; i < pictures.size(); i++) {
  50.  
    XSSFPictureData pictureData = pictures.get(i);
  51.  
    byte[] picData = pictureData.getData();
  52.  
    System.out.println("image-size:" + picData.length);
  53.  
    }
  54.  
     
  55.  
     
  56.  
    System.out.println(wb.getSheetName(0));
  57.  
    }
  58.  
    }
  59.  
     


posted on 2019-09-06 20:00  纯黑Se丶  阅读(4963)  评论(0编辑  收藏  举报