Override public int exportConfirmContent(String entityId, String path) throws ApiException, IOException { String content="0";//判断是什么类型的excel if (StringUtils.isNotBlank(path)) { String postfix=path.substring(path.lastIndexOf(".") + 1, path.length()); //2003 excel if ("xls".equals(postfix)) { content=readXls(path,entityId); } else if ("xlsx".equals(postfix)) {//2010 excel content=readXlsx(path,entityId); } }else { return content; } return content; } /** * 读2003excel * @param path excel文件路径 * @param entityId 学校id * @return */ public String readXls(String path,String entityId) throws IOException, ApiException{ InputStream is = new FileInputStream(path); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); String contentIds=""; // Read the Sheet for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { //从第一个sheet开始遍历,为空直接下一个 XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // Read the Row for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null && rowNum!=1 ) { XSSFCell no = xssfRow.getCell(0); contentIds += no.getStringCellValue().toString()+","; } } } return contentIds ; } /** * 读2010excel * @param path * @param entityId * @return * @throws ApiException * @throws */ public String readXlsx(String path,String entityId) throws ApiException, IOException{ InputStream is = new FileInputStream(path); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); String contentIds=""; // Read the Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // Read the Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null && rowNum!=1) { //第rownum行的第一个单元格 HSSFCell no = hssfRow.getCell(0); contentIds+= no.getStringCellValue().toString()+","; } } } return contentIds; }
代码没啥,具体可以参考poi文档,读取,和写入类似,只是get和set方法设置cell内容。但是写入涉及到一些cell样式,可以自行设置。这里这是提供一个工具类进行excel的读取写入数据库。
java:poi读取excel文件,分xls和xlsx
最新推荐文章于 2023-10-07 17:13:26 发布