ExcelUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.ruoyi.common.utils.file;
  2. import org.apache.commons.lang.time.DateFormatUtils;
  3. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  4. import org.apache.poi.ss.usermodel.DateUtil;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.apache.poi.ss.usermodel.*;
  7. import org.apache.poi.ss.util.CellRangeAddress;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.math.BigDecimal;
  15. import java.text.DecimalFormat;
  16. import java.text.NumberFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. /**
  22. * Created by Administrator on 2018/7/26 0026.
  23. */
  24. public class ExcelUtils {
  25. public static Workbook getWorkBook(MultipartFile file) {
  26. //获得文件名
  27. String fileName = file.getOriginalFilename();
  28. //创建Workbook工作薄对象,表示整个excel
  29. Workbook workbook = null;
  30. try {
  31. //获取excel文件的io流
  32. InputStream is = file.getInputStream();
  33. //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
  34. if (fileName.endsWith("xls")) {
  35. //2003
  36. workbook = new HSSFWorkbook(is);
  37. } else if (fileName.endsWith("xlsx")) {
  38. //2007 及2007以上
  39. workbook = new XSSFWorkbook(is);
  40. }
  41. } catch (IOException e) {
  42. // log.error(e.getMessage());
  43. }
  44. return workbook;
  45. }
  46. public static Workbook getWorkBook(File file) {
  47. //获得文件名
  48. String fileName = file.getName();
  49. //创建Workbook工作薄对象,表示整个excel
  50. Workbook workbook = null;
  51. try {
  52. //获取excel文件的io流
  53. InputStream is =new FileInputStream(file);
  54. //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
  55. if (fileName.endsWith("xls")) {
  56. //2003
  57. workbook = new HSSFWorkbook(is);
  58. } else if (fileName.endsWith("xlsx")) {
  59. //2007 及2007以上
  60. workbook = new XSSFWorkbook(is);
  61. }
  62. } catch (IOException e) {
  63. // log.error(e.getMessage());
  64. }
  65. return workbook;
  66. }
  67. public static String getCellValue(Cell cell) {
  68. String cellValue = "";
  69. if (cell == null) {
  70. return cellValue;
  71. }
  72. System.out.println("Excel格式:"+ cell.getCellType().name());
  73. switch (cell.getCellType().name()) {
  74. case "STRING":
  75. cellValue = cell.getRichStringCellValue().getString();
  76. break;
  77. case "NUMERIC":
  78. System.out.println("Excel数据样式:"+ cell.getCellStyle().getDataFormatString());
  79. if("General".equals(cell.getCellStyle().getDataFormatString())){
  80. cellValue = new BigDecimal(Double.toString(cell.getNumericCellValue())).toPlainString();
  81. }else if("m/d/yyyy;@".equals(cell.getCellStyle().getDataFormatString())){
  82. cellValue = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  83. }else if("@".equals(cell.getCellStyle().getDataFormatString())){
  84. cellValue = cell.getNumericCellValue() + "";
  85. }else{
  86. try {
  87. cellValue = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  88. }catch (Exception e){
  89. cellValue = cell.getNumericCellValue() + "";
  90. }
  91. }
  92. break;
  93. case "BLANK":
  94. cellValue = "";
  95. break;
  96. default:
  97. cellValue = cell.toString();
  98. break;
  99. }
  100. //判断数据的类型
  101. // switch (cell.getCellType()) {
  102. // case Cell.CELL_TYPE_NUMERIC: //数字
  103. // cellValue = stringDateProcess(cell);
  104. // break;
  105. // case Cell.CELL_TYPE_STRING: //字符串
  106. // cellValue = String.valueOf(cell.getStringCellValue());
  107. // break;
  108. // case Cell.CELL_TYPE_BOOLEAN: //Boolean
  109. // cellValue = String.valueOf(cell.getBooleanCellValue());
  110. // break;
  111. // case Cell.CELL_TYPE_FORMULA: //公式
  112. // cellValue = String.valueOf(cell.getCellFormula());
  113. // break;
  114. // case Cell.CELL_TYPE_BLANK: //空值
  115. // cellValue = "";
  116. // break;
  117. // case Cell.CELL_TYPE_ERROR: //故障
  118. // cellValue = "非法字符";
  119. // break;
  120. // default:
  121. // cellValue = "未知类型";
  122. // break;
  123. // }
  124. // cellValue = String.valueOf(cell.getStringCellValue());
  125. return cellValue;
  126. }
  127. /**
  128. * 时间格式处理
  129. *
  130. * @return
  131. * @author Liu Xin Nan
  132. * @data 2017年11月27日
  133. */
  134. public static String stringDateProcess(Cell cell) {
  135. String result = new String();
  136. if (DateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
  137. SimpleDateFormat sdf = null;
  138. if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
  139. sdf = new SimpleDateFormat("HH:mm");
  140. } else {// 日期
  141. sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  142. }
  143. Date date = cell.getDateCellValue();
  144. result = sdf.format(date);
  145. } else if (cell.getCellStyle().getDataFormat() == 58) {
  146. // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
  147. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  148. double value = cell.getNumericCellValue();
  149. Date date = DateUtil
  150. .getJavaDate(value);
  151. result = sdf.format(date);
  152. } else {
  153. double value = cell.getNumericCellValue();
  154. CellStyle style = cell.getCellStyle();
  155. DecimalFormat format = new DecimalFormat();
  156. String temp = style.getDataFormatString();
  157. // 单元格设置成常规
  158. if (temp.equals("General")) {
  159. format.applyPattern("#");
  160. }
  161. result = format.format(value);
  162. }
  163. return result;
  164. }
  165. /**
  166. * 合并单元格处理,获取合并行
  167. *
  168. * @param sheet
  169. * @return List<CellRangeAddress>
  170. */
  171. public static List<CellRangeAddress> getCombineCell(Sheet sheet) {
  172. List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
  173. //获得一个 sheet 中合并单元格的数量
  174. int sheetmergerCount = sheet.getNumMergedRegions();
  175. //遍历所有的合并单元格
  176. for (int i = 0; i < sheetmergerCount; i++) {
  177. //获得合并单元格保存进list中
  178. CellRangeAddress ca = sheet.getMergedRegion(i);
  179. list.add(ca);
  180. }
  181. return list;
  182. }
  183. public static int getRowNum(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet) {
  184. int xr = 0;
  185. int firstC = 0;
  186. int lastC = 0;
  187. int firstR = 0;
  188. int lastR = 0;
  189. for (CellRangeAddress ca : listCombineCell) {
  190. //获得合并单元格的起始行, 结束行, 起始列, 结束列
  191. firstC = ca.getFirstColumn();
  192. lastC = ca.getLastColumn();
  193. firstR = ca.getFirstRow();
  194. lastR = ca.getLastRow();
  195. if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
  196. if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
  197. xr = lastR;
  198. }
  199. }
  200. }
  201. return xr;
  202. }
  203. /**
  204. * 判断单元格是否为合并单元格,是的话则将单元格的值返回
  205. *
  206. * @param listCombineCell 存放合并单元格的list
  207. * @param cell 需要判断的单元格
  208. * @param sheet sheet
  209. * @return
  210. */
  211. public static String isCombineCell(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet)
  212. throws Exception {
  213. int firstC = 0;
  214. int lastC = 0;
  215. int firstR = 0;
  216. int lastR = 0;
  217. String cellValue = null;
  218. for (CellRangeAddress ca : listCombineCell) {
  219. //获得合并单元格的起始行, 结束行, 起始列, 结束列
  220. firstC = ca.getFirstColumn();
  221. lastC = ca.getLastColumn();
  222. firstR = ca.getFirstRow();
  223. lastR = ca.getLastRow();
  224. if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
  225. if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
  226. Row fRow = sheet.getRow(firstR);
  227. Cell fCell = fRow.getCell(firstC);
  228. cellValue = getCellValue(fCell);
  229. break;
  230. }
  231. } else {
  232. cellValue = "";
  233. }
  234. }
  235. return cellValue;
  236. }
  237. /**
  238. * 获取合并单元格的值
  239. *
  240. * @param sheet
  241. * @param row
  242. * @param column
  243. * @return
  244. */
  245. public static String getMergedRegionValue(Sheet sheet, int row, int column) {
  246. int sheetMergeCount = sheet.getNumMergedRegions();
  247. for (int i = 0; i < sheetMergeCount; i++) {
  248. CellRangeAddress ca = sheet.getMergedRegion(i);
  249. int firstColumn = ca.getFirstColumn();
  250. int lastColumn = ca.getLastColumn();
  251. int firstRow = ca.getFirstRow();
  252. int lastRow = ca.getLastRow();
  253. if (row >= firstRow && row <= lastRow) {
  254. if (column >= firstColumn && column <= lastColumn) {
  255. Row fRow = sheet.getRow(firstRow);
  256. Cell fCell = fRow.getCell(firstColumn);
  257. return getCellValue(fCell);
  258. }
  259. }
  260. }
  261. return null;
  262. }
  263. /**
  264. * 判断指定的单元格是否是合并单元格
  265. *
  266. * @param sheet
  267. * @param row 行下标
  268. * @param column 列下标
  269. * @return
  270. */
  271. public static boolean isMergedRegion(Sheet sheet, int row, int column) {
  272. int sheetMergeCount = sheet.getNumMergedRegions();
  273. for (int i = 0; i < sheetMergeCount; i++) {
  274. CellRangeAddress range = sheet.getMergedRegion(i);
  275. int firstColumn = range.getFirstColumn();
  276. int lastColumn = range.getLastColumn();
  277. int firstRow = range.getFirstRow();
  278. int lastRow = range.getLastRow();
  279. if (row >= firstRow && row <= lastRow) {
  280. if (column >= firstColumn && column <= lastColumn) {
  281. return true;
  282. }
  283. }
  284. }
  285. return false;
  286. }
  287. }