zoukankan      html  css  js  c++  java
  • Java POI读取excel 支持xls、xlsx

    import java.io.File;
    import java.io.FileInputStream;
    import java.util.List;
     
    import org.apache.poi.hssf.usermodel.HSSFPicture;
    import org.apache.poi.hssf.usermodel.HSSFPictureData;
    import org.apache.poi.hssf.usermodel.HSSFShape;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DateUtil;
    import org.apache.poi.ss.usermodel.Row;
     
    public final class TestImportExcel {
     
        public static void main(String[] args) throws Exception  {
     
            File excelFile = new File("test.xls");
            HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
            HSSFSheet sheet = wb.getSheetAt(0);
     
            for (Row row : sheet) {
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getRichStringCellValue().getString());
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (DateUtil.isCellDateFormatted(cell)) {
                            System.out.print(String.valueOf(cell.getDateCellValue()));
                        } else {
                            System.out.print(cell.getNumericCellValue());
                        }
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue());
                        break;
                    default:
                    }
                }
                System.out.println();
            }
             
            //读取图片
            List<HSSFPictureData> pictures = wb.getAllPictures();  
            for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {  
                if (shape instanceof HSSFPicture) {
                    HSSFPicture pic = (HSSFPicture) shape;  
                    int pictureIndex = pic.getPictureIndex()-1;  
                    HSSFPictureData picData = pictures.get(pictureIndex);
                    System.out.println("image-size:" + picData.getData().length);
                }  
            }  
             
            System.out.println(wb.getSheetName(0));
        }
    }
    
  • 相关阅读:
    hihocoder 1142 三分·三分求极值(三分)
    poj 3304 Segments(计算直线与线段之间的关系)
    poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)
    poj 2398 Toy Storage(计算几何 点线关系)
    poj 2318 TOYS(计算几何 点与线段的关系)
    计算几何基础(模板)
    Jmeter-基本组成
    java-面向对象
    性能测试基础
    java-多线程
  • 原文地址:https://www.cnblogs.com/sunBinary/p/12035954.html
Copyright © 2011-2022 走看看