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);