以前系统中有解析Excel及导出Excel操作,使用的是POI包解析上传Excel文件,再加上配置文件或有excel模板在系统中,解析上传的Excel文档时读取配置或Excel模板,看是否有必须的列项被缺省,这种方式Excel格式及样式只能写固定,或更改的话需要重新发布应用。
以前解析Excel实现方式类似这种:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.NumberFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIUtil2 {
public static String readEXCEL(String file) throws FileNotFoundException,
IOException {
StringBuilder content = new StringBuilder();
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));//创建对Excel工作簿文件的引用
for( int numSheets=0;numSheets<workbook.getNumberOfSheets();numSheets++){
if(workbook.getSheetAt(numSheets)!=null){
HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
content.append("sheetName:"+aSheet.getSheetName()+"\n");
for(Row row:aSheet){
//遍历行
for(Cell cell : row){
//遍历列
if(cell!=null && convertCell(cell).length()>0){
content.append(convertCell(cell)).append("\t");
}
}
content.append("\n");
}
}
}
return content.toString();
}
/**
* 读取xlsx文件
* @param file
* @return
* @throws InvalidFormatException
* @throws IOException
*/
public static String readEXCEL2007(String file) throws InvalidFormatException, IOException{
StringBuilder content = new StringBuilder();
OPCPackage pkg = OPCPackage.open(new File(file));
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
for(int numSheets = 0; numSheets < workbook.getNumberOfSheets();numSheets++){
if(workbook.getSheetAt(numSheets) != null){
XSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet
content.append("sheetName:"+aSheet.getSheetName()+"\n");
for(Row row : aSheet){//遍历行
for(Cell cell : row){
if(cell != null && convertCell(cell).length()>0){
content.append(convertCell(cell)).append("\t");//获得一个单元格的值
}
}
content.append("\n");
}
}
}
return content.toString();
}
private static String convertCell(Cell cell) {
// TODO Auto-generated method stub
String cellValue="";
NumberFormat formater = NumberFormat.getInstance();
formater.setGroupingUsed(false);
if(cell != null){
switch(cell.getCellType()){
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = formater.format(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue= cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue =String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
cellValue = String.valueOf(cell.getErrorCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
cellValue = cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
default:
}
}
return cellValue.trim();
}
public static void main(String[] args) throws InvalidFormatException, IOException{
String filePath = "C:\\test.xls";
String content = null;
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
if(fileName.endsWith("xlsx")){
content = readEXCEL2007(filePath);
}else if(fileName.endsWith("xls")){
content = readEXCEL(filePath);
}
System.out.println(content);
}
}
再定义一个JavaBean与读取的Excel行字段对应,如果是生成Excel文档的话和上面方式类似,但是有一个问题,如果是JavaBean和Excel行对应,若JavaBean中有字段更改或顺序调整,可能会导致生成Excel行中cell顺序发生变化。
最近在做Excel操作时,使用了easy-excel开源框架,简化了解析及生成Excel操作,且Excel表格样式、字段等可动态在XML文件里修改配置即可,不用重新发布只替换XML文件。使用easy-excel方式如下:
https://www.cnblogs.com/plan123/p/5623201.html