packagecom.gavin.operational.excle;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.DateUtil;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.xssf.usermodel.XSSFCell;importorg.apache.poi.xssf.usermodel.XSSFRow;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classReadExcelXlsx {public static void main(String[] args) throwsException{//读取xlsx
Map> map = readXlsx("E:/测试读取XLSX.xlsx");for(int n=0;n
List list =map.get(n);
System.out.println("-------------------------sheet"+n+"--------------------------------");for(int i=0;i
String[] arr=(String[]) list.get(i);for(int j=0;j
System.out.print(arr[j]);elseSystem.out.print(arr[j]+"|");
}
System.out.println();
}
}//写入xlsx
writeXlsx("E:/测试写入XLSX.xlsx",map);
}//读取Xlsx
public static Map>readXlsx(String fileName) {
Map> map = new HashMap>();try{
InputStream is= newFileInputStream(fileName);
XSSFWorkbook workbook= newXSSFWorkbook(is);//循环工作表Sheet
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet=workbook.getSheetAt(numSheet);if (xssfSheet == null) {continue;
}
List list = new ArrayList();for (int row=0;row<=xssfSheet.getLastRowNum();row++){
XSSFRow xssfRow=xssfSheet.getRow(row);if (xssfRow == null) {continue;
}
String[] singleRow= newString[xssfRow.getLastCellNum()];for(int column=0;column
Cell cell=xssfRow.getCell(column,Row.CREATE_NULL_AS_BLANK);switch(cell.getCellType()){caseCell.CELL_TYPE_BLANK:
singleRow[column]= "";break;caseCell.CELL_TYPE_BOOLEAN:
singleRow[column]=Boolean.toString(cell.getBooleanCellValue());break;caseCell.CELL_TYPE_ERROR:
singleRow[column]= "";break;caseCell.CELL_TYPE_FORMULA:
cell.setCellType(Cell.CELL_TYPE_STRING);
singleRow[column]=cell.getStringCellValue();if (singleRow[column] != null) {
singleRow[column]= singleRow[column].replaceAll("#N/A", "").trim();
}break;caseCell.CELL_TYPE_NUMERIC:if(DateUtil.isCellDateFormatted(cell)) {
singleRow[column]=String.valueOf(cell.getDateCellValue());
}else{
cell.setCellType(Cell.CELL_TYPE_STRING);
String temp=cell.getStringCellValue();//判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串
if (temp.indexOf(".") > -1) {
singleRow[column]= String.valueOf(newDouble(temp)).trim();
}else{
singleRow[column]=temp.trim();
}
}break;caseCell.CELL_TYPE_STRING:
singleRow[column]=cell.getStringCellValue().trim();break;default:
singleRow[column]= "";break;
}
}
list.add(singleRow);
}
map.put(numSheet, list);
}
}catch(FileNotFoundException e) {//TODO 自动生成的 catch 块
e.printStackTrace();
}catch(IOException e) {//TODO 自动生成的 catch 块
e.printStackTrace();
}returnmap;
}//写入Xlsx
public static void writeXlsx(String fileName,Map>map) {try{
XSSFWorkbook wb= newXSSFWorkbook();for(int sheetnum=0;sheetnum
XSSFSheet sheet= wb.createSheet(""+sheetnum);
List list =map.get(sheetnum);for(int i=0;i
XSSFRow row=sheet.createRow(i);
String[] str=list.get(i);for(int j=0;j
XSSFCell cell=row.createCell(j);
cell.setCellValue(str[j]);
}
}
}
FileOutputStream outputStream= newFileOutputStream(fileName);
wb.write(outputStream);
outputStream.close();
}catch(FileNotFoundException e) {//TODO 自动生成的 catch 块
e.printStackTrace();
}catch(IOException e) {//TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}