美文网首页
Apache POI -Java Excel

Apache POI -Java Excel

作者: 夏日橘子冰 | 来源:发表于2017-07-03 15:10 被阅读0次

一、创建电子簿

System.out.println("----------创建一个excel工作薄----------");
        XSSFWorkbook workbook = new XSSFWorkbook();
        FileOutputStream out;
        try {
            out = new FileOutputStream(new File("createworkbook.xlsx"));
            workbook.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("成功创建");

二、读取电子簿

File file = new File("openworkbook.xlsx");
        FileInputStream fIP;
        try {
            fIP = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(fIP);
            if (file.isFile() && file.exists()) {
                System.out.println("openworkbook.xlsx打开成功.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

三、创建电子表格

        XSSFWorkbook workbook = new XSSFWorkbook(); 
        //创建一个电子表格,对应excel表中的sheet
        XSSFSheet spreadsheet = workbook.createSheet("kitty information");
        //设置单元格格式
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 20);
        cellStyle.setFont(font);
        //数据
        Map<String,Object[]> info = new TreeMap<String, Object[]>();
        info.put("1",new Object[] {"日期","地址","爱好"} );
        info.put("2",new Object[] {"20170629","广州市","游泳"} );
        info.put("3",new Object[] {"20170630","上海市","打羽毛球"} );
        info.put("4",new Object[] {"20170701","北京市","吃驴肉火烧"} );
        Set<String> keyid = info.keySet();
        int rowid = 0;
        for(String key : keyid){
            XSSFRow row = spreadsheet.createRow(rowid++);
            int cellid=0;
            for(Object obj : info.get(key)){
                Cell cell = row.createCell(cellid++);
                cell.setCellValue((String)obj);
                if(rowid==0){
                    cell.setCellStyle(cellStyle);
                }
            }
        }
        //输出到excel表
        FileOutputStream outStream;
        try {
            outStream = new FileOutputStream(new File("writeSheet.xlsx"));
            workbook.write(outStream);
            outStream.close();
            System.out.println("写表成功");
        } catch (IOException e) {
            e.printStackTrace();
        }```

###四、读取电子表格
```try{
            FileInputStream fis = new FileInputStream(new File("writeSheet.xlsx"));
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheetAt(0);
            if(sheet != null){
                for(int i=0;i<sheet.getPhysicalNumberOfRows();i++){
                    XSSFRow row = sheet.getRow(i);
                    for(int j=0;j<row.getLastCellNum();j++){
                        System.out.println("第"+i+"行"+"第"+j+"列:"+row.getCell(j).toString());
                    }
                }
            }       
        }catch(IOException e){
            e.printStackTrace();
        }```

相关文章

网友评论

      本文标题:Apache POI -Java Excel

      本文链接:https://www.haomeiwen.com/subject/zdracxtx.html