一、新建工作簿、sheet、单元格

 1 public static void main(String[] args) throws Exception {
 2         Workbook wb = new HSSFWorkbook();    //定义一个新的工作簿
 3         Sheet sheet = wb.createSheet("第一个sheet页");
 4         wb.createSheet("第二个sheet页");
 5         Row row = sheet.createRow(0);                //创建第0行
 6         Cell cell = row.createCell(0);                //创建一个单元格,第0行,第1列
 7         cell.setCellValue(1);                         //给单元格设置值
 8         row.createCell(1).setCellValue(1.2);        //第2列
 9         row.createCell(2).setCellValue("这是一个字符串");
10         row.createCell(3).setCellValue(false);
11         FileOutputStream fileOut = new FileOutputStream("E:\\用POI搞出来的工作簿3.xls");
12         wb.write(fileOut);
13         fileOut.close();
14     }

效果图:

java操作Excel之POI(1)

 

 

二、创建一个时间格式的单元格:

 1 public static void main(String[] args) throws Exception {
 2         Workbook wb = new HSSFWorkbook();    //定义一个新的工作簿
 3         Sheet sheet = wb.createSheet("第一个sheet页");
 4         wb.createSheet("第二个sheet页");
 5         Row row = sheet.createRow(0);                //创建第0行
 6         Cell cell = row.createCell(0);                //创建一个单元格,第0行,第1列
 7         cell.setCellValue(new Date());
 8         
 9         CreationHelper creationHelper = wb.getCreationHelper();    
10         CellStyle cellStyle = wb.createCellStyle();
11         //设置单元格时间格式
12         cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
13         cell = row.createCell(1);                    //第2列
14         cell.setCellValue(new Date());
15         cell.setCellStyle(cellStyle);
16         
17         cell = row.createCell(2);                    //第3列
18         cell.setCellValue(Calendar.getInstance());
19         cell.setCellStyle(cellStyle);
20         
21         FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls");
22         wb.write(fileOut);
23         fileOut.close();
24     }

效果:

java操作Excel之POI(1)

 

三、创建不同格式的单元格:

/**
     * 创建不同格式的单元格
     */
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook();    //定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个sheet页");
        Row row = sheet.createRow(0);        //第一行
        row.createCell(0).setCellValue(new Date());    //第1列
        row.createCell(1).setCellValue(1);
        row.createCell(2).setCellValue("一个字符串");
        row.createCell(3).setCellValue(true);
        row.createCell(4).setCellValue(HSSFCell.CELL_TYPE_NUMERIC);
        row.createCell(5).setCellValue(false);
        
        FileOutputStream fileOut = new FileOutputStream("E:\\工作簿2.xls");
        wb.write(fileOut);
        fileOut.close();
    }

java操作Excel之POI(1)

 

三、遍历一个工作簿:

 1 /**
 2      * 遍历一个工作簿
 3      */
 4     public static void main(String[] args) throws Exception {
 5         InputStream is = new FileInputStream("E:\\二货.xls");
 6         POIFSFileSystem ps = new POIFSFileSystem(is);
 7         HSSFWorkbook wb = new HSSFWorkbook(ps);
 8         HSSFSheet hssfSheet = wb.getSheetAt(0);  //获取第一个sheet页
 9         if(hssfSheet == null){
10             return;
11         }
12         //遍历row
13         for(int rowNum=0; rowNum<=hssfSheet.getLastRowNum(); rowNum++){
14             HSSFRow hssfRow = hssfSheet.getRow(rowNum);
15             if(hssfRow == null){
16                 continue;
17             }
18             //遍历行
19             for(int cellNum=0; cellNum<=hssfRow.getLastCellNum(); cellNum++){
20                 HSSFCell hssfCell = hssfRow.getCell(cellNum);
21                 if(hssfCell == null){
22                     continue;
23                 }
24                 System.out.print(getValue(hssfCell) + "  ");
25             }
26             
27             System.out.println();
28         }
29         
30         is.close();
31     }
32     
33     private static String getValue(HSSFCell hssfCell){
34         if(hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
35             return String.valueOf(hssfCell.getBooleanCellValue());
36         }else if(hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
37             return String.valueOf(hssfCell.getNumericCellValue());
38         }else{
39             return String.valueOf(hssfCell.getStringCellValue());
40         }
41     }

java操作Excel之POI(1)

 

四、提取文本:

 1 /**
 2      * 提取文本
 3      */
 4     public static void main(String[] args) throws Exception {
 5         InputStream is = new FileInputStream("E:\\二货.xls");
 6         POIFSFileSystem ps = new POIFSFileSystem(is);
 7         HSSFWorkbook wb = new HSSFWorkbook(ps);
 8         
 9         ExcelExtractor excelExtractor = new ExcelExtractor(wb);
10         excelExtractor.setIncludeSheetNames(false); //不需要sheet页的名字
11         System.out.println(excelExtractor.getText());
12     }

java操作Excel之POI(1)

 

相关文章:

  • 2022-01-07
  • 2021-09-09
  • 2021-04-15
  • 2021-04-25
  • 2021-04-30
  • 2021-08-12
  • 2021-11-08
  • 2021-06-16
猜你喜欢
  • 2021-05-22
  • 2022-01-27
  • 2021-05-29
  • 2021-07-06
  • 2021-08-23
  • 2022-12-23
  • 2021-09-01
相关资源
相似解决方案