前言:
前些天遇到了這樣的一個需求,將下圖:

將表格中貨號-前面部分一致的行合成一行,並且將第二行,第三行的價格添加到第一行中為價格二,價格三。如圖:

接到這樣的需求,我的第一感覺是直接手動合並(暗暗再想這也太簡單了),然后我看了總記錄數我放棄了,決定在網上找找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("數據庫導出成功");
}
完美的解決了這個比較特殊而又不特殊的需求,代碼提供僅供互相大家學習,歡迎訪問提點不足之處。
