poi導出word文檔,POI上傳Excel的小問題處理

 2023-10-18 阅读 24 评论 0

摘要:package com.platform.utils.excel;import com.platform.utils.RRException; import org.springframework.web.multipart.MultipartFile;import java.io.IOException; import java.util.List; import java.util.Map;/*** Excel文件導入的基本功能類* 可導入EXCEL2003 和 EXCE
package com.platform.utils.excel;import com.platform.utils.RRException;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.util.List;
import java.util.Map;/*** Excel文件導入的基本功能類* 可導入EXCEL2003 和 EXCEL2007格式。** @date 2017年10月28日 13:11:27*/
public class ExcelImport {/*** excel2003擴展名*/public static final String EXCEL03_EXTENSION = ".xls";/*** excel2007擴展名*/public static final String EXCEL07_EXTENSION = ".xlsx";private ExcelImport() {}/*** 解析EXCEL數據為 List<String[]>** @param excelFile 要解析的上傳EXCEL文件* @return List<String[]) 行(列)*/public static List<String[]> getExcelData07(MultipartFile excelFile) {List<String[]> resultList = null;if (null == excelFile || excelFile.isEmpty()) {throw new RRException("文件內容為空!");}Excel2007Reader excel07 = new Excel2007Reader();try {excel07.process(excelFile.getInputStream(), false);} catch (Exception e) {throw new RRException("excel解析失敗!");}resultList = excel07.getSheetData(0);return resultList;}/*** 解析EXCEL數據為 List<String[]>** @param excelFile 要解析的上傳EXCEL文件* @return List<String[]) 行(列)*/public static List<String[]> getExcelData03(MultipartFile excelFile) {List<String[]> resultList = null;if (null == excelFile || excelFile.isEmpty()) {throw new RRException("文件內容為空!");}Excel2003Reader excel03 = new Excel2003Reader();// 實例化excel處理對象try {excel03.process(excelFile.getInputStream());} catch (IOException e) {throw new RRException("excel解析失敗!");}resultList = excel03.getSheetData(0);return resultList;}/*** 通過解析MultipartFile對象獲取excel內容,并且將其拼裝為List<String[]>對象返回** @param excelFile* @return* @throws Exception*/public static List<String[]> getExcelData(MultipartFile excelFile)throws RRException {List<String[]> resultList = null;if (!excelFile.isEmpty()) {// 上傳的文件不能為空String excelFileName = excelFile.getOriginalFilename();// 文件名(帶后綴)if (excelFileName.toLowerCase().endsWith(EXCEL03_EXTENSION)) {// 如果文件是以.xls為后綴Excel2003Reader excel03 = new Excel2003Reader();// 實例化excel處理對象try {excel03.process(excelFile.getInputStream());} catch (IOException e) {throw new RRException("excel解析失敗!");}resultList = excel03.getSheetData(0);} else if (excelFileName.toLowerCase().endsWith(EXCEL07_EXTENSION)) {// 如果文件是以.xlsx為后綴Excel2007Reader excel07 = new Excel2007Reader();try {excel07.process(excelFile.getInputStream(), false);} catch (Exception e) {throw new RRException("excel解析失敗!");}resultList = excel07.getSheetData(0);}}return resultList;}/*** 通過解析MultipartFile對象獲取excel內容,并且將其拼裝為Map<Integer, List<String[]>>對象返回** @param excelFile* @return* @throws Exception*/public static Map<Integer, List<String[]>> getExcelDataAll(MultipartFile excelFile)throws RRException {Map<Integer, List<String[]>> result = null;if (!excelFile.isEmpty()) {// 上傳的文件不能為空String excelFileName = excelFile.getOriginalFilename();// 文件名(帶后綴)if (excelFileName.toLowerCase().endsWith(EXCEL03_EXTENSION)) {// 如果文件是以.xls為后綴Excel2003Reader excel03 = new Excel2003Reader();// 實例化excel處理對象try {excel03.process(excelFile.getInputStream());} catch (IOException e) {throw new RRException("excel解析失敗!");}result = excel03.getSheetData();} else if (excelFileName.toLowerCase().endsWith(EXCEL07_EXTENSION)) {// 如果文件是以.xlsx為后綴Excel2007Reader excel07 = new Excel2007Reader();try {excel07.process(excelFile.getInputStream(), true);} catch (Exception e) {throw new RRException("excel解析失敗!");}result = excel07.getSheetData();}}return result;}
}
RRException 
package com.platform.utils;/*** 自定義異常** * @date 2017年11月18日 下午13:13:23*/
public class RRException extends RuntimeException {private static final long serialVersionUID = 1L;private String msg;private int code = 500;public RRException(String msg) {super(msg);this.msg = msg;}public RRException(String msg, Throwable e) {super(msg, e);this.msg = msg;}public RRException(String msg, int code) {super(msg);this.msg = msg;this.code = code;}public RRException(String msg, int code, Throwable e) {super(msg, e);this.msg = msg;this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}}

?

poi導出word文檔。利用POI上傳Excel 出現如果這一行前幾個是空的? 傳到后臺的list里面第一個數據是從有數據的列開始的? 這樣會造成取數據的時候報錯

解決辦法:

遍歷接受的list,獲取頭的長度,如果兩個長度不同,新定義一個長度和頭信息相同的數組,遍歷這個數組? 將沒有數據的在數組里面加上一個空的字符串,再講數組放到一個list里面

調用set方法

set方法是將原來位置上的那個給取代了,并將原來位置上對象的返回。

 for(int i = 1;i<list.size();i++){if(list.get(i-1).length!=list.get(i).length){Integer count = list.get(0).length-list.get(i).length;String[] a = new String[count];for(int j=0;j<a.length;j++){a[j]="";}List l = new ArrayList(Arrays.asList(list.get(i)));l.addAll(Arrays.asList(a));String[] arr = (String[]) l.toArray(new String[list.get(0).length]);list.set(i,arr);}

?

轉載于:https://www.cnblogs.com/NCL--/p/9754390.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/149704.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息