java使用poi-ooxml操作excel
2021-06-03 11:31 方斌 阅读(2514) 评论(0) 编辑 收藏 举报java使用poi-ooxml操作excel
读取excel
InputStream stream = new FileInputStream(file);
Workbook wb = new HSSFWorkbook(stream);
int sheetSize = wb.getNumberOfSheets();
List<List<List>> sheetList = new ArrayList<>();
for (int i = 0; i < sheetSize; i++) {
List<List> outerList = new ArrayList<>();
Sheet sheet = wb.getSheetAt(i);
for (int j = 0; j <= sheet.getLastRowNum(); j++) {
List<String> list = new ArrayList<>();
for (int c = 0; c < sheet.getRow(j).getLastCellNum(); c++) {
Cell cell = sheet.getRow(j).getCell(c);
String str = cell.getStringCellValue();
list.add(str);
}
outerList.add(list);
}
sheetList.add(outerList);
}
写入excel
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
for (int k = 0 ; k < 数组.size(); k++) {
HSSFRow hssfRow = sheet.createRow(k);
hssfRow.createCell(0).setCellValue("..............");
}
File jsonFile = new File("。。。。。。。。。。。");
FileOutputStream out = new FileOutputStream(jsonFile);
hssfWorkbook.write(out);
注意事项:
Excel2003版本:后缀是.xls,对应poi的HSSF系,HSSFWorkbook、HSSFSheet……
Excel2007版本:后缀是.xlsx,对应poi的XSSF系,XSSFWorkbook、XSSFSheet……