JAVA程序实现导入导出数据到给定的EXCEL文件模板中
首先创建一个maven项目,在pom.xml文件中引入需要的包(这里3.14版本的poi会有一些问题,在导入07版本的excel时xmlbeans使用2.6.0及以上版本):
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency>
准备好需要导入的excel模板文件:D://a.xlxs 。
关键代码: //把查询出的数据导出到给定的excel模板中 public byte[] exportExcel(int startIndex, String[] keys, Map<String, Object> data){ //获取到模板文件 File excel = PropertiesUtil.getFile("D://a.xlxs"); FileInputStream fis = new FileInputStream(excel); //创建excel XSSFWorkbook book = new XSSFWorkbook(fis); //keys是对应的导出数据字段,map中的key列表 for(int i=0; i<keys.length; k++){ String[] key = keys[i]; XSSFSheet sheet = book.getSheetAt(i); List<Map<String, Object>> list = data.get("list"+i); for(int j=0; j<list.size(); k++){ Row row = sheet.createRow(j+startIndex); Map<String, Object> map = list.get(j); for(int k=0; k<key.length; k++){ String s = map.get(key[k])+""; row.createCell(k).setCellValue(s); } } } } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wbs.write(os); } catch (IOException e) { e.printStackTrace(); } return os.toByteArray(); }
文件导入 public Map<String, Object> importSeries(InputStream is, String[] keys, int startIndex) throws Exception{ Map<String, Object> map = new HashMap<String, Object>(); Workbook book = null; try { book = new XSSFWorkbook(is); } catch (IOException e) { book = new HSSFWorkbook(is); } for(int i=0; i<keys.length; i++){ List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); String[] key = keys[i]; Sheet sheet = book.getSheetAt(i); int totalRows = sheet.getLastRowNum(); for(int j = 0; j<totalRows; j++){ Row row = sheet.getRow(startIndex+j); if(row == null){ continue; } int totalCells = row.getLastCellNum(); Map<String, Object> map1 = new HashMap<String, Object>(); for(int k=0; k<totalCells; k++){ Cell cell = row.getCell(k); if(cell!=null){ map1.put(key[k], row.getCell(k).getStringCellValue()); } } list.add(map1); } map.put("list"+i, list); } return map; }
导出到文件 private static void exportExcel(List<ZSMM_SRM_MAT_SYNC> list) throws IOException{ List<ZSMM_SRM_MAT_SYNC> list0 = list.subList(0, 20000); List<ZSMM_SRM_MAT_SYNC> list1 = list.subList(20000, 40000); List<ZSMM_SRM_MAT_SYNC> list2 = list.subList(40000, list.size()); List<Object> listx = new ArrayList<Object>(); listx.add(list0); listx.add(list1); listx.add(list2); for(int j=0; j<3; j++){ System.out.println(j); @SuppressWarnings("unchecked") List<ZSMM_SRM_MAT_SYNC> lists= (List<ZSMM_SRM_MAT_SYNC>) listx.get(j); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("物料主数据"+j); sheet.setDefaultColumnWidth(20); //XSSFCellStyle style = wb.createCellStyle(); XSSFRow row = sheet.createRow(0); // style.setAlignment(XSSFCellStyle.ALIGN_CENTER); XSSFCell cell = row.createCell(0); cell.setCellValue("物料号"); cell = row.createCell(1); cell.setCellValue("物料描述"); cell = row.createCell(2); cell.setCellValue("创建日期"); cell = row.createCell(3); cell.setCellValue("删除标识符"); cell = row.createCell(4); cell.setCellValue("跨工厂物料状态 "); cell = row.createCell(5); cell.setCellValue("物料类型"); cell = row.createCell(6); cell.setCellValue("物料组"); cell = row.createCell(7); cell.setCellValue("外部物料组"); cell = row.createCell(8); cell.setCellValue("基本计量单位"); cell = row.createCell(9); cell.setCellValue("采购订单的计量单位 "); cell = row.createCell(10); cell.setCellValue("实验室/设计室 "); cell = row.createCell(11); cell.setCellValue("毛重 "); cell = row.createCell(12); cell.setCellValue("净重 "); cell = row.createCell(13); cell.setCellValue("重量单位 "); cell = row.createCell(14); cell.setCellValue("业务量 "); cell = row.createCell(15); cell.setCellValue("体积单位 "); cell = row.createCell(16); cell.setCellValue("长 "); cell = row.createCell(17); cell.setCellValue("宽 "); cell = row.createCell(18); cell.setCellValue("高"); cell = row.createCell(19); cell.setCellValue("物料描述-英 "); for (int i = 0; i < lists.size(); i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(lists.get(i).getMATNR()); row.createCell(1).setCellValue(lists.get(i).getMAKTX()); row.createCell(2).setCellValue(lists.get(i).getERSDA()+""); row.createCell(3).setCellValue(lists.get(i).getLVORM()); row.createCell(4).setCellValue(lists.get(i).getMSTAE()); row.createCell(5).setCellValue(lists.get(i).getMTART()); row.createCell(6).setCellValue(lists.get(i).getMATKL()); row.createCell(7).setCellValue(lists.get(i).getEXTWG()); row.createCell(8).setCellValue(lists.get(i).getMEINS()); row.createCell(9).setCellValue(lists.get(i).getBSTME()); row.createCell(10).setCellValue(lists.get(i).getLABOR()); row.createCell(11).setCellValue(lists.get(i).getBRGEW()+""); row.createCell(12).setCellValue(lists.get(i).getNTGEW()+""); row.createCell(13).setCellValue(lists.get(i).getGEWEI()); row.createCell(14).setCellValue(lists.get(i).getVOLUM()+""); row.createCell(15).setCellValue(lists.get(i).getVOLEH()); row.createCell(16).setCellValue(lists.get(i).getLENGTH()); row.createCell(17).setCellValue(lists.get(i).getWIDTH()); row.createCell(18).setCellValue(lists.get(i).getHEIGHT()); row.createCell(19).setCellValue(lists.get(i).getMAKTX_E()); } FileOutputStream out = new FileOutputStream("E://物料主数据"+j+".xlsx"); wb.write(out); out.close(); System.out.println("ok"+j); }
Java导入导出数据到给定多sheet的excel文件
猜你喜欢
转载自blog.csdn.net/weixin_42171657/article/details/82772479
今日推荐
周排行