直接上代码
public static void main(String[] args) throws IOException {
List<List<String>>list= new ArrayList<>();
//System.out.println(getZipFiles1().length);
try (ZipInputStream in = new ZipInputStream(
new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.zip")
, Charset.forName("gbk"))) {
ZipEntry zip=null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//获取压缩文件中的每个子文件
while((zip=in.getNextEntry())!=null) {
if (zip.isDirectory()) {
//如果是目录,不处理
continue;
}
String zipFileName = zip.getName();
System.out.println(zip.getName());
if (zipFileName != null && zipFileName.indexOf(".") != -1 && (zipFileName.toUpperCase().endsWith("XLSX") ||zipFileName.toUpperCase().endsWith("XLS") )) {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
InputStream stream1 = new ByteArrayInputStream(baos.toByteArray()); //excel 流
//读取EXCEL中的值
List<List<String>> list1 =readExcelInfo(stream1, zipFileName) ;
if(list1 != null){
list.addAll(list1);
}
baos.reset();
continue;
}
}
System.out.println(list);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static List<List<String>> readExcelInfo(InputStream is, String fileName) throws Exception {
Workbook workbook = getWorkBook(is, fileName);
int sheetNum = workbook.getNumberOfSheets();
List<List<String>> dataList = new ArrayList<List<String>>();
for (int index = 0; index < sheetNum; index++) {
Sheet sheet = workbook.getSheetAt(index);
if (sheet == null) {
continue;
}
Row row1 = sheet.getRow(0);
// sheet.getRow(0).getCell(1);
short firstNum = row1.getLastCellNum();
for (int rowIndex = 3; rowIndex <= sheet.getLastRowNum(); rowIndex++) { //默认从第二行开始读取
Row row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
List<String> cellList = new ArrayList<String>();
for (int cellIndex = 0; cellIndex < firstNum; cellIndex++) {
Cell cell = row.getCell(cellIndex);
if (cell != null) {
String v= getCellValue(cell);
// a== - 时 跳过
if(v != null &&!"-" .equals(v.trim())){
cellList.add(v);
}
}else{
cellList.add("");
}
}
//每一条数据的orgcode
cellList.add(fileName.substring(1+fileName.lastIndexOf("/"),fileName.lastIndexOf(".")));
if(!cellList.isEmpty()){
dataList.add(cellList);
}
}
}
is.close();
// System.out.println(dataList);
return dataList;
}
public static Workbook getWorkBook(InputStream is, String fileName) throws Exception {
Workbook workbook = null;
if (fileName.toUpperCase().endsWith("XLS")) {
workbook = new HSSFWorkbook(is);
} else if (fileName.toUpperCase().endsWith("XLSX")) {
workbook = new XSSFWorkbook(is);
}
return workbook;
}
public static String getCellValue(Cell cell) {
CellType cellType = cell.getCellTypeEnum();
String cellValue = "";
if (cell == null || cell.toString().trim().equals("")) {
return null;
}
if (cellType == CellType.STRING) {
cellValue = cell.getStringCellValue().trim();
return cellValue = StringUtil.isEmpty(cellValue) ? "" : cellValue;
}
if (cellType == CellType.NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) { //判断日期类型
cellValue = String.valueOf(cell.getDateCellValue().getTime());
} else { //否
cellValue = new DecimalFormat("#.######").format(cell.getNumericCellValue());
}
return cellValue;
}
if (cellType == CellType.BOOLEAN) {
cellValue = String.valueOf(cell.getBooleanCellValue());
return cellValue;
}
return null;
}