前言:html
前些天遇到了这样的一个需求,将下图:java
将表格中货号-前面部分一致的行合成一行,而且将第二行,第三行的价格添加到第一行中为价格二,价格三。如图:数据库
接到这样的需求,个人第一感受是直接手动合并(暗暗再想这也太简单了),而后我看了总记录数我放弃了,决定在网上找找excel的操做方法,找了一会没发现,心想不能浪费太多时间,不如本身动手丰衣足食,可能也是小弟(刚刚说老汉被批评了)比较愚昧,毕竟没怎么学过excel,望有会的大神留言,也当学习了。好了废话很少说了,接下来让咱们来看看如何实现的吧。缓存
首先想要实现此功能须要将读入excel表格,我这里使用的是HSSFWorkbook,由于用的是03版,若是想要兼容07版能够访问此博客http://www.cnblogs.com/yejg1212/p/3969822.html,我这就很少作介绍。想要读入文件咱们首先是要获得这个文件流,即:性能
InputStream is = new FileInputStream("C://jlo.xls");
而后利用HSSFWorkbook读取,首先读取sheet,找到本身想要的sheet,获取循环全部行获得每列的值,以下:学习
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); HashMap<String, String> map = new HashMap<>(); // 循环工做表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } } }
我这里为了更好的保存的,因此我建了一个实体类用于保存所得到值,写着写着忽然停了,蒙了,我该如何将货号同样的内容拼接成一个实体类中了,想了想用数据库确定不合适,太影响性能,因此机智的我选择了全局变量,相似于缓存,由于根据本excel显示规则,最多有三个会相同,而其余内容都是一致,因此只须要将相同的每一行的价格记录下来,保存到HashMap集合中,将其所有保存至最后一个实体model中,而且将其放入用于缓存的全局变量hashMap中,最后将其hashMap中全部value值便是处理后实体进行循环写入一个excel中,哇,就这么完成了。有点简单的,比在网上找excel操做而且还找不到感受要快。接下来就是读取excel的具体代码实现:excel
/** * 读取xls文件内容 * * @throws IOException * 输入/输出(i/o)异常 */ private void readXls() throws IOException { InputStream is = new FileInputStream("C://jlo.xls"); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); HashMap<String, String> map = new HashMap<>(); // 循环工做表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } XlsDto xld = new XlsDto(); xld.setSmiles(getValue(hssfRow.getCell(0))); xld.setHuoHao(getValue(hssfRow.getCell(1)).substring(0,getValue(hssfRow.getCell(1)).indexOf("-"))); xld.seteName(getValue(hssfRow.getCell(2))); xld.setcName(getValue(hssfRow.getCell(3))); xld.setCas(getValue(hssfRow.getCell(4))); xld.setHuoDate(getValue(hssfRow.getCell(5))); xld.setPurity(getValue(hssfRow.getCell(6))); xld.setKunCun(getValue(hssfRow.getCell(7))); xld.setIsCreate(getValue(hssfRow.getCell(8))); xld.setaCost(getValue(hssfRow.getCell(9))); xld.setxType(getValue(hssfRow.getCell(1)).substring(0,getValue(hssfRow.getCell(1)).indexOf("-"))); if(StringUtils.isNotBlank(getValue(hssfRow.getCell(1)))){ if(!map.containsKey(xld.getxType())){ String cost = getValue(hssfRow.getCell(9)); //insertX(xld); hashMap.put(xld.getxType(), xld); map.put(xld.getxType(), "1"); map.put(xld.getxType()+"1", cost); }else{ //String xType = getValue(hssfRow.getCell(1)).substring(0,getValue(hssfRow.getCell(1)).indexOf("-")); if("1".equals(map.get(xld.getxType()))){ String cost = getValue(hssfRow.getCell(9)); xld.setaCost(map.get(xld.getxType()+"1")); xld.setbCost(cost); hashMap.put(xld.getxType(), xld); //updateX(xType, cost,1); map.put(xld.getxType(), "2"); map.put(xld.getxType()+"2", cost); }else{ String cost = getValue(hssfRow.getCell(9)); xld.setaCost(map.get(xld.getxType()+"1")); xld.setbCost(map.get(xld.getxType()+"2")); xld.seteCost(cost); hashMap.put(xld.getxType(), xld); //updateX(xType, cost,2); map.put(xld.getxType(), "3"); } } } } } }
处理完成以后,将其再次导入给excel,实现以下:code
/** * * @param xls * XlsDto实体类的一个对象 * @throws Exception * 在导入Excel的过程当中抛出异常 */ public static void xlsDto2Excel(List<XlsDto> xls) throws Exception { // 获取总列数 int CountColumnNum = xls.size(); // 建立Excel文档 HSSFWorkbook hwb = new HSSFWorkbook(); XlsDto xlsDto = null; // sheet 对应一个工做页 HSSFSheet sheet = hwb.createSheet("sheet1"); HSSFRow firstrow = sheet.createRow(0); // // 下标为0的行开始 HSSFCell[] firstcell = new HSSFCell[CountColumnNum]; String[] names = new String[12]; names[0] = "SMILES"; names[1] = "货号"; names[2] = "产品名称(英文"; names[3] = "产品名称(中文"; names[4] = "CAS号"; names[5] = "货期(天)"; names[6] = "纯度"; names[7] = "库存"; names[8] = "是否可定制"; names[9] = "包装/价格1"; names[10] = "包装/价格2"; names[11] = "包装/价格3"; for (int j = 0; j < 12; j++) { firstcell[j] = firstrow.createCell(j); firstcell[j].setCellValue(new HSSFRichTextString(names[j])); } for (int i = 0; i < xls.size(); i++) { // 建立一行 HSSFRow row = sheet.createRow(i + 1); // 获得要插入的每一条记录 xlsDto = xls.get(i); // 在一行内循环 HSSFCell xh = row.createCell(0); xh.setCellValue(xlsDto.getSmiles()); HSSFCell xm = row.createCell(1); xm.setCellValue(xlsDto.getHuoHao()); HSSFCell yxsmc = row.createCell(2); yxsmc.setCellValue(xlsDto.geteName()); HSSFCell kcm = row.createCell(3); kcm.setCellValue(xlsDto.getcName()); HSSFCell cj = row.createCell(4); cj.setCellValue(xlsDto.getCas()); HSSFCell hd = row.createCell(5); hd.setCellValue(xlsDto.getHuoDate()); HSSFCell purity = row.createCell(6); purity.setCellValue(xlsDto.getPurity()); HSSFCell kuncun = row.createCell(7); kuncun.setCellValue(xlsDto.getKunCun()); HSSFCell isc = row.createCell(8); isc.setCellValue(xlsDto.getIsCreate()); HSSFCell ac = row.createCell(9); ac.setCellValue(xlsDto.getaCost()); HSSFCell bc = row.createCell(10); bc.setCellValue(xlsDto.getbCost()); HSSFCell ec = row.createCell(11); ec.setCellValue(xlsDto.geteCost()); } // 建立文件输出流,准备输出电子表格 OutputStream out = new FileOutputStream("C://jlol.xls"); hwb.write(out); out.close(); System.out.println("数据库导出成功"); }
完美的解决了这个比较特殊而又不特殊的需求,代码提供仅供互相你们学习,欢迎访问提点不足之处。htm