Java 上传Excel 并解析Excel

布满荆棘的人生 2022-05-15 03:57 678阅读 0赞

这个没什么好说的直接上代码了

调用方法

  1. FileUploadDto fileUploadDto = TransferExcelDateToFileUploadDtoUtil.TransferExcelDateToFileUploadDto(
  2. request, 2, 8, 0);
  3. // 解析后返回List集合
  4. List<FileUploadBaseDto> baseDtos = fileUploadDto.getList();

Dto 上传解析的列

  1. package com.hand.utils.excel;
  2. public class FileUploadBaseDto {
  3. private int rowNum; //行号
  4. private String attribute1;
  5. private String attribute2;
  6. private String attribute3;
  7. private String attribute4;
  8. private String attribute5;
  9. private String attribute6;
  10. private String attribute7;
  11. private String attribute8;
  12. private String attribute9;
  13. private String attribute10;
  14. private String attribute11;
  15. private String attribute12;
  16. private String attribute13;
  17. private String attribute14;
  18. private String attribute15;
  19. private String attribute16;
  20. private String attribute17;
  21. private String attribute18;
  22. private String attribute19;
  23. private String attribute20;
  24. private String attribute21;
  25. private String attribute22;
  26. private String attribute23;
  27. private String attribute24;
  28. private String attribute25;
  29. private String attribute26;
  30. private String attribute27;
  31. private String attribute28;
  32. private String attribute29;
  33. private String attribute30;
  34. private String attribute31;
  35. private String attribute32;
  36. private String attribute33;
  37. private String attribute34;
  38. private String attribute35;
  39. private String attribute36;
  40. private String attribute37;
  41. private String attribute38;
  42. private String attribute39;
  43. private String attribute40;
  44. public int getRowNum() {
  45. return rowNum;
  46. }
  47. public void setRowNum(int rowNum) {
  48. this.rowNum = rowNum;
  49. }
  50. public String getAttribute1() {
  51. return attribute1;
  52. }
  53. public void setAttribute1(String attribute1) {
  54. this.attribute1 = attribute1;
  55. }
  56. public String getAttribute2() {
  57. return attribute2;
  58. }
  59. public void setAttribute2(String attribute2) {
  60. this.attribute2 = attribute2;
  61. }
  62. public String getAttribute3() {
  63. return attribute3;
  64. }
  65. public void setAttribute3(String attribute3) {
  66. this.attribute3 = attribute3;
  67. }
  68. public String getAttribute4() {
  69. return attribute4;
  70. }
  71. public void setAttribute4(String attribute4) {
  72. this.attribute4 = attribute4;
  73. }
  74. public String getAttribute5() {
  75. return attribute5;
  76. }
  77. public void setAttribute5(String attribute5) {
  78. this.attribute5 = attribute5;
  79. }
  80. public String getAttribute6() {
  81. return attribute6;
  82. }
  83. public void setAttribute6(String attribute6) {
  84. this.attribute6 = attribute6;
  85. }
  86. public String getAttribute7() {
  87. return attribute7;
  88. }
  89. public void setAttribute7(String attribute7) {
  90. this.attribute7 = attribute7;
  91. }
  92. public String getAttribute8() {
  93. return attribute8;
  94. }
  95. public void setAttribute8(String attribute8) {
  96. this.attribute8 = attribute8;
  97. }
  98. public String getAttribute9() {
  99. return attribute9;
  100. }
  101. public void setAttribute9(String attribute9) {
  102. this.attribute9 = attribute9;
  103. }
  104. public String getAttribute10() {
  105. return attribute10;
  106. }
  107. public void setAttribute10(String attribute10) {
  108. this.attribute10 = attribute10;
  109. }
  110. public String getAttribute11() {
  111. return attribute11;
  112. }
  113. public void setAttribute11(String attribute11) {
  114. this.attribute11 = attribute11;
  115. }
  116. public String getAttribute12() {
  117. return attribute12;
  118. }
  119. public void setAttribute12(String attribute12) {
  120. this.attribute12 = attribute12;
  121. }
  122. public String getAttribute13() {
  123. return attribute13;
  124. }
  125. public void setAttribute13(String attribute13) {
  126. this.attribute13 = attribute13;
  127. }
  128. public String getAttribute14() {
  129. return attribute14;
  130. }
  131. public void setAttribute14(String attribute14) {
  132. this.attribute14 = attribute14;
  133. }
  134. public String getAttribute15() {
  135. return attribute15;
  136. }
  137. public void setAttribute15(String attribute15) {
  138. this.attribute15 = attribute15;
  139. }
  140. public String getAttribute16() {
  141. return attribute16;
  142. }
  143. public void setAttribute16(String attribute16) {
  144. this.attribute16 = attribute16;
  145. }
  146. public String getAttribute17() {
  147. return attribute17;
  148. }
  149. public void setAttribute17(String attribute17) {
  150. this.attribute17 = attribute17;
  151. }
  152. public String getAttribute18() {
  153. return attribute18;
  154. }
  155. public void setAttribute18(String attribute18) {
  156. this.attribute18 = attribute18;
  157. }
  158. public String getAttribute19() {
  159. return attribute19;
  160. }
  161. public void setAttribute19(String attribute19) {
  162. this.attribute19 = attribute19;
  163. }
  164. public String getAttribute20() {
  165. return attribute20;
  166. }
  167. public void setAttribute20(String attribute20) {
  168. this.attribute20 = attribute20;
  169. }
  170. public String getAttribute21() {
  171. return attribute21;
  172. }
  173. public void setAttribute21(String attribute21) {
  174. this.attribute21 = attribute21;
  175. }
  176. public String getAttribute22() {
  177. return attribute22;
  178. }
  179. public void setAttribute22(String attribute22) {
  180. this.attribute22 = attribute22;
  181. }
  182. public String getAttribute23() {
  183. return attribute23;
  184. }
  185. public void setAttribute23(String attribute23) {
  186. this.attribute23 = attribute23;
  187. }
  188. public String getAttribute24() {
  189. return attribute24;
  190. }
  191. public void setAttribute24(String attribute24) {
  192. this.attribute24 = attribute24;
  193. }
  194. public String getAttribute25() {
  195. return attribute25;
  196. }
  197. public void setAttribute25(String attribute25) {
  198. this.attribute25 = attribute25;
  199. }
  200. public String getAttribute26() {
  201. return attribute26;
  202. }
  203. public void setAttribute26(String attribute26) {
  204. this.attribute26 = attribute26;
  205. }
  206. public String getAttribute27() {
  207. return attribute27;
  208. }
  209. public void setAttribute27(String attribute27) {
  210. this.attribute27 = attribute27;
  211. }
  212. public String getAttribute28() {
  213. return attribute28;
  214. }
  215. public void setAttribute28(String attribute28) {
  216. this.attribute28 = attribute28;
  217. }
  218. public String getAttribute29() {
  219. return attribute29;
  220. }
  221. public void setAttribute29(String attribute29) {
  222. this.attribute29 = attribute29;
  223. }
  224. public String getAttribute30() {
  225. return attribute30;
  226. }
  227. public void setAttribute30(String attribute30) {
  228. this.attribute30 = attribute30;
  229. }
  230. public String getAttribute31() {
  231. return attribute31;
  232. }
  233. public void setAttribute31(String attribute31) {
  234. this.attribute31 = attribute31;
  235. }
  236. public String getAttribute32() {
  237. return attribute32;
  238. }
  239. public void setAttribute32(String attribute32) {
  240. this.attribute32 = attribute32;
  241. }
  242. public String getAttribute33() {
  243. return attribute33;
  244. }
  245. public void setAttribute33(String attribute33) {
  246. this.attribute33 = attribute33;
  247. }
  248. public String getAttribute34() {
  249. return attribute34;
  250. }
  251. public void setAttribute34(String attribute34) {
  252. this.attribute34 = attribute34;
  253. }
  254. public String getAttribute35() {
  255. return attribute35;
  256. }
  257. public void setAttribute35(String attribute35) {
  258. this.attribute35 = attribute35;
  259. }
  260. public String getAttribute36() {
  261. return attribute36;
  262. }
  263. public void setAttribute36(String attribute36) {
  264. this.attribute36 = attribute36;
  265. }
  266. public String getAttribute37() {
  267. return attribute37;
  268. }
  269. public void setAttribute37(String attribute37) {
  270. this.attribute37 = attribute37;
  271. }
  272. public String getAttribute38() {
  273. return attribute38;
  274. }
  275. public void setAttribute38(String attribute38) {
  276. this.attribute38 = attribute38;
  277. }
  278. public String getAttribute39() {
  279. return attribute39;
  280. }
  281. public void setAttribute39(String attribute39) {
  282. this.attribute39 = attribute39;
  283. }
  284. public String getAttribute40() {
  285. return attribute40;
  286. }
  287. public void setAttribute40(String attribute40) {
  288. this.attribute40 = attribute40;
  289. }
  290. }

解析工具

  1. package com.hand.utils.excel;
  2. import oracle.core.lmx.CoreException;
  3. import org.apache.commons.fileupload.FileItem;
  4. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  5. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.apache.poi.ss.usermodel.Sheet;
  10. import org.apache.poi.ss.usermodel.Workbook;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. import javax.servlet.http.HttpServletRequest;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.lang.reflect.Method;
  16. import java.text.DecimalFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.List;
  21. public class TransferExcelDateToFileUploadDtoUtil {
  22. public static FileUploadDto TransferExcelDateToFileUploadDto(HttpServletRequest request, int startRowId, int colCount, int sheetIndex) throws Exception {
  23. /**
  24. *
  25. * 功能描述:
  26. *
  27. * @auther:lkj
  28. * @date:2018/8/24 上午10:37
  29. * @param:[request 请求, startRowId 从第几行开始解析, colCount 一共几列, sheetIndex 解析的sheet页]
  30. * @return:com.hand.utils.excel.FileUploadDto
  31. *
  32. */
  33. //将excel文件流解析为InputStream流
  34. DiskFileItemFactory factory = new DiskFileItemFactory();
  35. ServletFileUpload upload = new ServletFileUpload(factory);
  36. List items = upload.parseRequest(request);
  37. InputStream inputStream = null;
  38. String excelType = "";
  39. if(!items.isEmpty()) {
  40. FileItem item = (FileItem)items.get(0);
  41. if(!item.isFormField()) {
  42. inputStream = item.getInputStream();
  43. //获得当前Excel格式
  44. String fileName = item.getName();
  45. excelType = fileName.substring(fileName.lastIndexOf(".") + 1);
  46. }
  47. }
  48. //将该fileInputStream解析为list对象
  49. FileUploadDto fileUploadDto = TransferExcelDateToFileUploadDtoUtil.transferExcelDateToArrayList(inputStream,excelType,startRowId,colCount,sheetIndex);
  50. return fileUploadDto;
  51. }
  52. /*
  53. 将excel中的数据转换为java对象数组
  54. 传参fileInputStream:excel的文件输入流
  55. 返回:FileUploadDto
  56. */
  57. public static FileUploadDto transferExcelDateToArrayList(InputStream inputStream, String excelType, int startRowId, int colCount, int sheetIndex) throws Exception {
  58. Sheet sheet;
  59. Workbook workBook = null;
  60. List<FileUploadBaseDto> list = new ArrayList<>();
  61. FileUploadDto fileUploadDto = new FileUploadDto();
  62. /*
  63. * 得到sheet
  64. */
  65. try {
  66. if (excelType.equals("xls")){
  67. workBook = new HSSFWorkbook(inputStream);
  68. } else {
  69. workBook = new XSSFWorkbook(inputStream);
  70. }
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. /*
  75. * 验证:导入模板数据时,如果有多个sheet如何选择?
  76. */
  77. int count = workBook.getNumberOfSheets();
  78. if(count>1){
  79. throw new Exception("导入模板中存在多页!");
  80. }
  81. //获得第一个sheet
  82. sheet = workBook.getSheetAt(sheetIndex);
  83. /*
  84. 遍历所有行
  85. */
  86. int rownum = sheet.getLastRowNum() + 1;
  87. for (int i = startRowId-1; i < rownum; i++) {
  88. Row row = sheet.getRow(i);
  89. FileUploadBaseDto model = new FileUploadBaseDto();
  90. model.setRowNum(i+1);
  91. /*
  92. 构造sheet中单行数据的dto
  93. */
  94. for(int j = 0;j < colCount; j++){
  95. model = TransferExcelDateToFileUploadDtoUtil.setAttribute(model,row,j);
  96. }
  97. list.add(model);
  98. }
  99. //返回
  100. fileUploadDto.setList(list);
  101. fileUploadDto.setN(colCount);
  102. return fileUploadDto;
  103. }
  104. /*
  105. * 将Row中第colNum列的信息放入FileUploadBaseDto中
  106. */
  107. public static FileUploadBaseDto setAttribute(FileUploadBaseDto fileUploadBaseDto,Row row,int colNum ){
  108. Cell cell = row.getCell(colNum);
  109. if(cell == null)
  110. return fileUploadBaseDto;
  111. if(cell.toString().equals("")){
  112. return fileUploadBaseDto;
  113. }
  114. Class clazz = FileUploadBaseDto.class;
  115. int count = colNum+1;
  116. try {
  117. String val = "";
  118. switch (cell.getCellType()) {
  119. case Cell.CELL_TYPE_STRING:
  120. val = cell.getStringCellValue();
  121. break;
  122. case Cell.CELL_TYPE_BOOLEAN:
  123. Boolean val1 = cell.getBooleanCellValue();
  124. val = val1.toString();
  125. break;
  126. case Cell.CELL_TYPE_NUMERIC:
  127. if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
  128. Date theDate = cell.getDateCellValue();
  129. SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  130. val = dff.format(theDate);
  131. }else{
  132. DecimalFormat df = new DecimalFormat("#.##");
  133. val = df.format(cell.getNumericCellValue());
  134. }
  135. break;
  136. case Cell.CELL_TYPE_BLANK:
  137. break;
  138. default:
  139. throw new CoreException("数据类型配置不正确");
  140. }
  141. Method method = clazz.getDeclaredMethod("setAttribute" + count,String.class);
  142. method.invoke(fileUploadBaseDto,val);
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. }
  146. return fileUploadBaseDto;
  147. }
  148. }

返回List 集合

  1. package com.hand.utils.excel;
  2. import java.util.List;
  3. public class FileUploadDto {
  4. private List<FileUploadBaseDto> list; //将file解析为List集合
  5. private int n; //列数
  6. public List<FileUploadBaseDto> getList() {
  7. return list;
  8. }
  9. public void setList(List<FileUploadBaseDto> list) {
  10. this.list = list;
  11. }
  12. public int getN() {
  13. return n;
  14. }
  15. public void setN(int n) {
  16. this.n = n;
  17. }
  18. }

发表评论

表情:
评论列表 (有 0 条评论,678人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Excel解析

    该篇博客废除,见解析excel工具类 兼容2007和2003两种类型的文件,举例:这里模板有两个页脚: ![20180404100358222][]           

    相关 java excel--poi

    工作中很多批量上传需求不同,每个需求都要写一次批量上传代码,太烦。。决定写一个通用工具,此代码复制即用。需要改进的地方请评论区留言 方法调用传参例子: 上传文件中列的字段