excel 2003导入升级到excel 2007

jxl不支持excel 07导入,将POI及jxls升级到较新的版本

 

1.在项目中增加已下jar包:

 

poi-ooxml-3.7.jar

poi-ooxml-schemas-3.7.jar

poi-3.7.jar(替换,将原有poi包删除)

jxls-core-1.0.jar(同上)

jxls-reader-1.0.jar(同上)

commons-jexl-2.1.1.jar(同上)

 

2.修改导入将jxl替换为poi,需使用poi的包

JXL导入
Workbook wb = Workbook.getWorkbook(in);
  Sheet sheet = wb.getSheet(sheetIndex);
  int rows = sheet.getRows(); //多少行
  int cols = sheet.getColumns(); //多少列
  String[][] content = new String[rows - begin][cols];
  for ( int i = begin; i < rows; i++) {
  for ( int j = 0 ; j < cols; j++) {
  Cell cell = sheet.getCell(j, i);
  content[i - begin][j] = cell.getContents();
  }
  }
POI导入
Workbook wb = new XSSFWorkbook(in);
  Sheet sheet = wb.getSheetAt(sheetIndex);
  int rows = sheet.getLastRowNum()+ 1 ; //多少行
  int cols = sheet.getRow(begin).getLastCellNum(); //多少列
  String[][] content = new String[rows - begin][cols];
  Cell tempCell = null ;
  for ( int i = begin; i < rows; i++) {
  Row r = sheet.getRow(i);
  for ( int j = 0 ; j < cols; j++) {
  tempCell = r.getCell(j);
  String str = "" ;
  if (tempCell.getCellType()==HSSFCell.CELL_TYPE_STRING){
  str = tempCell.getStringCellValue();
  } else if (tempCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
  str = String.valueOf(tempCell.getNumericCellValue());
  }
  content[i - begin][j] = str;
  }
  }

3.将模板导出的HSSFWorkbook workbook;改为org.apache.poi.ss.usermodel.Workbook workbook;

HSSFWorkbook workbook;
  String templateFileName = sc.getRealPath( "/" ) + templatePath;
  try {
  in = new BufferedInputStream( new FileInputStream(templateFileName));
  XLSTransformer transformer = new XLSTransformer();
  workbook = transformer.transformXLS(in, map);
  OutputStream out = response.getOutputStream();
  workbook.write(out);
  out.flush();
org.apache.poi.ss.usermodel.Workbook workbook;
  String templateFileName = sc.getRealPath( "/" ) + templatePath;
  try {
  in = new BufferedInputStream( new FileInputStream(templateFileName));
  XLSTransformer transformer = new XLSTransformer();
  workbook = transformer.transformXLS(in, map);
  OutputStream out = response.getOutputStream();
  workbook.write(out);
  out.flush();

	private static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
		if (!inp.markSupported()) {
			inp = new PushbackInputStream(inp, 8);
		}
		if (POIFSFileSystem.hasPOIFSHeader(inp)) {
			return new HSSFWorkbook(inp);
		}
		if (POIXMLDocument.hasOOXMLHeader(inp)) {
			return new XSSFWorkbook(OPCPackage.open(inp));
		}
		throw new IllegalArgumentException("你的excel版本目前poi解析不了");
	}
 

你可能感兴趣的:(Excel)