ExcelUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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.hssf.usermodel.HSSFDateUtil;
  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")||fileName.endsWith("xlsm")) {
  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(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 if(cell.getCellStyle().getDataFormatString().indexOf("0_") > -1){
  86. //数字非日期
  87. System.out.println("Excel值:"+ cell.getNumericCellValue());
  88. cellValue = cell.getNumericCellValue() + "";
  89. }else{
  90. try {
  91. cellValue = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  92. }catch (Exception e){
  93. cellValue = cell.getNumericCellValue() + "";
  94. }
  95. }
  96. break;
  97. case "BLANK":
  98. cellValue = "";
  99. break;
  100. default:
  101. cellValue = cell.toString();
  102. break;
  103. }
  104. //判断数据的类型
  105. // switch (cell.getCellType()) {
  106. // case Cell.CELL_TYPE_NUMERIC: //数字
  107. // cellValue = stringDateProcess(cell);
  108. // break;
  109. // case Cell.CELL_TYPE_STRING: //字符串
  110. // cellValue = String.valueOf(cell.getStringCellValue());
  111. // break;
  112. // case Cell.CELL_TYPE_BOOLEAN: //Boolean
  113. // cellValue = String.valueOf(cell.getBooleanCellValue());
  114. // break;
  115. // case Cell.CELL_TYPE_FORMULA: //公式
  116. // cellValue = String.valueOf(cell.getCellFormula());
  117. // break;
  118. // case Cell.CELL_TYPE_BLANK: //空值
  119. // cellValue = "";
  120. // break;
  121. // case Cell.CELL_TYPE_ERROR: //故障
  122. // cellValue = "非法字符";
  123. // break;
  124. // default:
  125. // cellValue = "未知类型";
  126. // break;
  127. // }
  128. // cellValue = String.valueOf(cell.getStringCellValue());
  129. return cellValue;
  130. }
  131. /**
  132. * 时间格式处理
  133. *
  134. * @return
  135. * @author Liu Xin Nan
  136. * @data 2017年11月27日
  137. */
  138. public static String stringDateProcess(Cell cell) {
  139. String result = new String();
  140. if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
  141. SimpleDateFormat sdf = null;
  142. if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
  143. sdf = new SimpleDateFormat("HH:mm");
  144. } else {// 日期
  145. sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  146. }
  147. Date date = cell.getDateCellValue();
  148. result = sdf.format(date);
  149. } else if (cell.getCellStyle().getDataFormat() == 58) {
  150. // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
  151. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  152. double value = cell.getNumericCellValue();
  153. Date date = DateUtil
  154. .getJavaDate(value);
  155. result = sdf.format(date);
  156. } else {
  157. double value = cell.getNumericCellValue();
  158. CellStyle style = cell.getCellStyle();
  159. DecimalFormat format = new DecimalFormat();
  160. String temp = style.getDataFormatString();
  161. // 单元格设置成常规
  162. if (temp.equals("General")) {
  163. format.applyPattern("#");
  164. }
  165. result = format.format(value);
  166. }
  167. return result;
  168. }
  169. /**
  170. * 合并单元格处理,获取合并行
  171. *
  172. * @param sheet
  173. * @return List<CellRangeAddress>
  174. */
  175. public static List<CellRangeAddress> getCombineCell(Sheet sheet) {
  176. List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
  177. //获得一个 sheet 中合并单元格的数量
  178. int sheetmergerCount = sheet.getNumMergedRegions();
  179. //遍历所有的合并单元格
  180. for (int i = 0; i < sheetmergerCount; i++) {
  181. //获得合并单元格保存进list中
  182. CellRangeAddress ca = sheet.getMergedRegion(i);
  183. list.add(ca);
  184. }
  185. return list;
  186. }
  187. public static int getRowNum(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet) {
  188. int xr = 0;
  189. int firstC = 0;
  190. int lastC = 0;
  191. int firstR = 0;
  192. int lastR = 0;
  193. for (CellRangeAddress ca : listCombineCell) {
  194. //获得合并单元格的起始行, 结束行, 起始列, 结束列
  195. firstC = ca.getFirstColumn();
  196. lastC = ca.getLastColumn();
  197. firstR = ca.getFirstRow();
  198. lastR = ca.getLastRow();
  199. if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
  200. if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
  201. xr = lastR;
  202. }
  203. }
  204. }
  205. return xr;
  206. }
  207. /**
  208. * 判断单元格是否为合并单元格,是的话则将单元格的值返回
  209. *
  210. * @param listCombineCell 存放合并单元格的list
  211. * @param cell 需要判断的单元格
  212. * @param sheet sheet
  213. * @return
  214. */
  215. public static String isCombineCell(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet)
  216. throws Exception {
  217. int firstC = 0;
  218. int lastC = 0;
  219. int firstR = 0;
  220. int lastR = 0;
  221. String cellValue = null;
  222. for (CellRangeAddress ca : listCombineCell) {
  223. //获得合并单元格的起始行, 结束行, 起始列, 结束列
  224. firstC = ca.getFirstColumn();
  225. lastC = ca.getLastColumn();
  226. firstR = ca.getFirstRow();
  227. lastR = ca.getLastRow();
  228. if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {
  229. if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {
  230. Row fRow = sheet.getRow(firstR);
  231. Cell fCell = fRow.getCell(firstC);
  232. cellValue = getCellValue(fCell);
  233. break;
  234. }
  235. } else {
  236. cellValue = "";
  237. }
  238. }
  239. return cellValue;
  240. }
  241. /**
  242. * 获取合并单元格的值
  243. *
  244. * @param sheet
  245. * @param row
  246. * @param column
  247. * @return
  248. */
  249. public static String getMergedRegionValue(Sheet sheet, int row, int column) {
  250. int sheetMergeCount = sheet.getNumMergedRegions();
  251. for (int i = 0; i < sheetMergeCount; i++) {
  252. CellRangeAddress ca = sheet.getMergedRegion(i);
  253. int firstColumn = ca.getFirstColumn();
  254. int lastColumn = ca.getLastColumn();
  255. int firstRow = ca.getFirstRow();
  256. int lastRow = ca.getLastRow();
  257. if (row >= firstRow && row <= lastRow) {
  258. if (column >= firstColumn && column <= lastColumn) {
  259. Row fRow = sheet.getRow(firstRow);
  260. Cell fCell = fRow.getCell(firstColumn);
  261. return getCellValue(fCell);
  262. }
  263. }
  264. }
  265. return null;
  266. }
  267. /**
  268. * 判断指定的单元格是否是合并单元格
  269. *
  270. * @param sheet
  271. * @param row 行下标
  272. * @param column 列下标
  273. * @return
  274. */
  275. public static boolean isMergedRegion(Sheet sheet, int row, int column) {
  276. int sheetMergeCount = sheet.getNumMergedRegions();
  277. for (int i = 0; i < sheetMergeCount; i++) {
  278. CellRangeAddress range = sheet.getMergedRegion(i);
  279. int firstColumn = range.getFirstColumn();
  280. int lastColumn = range.getLastColumn();
  281. int firstRow = range.getFirstRow();
  282. int lastRow = range.getLastRow();
  283. if (row >= firstRow && row <= lastRow) {
  284. if (column >= firstColumn && column <= lastColumn) {
  285. return true;
  286. }
  287. }
  288. }
  289. return false;
  290. }
  291. }