1. import org.apache.commons.lang.StringUtils;
  2. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  3. import org.apache.poi.ss.usermodel.*;
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  5.  
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.HashSet;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16.  
  17. /**
  18. * POI解析Excel
  19. */
  20. public class ExcelReaderUtil {
  21. /**
  22. * 根据fileType不同读取excel文件
  23. *
  24. * @param path
  25. * @param path
  26. * @throws IOException
  27. */
  28. public static List<List<String>> readExcel(String path) {
  29. String fileType = path.substring(path.lastIndexOf(".") + 1);
  30. // return a list contains many list
  31. List<List<String>> lists = new ArrayList<List<String>>();
  32. // 读取excel文件
  33. InputStream is = null;
  34. try {
  35. is = new FileInputStream(path);
  36. // 获取工作薄
  37. Workbook wb = null;
  38. if (fileType.equals("xls")) {
  39. wb = new HSSFWorkbook(is);
  40. } else if (fileType.equals("xlsx")) {
  41. wb = new XSSFWorkbook(is);
  42. } else {
  43. return null;
  44. }
  45.  
  46. // 读取第一个工作页sheet
  47. Sheet sheet = wb.getSheetAt(0);
  48. // 第一行为标题
  49. for (Row row : sheet) {
  50. ArrayList<String> list = new ArrayList<String>();
  51. for (Cell cell : row) {
  52. // 根据不同类型转化成字符串
  53. cell.setCellType(Cell.CELL_TYPE_STRING);
  54. list.add(cell.getStringCellValue());
  55. }
  56. lists.add(list);
  57. }
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. } finally {
  61. try {
  62. if (is != null)
  63. is.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. return lists;
  69. }
  70.  
  71. /**
  72. * 创建Excel.xls
  73. *
  74. * @param lists
  75. * 需要写入xls的数据
  76. * @param titles
  77. * 列标题
  78. * @param name
  79. * 文件名
  80. * @return
  81. * @throws IOException
  82. */
  83. public static Workbook creatExcel(List<List<String>> lists,String[] titles, String name) throws IOException {
  84. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
  85. String nowDate = df.format(new Date());
  86. // System.out.println(lists);
  87. // 创建新的工作薄
  88. Workbook wb = new HSSFWorkbook();
  89. // 创建第一个sheet(页),并命名
  90. Sheet sheet = wb.createSheet(name);
  91. // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。
  92. for (int i = 0; i < titles.length; i++) {
  93. sheet.setColumnWidth((short) i, (short) (35.7 * 150));
  94. }
  95.  
  96. // 创建第一行
  97. Row row = sheet.createRow((short) 0);
  98.  
  99. // 创建两种单元格格式
  100. CellStyle cs = wb.createCellStyle();
  101. CellStyle cs2 = wb.createCellStyle();
  102.  
  103. // 创建两种字体
  104. Font f = wb.createFont();
  105. Font f2 = wb.createFont();
  106.  
  107. // 创建第一种字体样式(用于列名)
  108. f.setFontHeightInPoints((short) 10);
  109. f.setColor(IndexedColors.BLACK.getIndex());
  110. f.setBoldweight(Font.BOLDWEIGHT_BOLD);
  111.  
  112. // 创建第二种字体样式(用于值)
  113. f2.setFontHeightInPoints((short) 10);
  114. f2.setColor(IndexedColors.BLACK.getIndex());
  115.  
  116. // 设置第一种单元格的样式(用于列名)
  117. cs.setFont(f);
  118. cs.setBorderLeft(CellStyle.BORDER_THIN);
  119. cs.setBorderRight(CellStyle.BORDER_THIN);
  120. cs.setBorderTop(CellStyle.BORDER_THIN);
  121. cs.setBorderBottom(CellStyle.BORDER_THIN);
  122. cs.setAlignment(CellStyle.ALIGN_CENTER);
  123.  
  124. // 设置第二种单元格的样式(用于值)
  125. cs2.setFont(f2);
  126. cs2.setBorderLeft(CellStyle.BORDER_THIN);
  127. cs2.setBorderRight(CellStyle.BORDER_THIN);
  128. cs2.setBorderTop(CellStyle.BORDER_THIN);
  129. cs2.setBorderBottom(CellStyle.BORDER_THIN);
  130. cs2.setAlignment(CellStyle.ALIGN_CENTER);
  131. // 设置列名
  132. for (int i = 0; i < titles.length; i++) {
  133. Cell cell = row.createCell(i);
  134. cell.setCellValue(titles[i]);
  135. cell.setCellStyle(cs);
  136. }
  137. if (lists == null || lists.size() == 0) {
  138. return wb;
  139. }
  140. // 设置每行每列的值
  141. for (short i = 1; i <= lists.size(); i++) {
  142. // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的
  143. // 创建一行,在页sheet上
  144. Row row1 = sheet.createRow((short) i);
  145. for (short j = 0; j < titles.length; j++) {
  146. // 在row行上创建一个方格
  147. Cell cell = row1.createCell(j);
  148. cell.setCellValue(lists.get(i - 1).get(j));
  149. cell.setCellStyle(cs2);
  150. }
  151. // 输出文件
  152. FileOutputStream fileOut = new FileOutputStream("d:/"+nowDate+"处理后的单位名称.xls");
  153. wb.write(fileOut);
  154. fileOut.close();
  155. }
  156. return wb;
  157. }
  158.  
  159. private static List<List<String>> transposeStr(List<List<String>> resources) {
  160. List<List<String>> converts = new LinkedList<List<String>>();
  161. for (List<String> list : resources) {
  162. for (String str : list) {
  163. if(StringUtils.isNotBlank(str)&&str.indexOf("&nbsp;")==-1){//剔除空串,同时搜索字符"&nbsp;",若不存在
  164. List<String> newList = new LinkedList<String>();
  165. newList.add(str);
  166. converts.add(newList);
  167. }else if(str.indexOf("&nbsp;")!=-1){//搜索字符"&nbsp;",若存在
  168. String strSearch=str.replace("&nbsp;","");//剔除子串"&nbsp;"
  169. String [] strSearchls=strSearch.split(",");//分割字符串
  170. for(String str2 : strSearchls){
  171. List<String> newList = new LinkedList<String>();
  172. newList.add(str2);
  173. converts.add(newList);
  174. }
  175. }
  176. }
  177. }
  178. return converts;
  179. }
  180.  
  181. public static void main(String[] args) {
  182. //输出A在B中的内容
  183. String pathA = "d:/提案查询(流程).xlsx";//提案查询(流程).xlsx
  184. String pathB = "d:/单位名称查询.xlsx";//单位名称查询.xlsx
  185. List<List<String>> listA = readExcel(pathA);
  186. List<List<String>> listsA=transposeStr(listA);//listsA为去除空串和&nbsp;后的list<string>
  187. // System.out.println("listsA:"+listsA);
  188. ArrayList<List<String>> listsNewA=new ArrayList<List<String>>(new HashSet<List<String>>(listsA));//去重
  189. // System.out.println("listsNewA:"+listsNewA);
  190.  
  191. List<List<String>> listB = readExcel(pathB);
  192. // System.out.println("listB:"+listB);
  193. listB.retainAll(listsNewA);
  194. // System.out.println(listB.size()+"处理后"+listB);
  195. String[] titles={"处理后的单位名称"};
  196. String name="处理结果";
  197. try {
  198. creatExcel(listB,titles,name);
  199. } catch (IOException e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. }
  203. /*
  204. * for (List<String> list : lists) { for (String strs : list) {
  205. * System.out.println(strs); } }
  206. */
  207. }
  208. }

excel合并的更多相关文章

  1. asp.net C#取Excel 合并单元格内容

    asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...

  2. NPOI之Excel——合并单元格、设置样式、输入公式

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

  3. 【NetOffice Excel】Excel合并单元格【原】

    CSharp操作Excel采用开源的原生.NET程序集NetOffice,格式兼容性更好. 在操作Excel的时候有时候需要合并单元格 using ExcelOffice = NetOffice.Ex ...

  4. 利用jxl读取excel合并的单元格的一个小样例

    工作中我们可能要把Excel文件的记录保存到数据库, 今天我用jxl读取Excel文件时遇到了合并格的问题,记录例如以下: 如Excel文件例如以下: watermark/2/text/aHR0cDo ...

  5. 使用python将多个excel合并

    最近看视频学习,老师布置了个作业,关于如何使用python将多个excel进行合并,老师写的代码我感觉比较复杂,下面是我自己改良之后较简单的方式. 实现这个功能主要有两种方法,一种是用xlwd,xls ...

  6. 让我头疼一下午的Excel合并单元格

    Excel导出常见问题 excel导出其实不算什么难事 在网上copy下模板代码,填充自己的业务数据,提供一个http接口基本就可以得到你要导出的数据了. 但是,凡事都有例外,截止今天,excel导出 ...

  7. NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等

    首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...

  8. poi excel 合并单元格

    结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,        colId, colId + c ...

  9. POI Excel 合并数据相同的行

    import java.io.Serializable; /** * POI Excel报表导出,列合并实体<br> * * @author WQ * */ public class Po ...

  10. poi导出excel合并单元格(包括列合并、行合并)

    1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-2012 ...

随机推荐

  1. Java 8 新特性7-方法引用、继承

    (原) 方法引用: 方法引用有4种: 1.静态方法引用:类名::静态方法名 在java中,对集合的排序,我们常用java提供的 Collections.sort(List<T> list, ...

  2. dotnetcore ueditor

    dotnetcore ueditor  https://github.com/durow/ueditornetcore http://www.cnblogs.com/durow/p/6116393.h ...

  3. 25 python 初学(socket,socketserver)

    参考blog :www.cnblogs.com/yuanchenqi/articles/5692716.html 1. sk = socket.socket() 里面有两个重要的参数,family 和 ...

  4. zabbix源码安装 令人窒息的操作

    一.简介 zabbix-server主要分为2部分: zabbix程序 程序根据客户端的监控项,从客户端获取数据并写入到数据库,再根据触发器/动作等配置进行操作. 展示页面 使用php编写,php脚本 ...

  5. Strem_01

    import 'package:flutter/material.dart';import 'dart:async';import 'dart:ui'; void main()=>runApp( ...

  6. Elastic Stack-Elasticsearch介绍

    一.前言     前篇写了好像没有多少人去看,但是还是要继续,我猜想可能是很多人接触的这块比较少吧,Elasticsearch这块有很多要说的,开始吧. 二.数据库.Elasticsearch选择   ...

  7. 对List集合嵌套了map集合对double值进行排序

    /*[ { "repairo": "asda", "num": 88.71 }, { "repairo": " ...

  8. js05-DOM对象二

    一.节点操作 创建节点:var ele_a = document.createElement('a');添加节点:ele_parent.appendChild(ele_img);删除节点:ele_pa ...

  9. Linux(Ubuntu 16) 下Java开发环境的配置(三)------Mysql配置

    前言 吐槽一句,如果在Ubuntu在默认情况下是只有最新的MySQL源的,即如果使用"sudo apt-get install mysql-server mysql-client " ...

  10. Shell命令-文件及目录操作之cp、find

    文件及目录操作 - cp.find 1.cp:复制文件或目录 cp命令的功能说明 cp命令用于复制文件或目录. cp命令的语法格式 cp [OPTION]... SOURCE... DIRECTORY ...