excel表格的特殊需求引發的Java思考,excel表格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的具體代碼實現:
/** * 讀取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,實現如下:
/** * * @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("資料庫匯出成功"); }
完美的解決了這個比較特殊而又不特殊的需求,代碼提供僅供互相大家學習,歡迎訪問提點不足之處。