Excel 处理 poi


        
            org.apache.poi
            poi
            3.14
        
        
        
            org.apache.poi
            poi-ooxml
            3.14
        
  1. 读取
    public List getSheets(String key) {
        try {
                Workbook workbook = WorkbookFactory.create(object.getObjectContent());
                Sheet sheet = workbook.getSheetAt(0);
                int idex;
                for (idex = 0;idex <= sheet.getLastRowNum(); idex++){
                    Row row = sheet.getRow(idex);
                    Cell a = row.getCell(0);
                    Cell b = row.getCell(1);
                
                    if (a.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        outParam.seta(new DecimalFormat("#").format(a.getNumericCellValue()));
                    } else {
                        outParam.seta(a.getStringCellValue());
                    }
                    if (b.getCellType() == Cell.CELL_TYPE_BLANK) {
                        outParam.setb("0");
                    } else {
                        if (b.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                            outParam.setb(new DecimalFormat("#").format(b.getNumericCellValue()));
                        } else {
                            outParam.setb(b.getStringCellValue());
                        }
                    }
                    if (idex >= 1) {
                        ParamList.add(outParam);
                    }
                    retrun null;
    }
  1. 写EXCEL
    /**
    • 生成创意xls表
      */
      private XSSFWorkbook getSheets(List List) {
      // 第一步,创建一个webbook,对应一个Excel文件
      XSSFWorkbook wb = new XSSFWorkbook();
      // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
      XSSFSheet sheet = wb.createSheet("report");
      // 第三步,在sheet中添加表头第0行
      XSSFRow row = sheet.createRow(0);
      // 第四步,创建单元格,并设置值表头 设置表头居中
      XSSFCellStyle style = wb.createCellStyle();
      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

      XSSFCell cell = row.createCell(0);
      cell.setCellValue("老师");
      cell.setCellStyle(style);
      cell = row.createCell(1);
      cell.setCellValue("工号");
      cell.setCellStyle(style);
      // 第五步,写入实体数据
      int rowIndex = 1;
      for (Param DTO : List) {
      row = sheet.createRow(rowIndex++);
      row.createCell(0).setCellValue(DTO.getName());
      row.createCell(1).setCellValue(DTO.getId());
      }
      return wb;
      }

你可能感兴趣的:(Excel 处理 poi)