poi3.7升级版本到4.1.2,导致原先的代码报错的解决办法

POIFSFileSystem.hasPOIFSHeader()

POIXMLDocument.hasOOXMLHeader()

old:

InputStream in = null;
		try {
			in = new FileInputStream(filename);
			if (!in.markSupported()) {
	            in = new PushbackInputStream(in, 8);
	        }
	        if (POIFSFileSystem.hasPOIFSHeader(in)) {
	        	HSSFWorkbook workbook = new HSSFWorkbook(in);
				if (workbook.getNumberOfSheets()==0) {
					return;
				}
				isXlsx = false;	
				readExcelForXls(workbook, fieldList);
	        } else if (POIXMLDocument.hasOOXMLHeader(in)) {
	        	XSSFWorkbook workbook = new XSSFWorkbook(in);
				if (workbook.getNumberOfSheets()==0) {
					return;
				}
				isXlsx = true;
				readExcelForXlsx(workbook, fieldList);
	        }
		} catch (Exception ex) {
			throw new IllegalArgumentException("你的excel版本目前poi解析不了");
		} finally {
			try {
				if (in!=null){
					in.close();
				}
			} catch (IOException ex) {
				if (log.isErrorEnabled()){
					log.error("ExcelUtil:"+ex.getMessage());
				}
			}
		}

new:

InputStream in = null;
	    try {
	        in = new FileInputStream(filename);
	        Workbook workbook = WorkbookFactory.create(in);
	        if (workbook.getNumberOfSheets() == 0) {
	            return;
	        }
	        if (workbook instanceof HSSFWorkbook) {
	            isXlsx = false;
	            readExcelForXls((HSSFWorkbook) workbook, fieldList);
	        } else if (workbook instanceof XSSFWorkbook) {
	            isXlsx = true;
	            readExcelForXlsx((XSSFWorkbook) workbook, fieldList);
	        }
	    } catch (Exception ex) {
	        throw new IllegalArgumentException("你的excel版本目前poi解析不了");
	    } finally {
	        try {
	            if (in != null) {
	                in.close();
	            }
	        } catch (IOException ex) {
	            if (log.isErrorEnabled()) {
	                log.error("ExcelUtil:" + ex.getMessage());
	            }
	        }
	    }

1、HSSFCellStyle.BORDER_THIN

HSSFCellStyle.BORDER_THIN
eg:cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
BorderStyle.THIN
eg:cellStyle.setBorderBottom(BorderStyle.THIN); 

2、HSSFCellStyle.SOLID_FOREGROUND 

cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

3、HSSFColor.GREY_25_PERCENT.index

cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
 
cellStyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());

4、HSSFFont.BOLDWEIGHT_BOLD

//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setBold(true);

5、HSSFCell.CELL_TYPE_STRING

                old: 
                switch (cType) {
                    case HSSFCell.CELL_TYPE_BOOLEAN:
                        toCell.setCellValue(fromCell.getBooleanCellValue());
                        // System.out.println("--------TYPE_BOOLEAN:" +
                        // targetCell.getBooleanCellValue());
                        break;
                    case HSSFCell.CELL_TYPE_ERROR:
                        toCell.setCellErrorValue(fromCell.getErrorCellValue());
                        // System.out.println("--------TYPE_ERROR:" +
                        // targetCell.getErrorCellValue());
                        break;
                    case HSSFCell.CELL_TYPE_FORMULA:
                        toCell.setCellFormula(parseFormula(fromCell.getCellFormula()));
                        // System.out.println("--------TYPE_FORMULA:" +
                        // targetCell.getCellFormula());
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        toCell.setCellValue(fromCell.getNumericCellValue());
                        // System.out.println("--------TYPE_NUMERIC:" +
                        // targetCell.getNumericCellValue());
                        break;
                    case HSSFCell.CELL_TYPE_STRING:
                        toCell.setCellValue(fromCell.getRichStringCellValue());
                        // System.out.println("--------TYPE_STRING:" + i +
                        // targetCell.getRichStringCellValue());
                        break;
                }

                new:
                switch (cType) {
                    case BOOLEAN:
                        toCell.setCellValue(fromCell.getBooleanCellValue());
                        // System.out.println("--------TYPE_BOOLEAN:" +
                        // targetCell.getBooleanCellValue());
                        break;
                    case ERROR:
                        toCell.setCellErrorValue(fromCell.getErrorCellValue());
                        // System.out.println("--------TYPE_ERROR:" +
                        // targetCell.getErrorCellValue());
                        break;
                    case FORMULA:
                        toCell.setCellFormula(parseFormula(fromCell.getCellFormula()));
                        // System.out.println("--------TYPE_FORMULA:" +
                        // targetCell.getCellFormula());
                        break;
                    case NUMERIC:
                        toCell.setCellValue(fromCell.getNumericCellValue());
                        // System.out.println("--------TYPE_NUMERIC:" +
                        // targetCell.getNumericCellValue());
                        break;
                    case STRING:
                        toCell.setCellValue(fromCell.getRichStringCellValue());
                        // System.out.println("--------TYPE_STRING:" + i +
                        // targetCell.getRichStringCellValue());
                        break;
                }

6、HSSFCellStyle.ALIGN_CENTER

//cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平
//cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直

7、POIOLE2TextExtractor 、PowerPointExtractor、XSLFPowerPointExtractor

//POIOLE2TextExtractor oleTextExtractor =  ExtractorFactory.createExtractor(fileSystem);
POITextExtractor oleTextExtractor = ExtractorFactory.createExtractor(fileSystem);
//PowerPointExtractor powerPointExtractor = (PowerPointExtractor) oleTextExtractor;
SlideShowExtractor powerPointExtractor = (SlideShowExtractor) oleTextExtractor;
//XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(OPCPackage.open(fis));
SlideShowExtractor extractor = new SlideShowExtractor((SlideShow)fis);

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值