ExcelUtils.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.cslg.ids.common.utils;
  2. import org.apache.poi.hssf.usermodel.*;
  3. import org.apache.poi.ooxml.POIXMLDocumentPart;
  4. import org.apache.poi.ss.usermodel.*;
  5. import org.apache.poi.xssf.usermodel.*;
  6. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  7. import java.awt.*;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. public class ExcelUtils {
  16. public static Map<String, PictureData> getDataFromExcel(String filePath) throws IOException {
  17. //判断是否为excel类型文件
  18. if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx") && !filePath.endsWith(".XLS") && !filePath.endsWith(".XLSX")) {
  19. System.out.println("文件不是excel类型");
  20. }
  21. FileInputStream fis = null;
  22. Workbook wookbook = null;
  23. Sheet sheet = null;
  24. try {
  25. //获取一个绝对地址的流
  26. fis = new FileInputStream(filePath);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. try {
  31. //2003版本的excel,用.xls结尾
  32. wookbook = new HSSFWorkbook(fis);//得到工作簿
  33. } catch (Exception ex) {
  34. //ex.printStackTrace();
  35. try {
  36. //2007版本的excel,用.xlsx结尾
  37. fis = new FileInputStream(filePath);
  38. wookbook = new XSSFWorkbook(fis);//得到工作簿
  39. } catch (IOException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. }
  44. Map<String, PictureData> maplist = null;
  45. //拿到excel表格的第一个sheet工作簿
  46. sheet = wookbook.getSheetAt(0);
  47. // 判断用07还是03的方法获取图片
  48. if (filePath.endsWith(".xls") || filePath.endsWith(".XLS")) {
  49. maplist = getPictures1((HSSFSheet) sheet);
  50. } else if (filePath.endsWith(".xlsx") || filePath.endsWith(".XLSX")) {
  51. maplist = getPictures2((XSSFSheet) sheet);
  52. }
  53. wookbook.close();
  54. return maplist;
  55. }
  56. /**
  57. * 获取图片和位置 (xls)
  58. *
  59. * @param sheet
  60. * @return
  61. * @throws IOException
  62. */
  63. public static Map<String, PictureData> getPictures1(HSSFSheet sheet) throws IOException {
  64. Map<String, PictureData> map = new HashMap();
  65. if (sheet.getDrawingPatriarch() != null) {
  66. List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
  67. for (HSSFShape shape : list) {
  68. if (shape instanceof HSSFPicture) {
  69. HSSFPicture picture = (HSSFPicture) shape;
  70. HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
  71. PictureData pdata = picture.getPictureData();
  72. String key = String.valueOf(cAnchor.getRow1());
  73. map.put(key, pdata);
  74. }
  75. }
  76. }
  77. return map;
  78. }
  79. /**
  80. * 获取图片和位置 (xlsx)
  81. *
  82. * @param sheet
  83. * @return
  84. * @throws IOException
  85. */
  86. public static Map<String, PictureData> getPictures2(XSSFSheet sheet) throws IOException {
  87. Map<String, PictureData> map = new HashMap();
  88. List<POIXMLDocumentPart> list = sheet.getRelations();
  89. for (POIXMLDocumentPart part : list) {
  90. if (part instanceof XSSFDrawing) {
  91. XSSFDrawing drawing = (XSSFDrawing) part;
  92. List<XSSFShape> shapes = drawing.getShapes();
  93. for (XSSFShape shape : shapes) {
  94. XSSFPicture picture = (XSSFPicture) shape;
  95. Dimension d = picture.getImageDimension();
  96. //解决图片空指针报错问题 lig 2021-06-03
  97. XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
  98. //XSSFClientAnchor anchor = picture.getPreferredSize();
  99. CTMarker marker = anchor.getFrom();
  100. String key = String.valueOf(marker.getRow());
  101. map.put(key, picture.getPictureData());
  102. }
  103. }
  104. }
  105. return map;
  106. }
  107. // /**
  108. // * @param pictureData 图片
  109. // * @return 返回图片的文件路径和文件名称
  110. // */
  111. // public static Map<String, String> savePicture(PictureData pictureData) throws IOException {
  112. // FileUtils fileUtils = SpringUtils.getBean(FileUtils.class);
  113. // Map<String, String> result = new HashMap<>();
  114. // String ext = pictureData.suggestFileExtension();
  115. // byte[] data = pictureData.getData();
  116. // String picName = IdUtil.simpleUUID() + "." + ext;
  117. // String date = DateUtils.getNowTimeFormat("yyyyMMdd");
  118. // String folderPath = fileUtils.getSavePath(date);
  119. // String filePath = FileUtils.FILE_SEPARATOR + date + FileUtils.FILE_SEPARATOR + picName;
  120. // File directory = new File(folderPath);
  121. // if (!directory.exists()) {
  122. // directory.mkdir();
  123. // }
  124. // FileOutputStream out = new FileOutputStream(folderPath + picName);
  125. // out.write(data);
  126. // out.close();
  127. // result.put("path", filePath);
  128. // result.put("name", picName);
  129. // return result;
  130. // }
  131. /**
  132. * @param
  133. * @param x 单元格x轴坐标
  134. * @param y 单元格y轴坐标
  135. * @param pictureData 图片二进制数据
  136. * @param picType 图片格式
  137. */
  138. public static void writePicture(Sheet sheet, int x, int y, byte[] pictureData, int picType) {
  139. Drawing drawingPatriarch = sheet.createDrawingPatriarch();
  140. //设置图片单元格位置
  141. ClientAnchor anchor = drawingPatriarch.createAnchor(0, 0, 0, 0, x, y, x + 1, y + 1);
  142. //随单元格改变位置和大小
  143. anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
  144. //添加图片
  145. int pictureIndex = sheet.getWorkbook().addPicture(pictureData, picType);
  146. drawingPatriarch.createPicture(anchor, pictureIndex);
  147. }
  148. public static void setExcelCellStyle(HSSFCellStyle cellStyle) {
  149. cellStyle.setBorderBottom(BorderStyle.THIN);
  150. cellStyle.setBorderLeft(BorderStyle.THIN);
  151. cellStyle.setBorderRight(BorderStyle.THIN);
  152. cellStyle.setBorderTop(BorderStyle.THIN);
  153. }
  154. public static String getValue(Cell cell) {
  155. if (cell != null) {
  156. if (cell.getCellType() == CellType.NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)) {
  157. Date date = cell.getDateCellValue();
  158. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  159. String dateString = dateFormat.format(date);
  160. return dateString;
  161. } else {
  162. return cell.toString();
  163. }
  164. }
  165. return "";
  166. }
  167. }