前言:
前些天遇到了这样的一个需求,将下图:

将表格中货号-前面部分一致的行合成一行,并且将第二行,第三行的价格添加到第一行中为价格二,价格三。如图:

接到这样的需求,我的第一感觉是直接手动合并(暗暗再想这也太简单了),然后我看了总记录数我放弃了,决定在网上找找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("数据库导出成功");
}
完美的解决了这个比较特殊而又不特殊的需求,代码提供仅供互相大家学习,欢迎访问提点不足之处。