相信在大部分的web專案中都會有匯出匯入Excel的需求,今天我們就來看看如何用Java程式碼去實現 用POI匯出Excel表格。
pom檔案中,新增以下依賴
<!--Excel工具-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
<scope>compile</scope>
</dependency>
1.ExcelClassField
package com.***;
import lombok.Data;
import java.util.LinkedHashMap;
/**
* @description: excel欄位
* @author: ***
* @date: 2022/6/21
*/
@Data
public class ExcelClassField {
/**
* 欄位名稱
*/
private String fieldName;
/**
* 表頭名稱
*/
private String name;
/**
* 對映關係
*/
private LinkedHashMap<String, String> kvMap;
/**
* 範例值
*/
private Object example;
/**
* 排序
*/
private int sort;
/**
* 是否為註解欄位:0-否,1-是
*/
private int hasAnnotation;
}
2.ExcelExport(匯出Excel用)
package com.***;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @description: excel匯出
* @author: ***
* @date: 2022/6/21
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelExport {
/**
* 欄位名稱
*/
String value();
/**
* 匯出排序先後: 數位越小越靠前(預設按Java類欄位順序匯出)
*/
int sort() default 0;
/**
* 匯出對映,格式如:0-未知;1-男;2-女
*/
String kv() default "";
/**
* 匯出模板範例值(有值的話,直接取該值,不做對映)
*/
String example() default "";
}
3.ExcelImport(匯入Excel用)
package com.***;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @description: excel匯入
* @author: ***
* @date: 2022/6/21
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelImport {
/**
* 欄位名稱
*/
String value();
/**
* 匯出對映,格式如:0-未知;1-男;2-女
*/
String kv() default "";
/**
* 是否為必填欄位(預設為非必填)
*/
boolean required() default false;
/**
* 最大長度(預設255)
*/
int maxLength() default 255;
/**
* 匯入唯一性驗證(多個欄位則取聯合驗證)
*/
boolean unique() default false;
}
4.ExcelUtils
package com.***;
import com.***.utils.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.***.uitl.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.net.URLEncoder;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
/**
* @description: 匯出報表util
* @author: ***
* @date: 2022/6/21
*/
public class ExcelUtils {
public static final String ROW_MERGE = "row_merge";
public static final String COLUMN_MERGE = "column_merge";
private static final String XLSX = ".xlsx";
private static final String XLS = ".xls";
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String ROW_NUM = "rowNum";
private static final String ROW_DATA = "rowData";
private static final String ROW_TIPS = "rowTips";
private static final int CELL_OTHER = 0;
private static final int CELL_ROW_MERGE = 1;
private static final int CELL_COLUMN_MERGE = 2;
private static final int IMG_HEIGHT = 30;
private static final int IMG_WIDTH = 30;
private static final char LEAN_LINE = '/';
private static final int BYTES_DEFAULT_LENGTH = 10240;
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance();
public static <T> List<T> readFile(File file, Class<T> clazz) throws Exception {
JSONArray array = readFile(file);
return getBeanList(array, clazz);
}
public static <T> List<T> readMultipartFile(MultipartFile mFile, Class<T> clazz) throws Exception {
JSONArray array = readMultipartFile(mFile);
return getBeanList(array, clazz);
}
public static JSONArray readFile(File file) throws Exception {
return readExcel(null, file);
}
public static JSONArray readMultipartFile(MultipartFile mFile) throws Exception {
return readExcel(mFile, null);
}
public static Map<String, JSONArray> readFileManySheet(File file) throws Exception {
return readExcelManySheet(null, file);
}
public static Map<String, JSONArray> readFileManySheet(MultipartFile file) throws Exception {
return readExcelManySheet(file, null);
}
private static <T> List<T> getBeanList(JSONArray array, Class<T> clazz) throws Exception {
List<T> list = new ArrayList<>();
Map<Integer, String> uniqueMap = new HashMap<>(16);
for (int i = 0; i < array.size(); i++) {
list.add(getBean(clazz, array.getJSONObject(i), uniqueMap));
}
return list;
}
/**
* 獲取每個物件的資料
*/
private static <T> T getBean(Class<T> c, JSONObject obj, Map<Integer, String> uniqueMap) throws Exception {
T t = c.newInstance();
Field[] fields = c.getDeclaredFields();
List<String> errMsgList = new ArrayList<>();
boolean hasRowTipsField = false;
StringBuilder uniqueBuilder = new StringBuilder();
int rowNum = 0;
for (Field field : fields) {
// 行號
if (field.getName().equals(ROW_NUM)) {
rowNum = obj.getInteger(ROW_NUM);
field.setAccessible(true);
field.set(t, rowNum);
continue;
}
// 是否需要設定異常資訊
if (field.getName().equals(ROW_TIPS)) {
hasRowTipsField = true;
continue;
}
// 原始資料
if (field.getName().equals(ROW_DATA)) {
field.setAccessible(true);
field.set(t, obj.toString());
continue;
}
// 設定對應屬性值
setFieldValue(t, field, obj, uniqueBuilder, errMsgList);
}
// 資料唯一性校驗
if (uniqueBuilder.length() > 0) {
if (uniqueMap.containsValue(uniqueBuilder.toString())) {
Set<Integer> rowNumKeys = uniqueMap.keySet();
for (Integer num : rowNumKeys) {
if (uniqueMap.get(num).equals(uniqueBuilder.toString())) {
errMsgList.add(String.format("資料唯一性校驗失敗,(%s)與第%s行重複)", uniqueBuilder, num));
}
}
} else {
uniqueMap.put(rowNum, uniqueBuilder.toString());
}
}
// 失敗處理
if (errMsgList.isEmpty() && !hasRowTipsField) {
return t;
}
StringBuilder sb = new StringBuilder();
int size = errMsgList.size();
for (int i = 0; i < size; i++) {
if (i == size - 1) {
sb.append(errMsgList.get(i));
} else {
sb.append(errMsgList.get(i)).append(";");
}
}
// 設定錯誤資訊
for (Field field : fields) {
if (field.getName().equals(ROW_TIPS)) {
field.setAccessible(true);
field.set(t, sb.toString());
}
}
return t;
}
private static <T> void setFieldValue(T t, Field field, JSONObject obj, StringBuilder uniqueBuilder, List<String> errMsgList) {
// 獲取 ExcelImport 註解屬性
ExcelImport annotation = field.getAnnotation(ExcelImport.class);
if (annotation == null) {
return;
}
String cname = annotation.value();
if (cname.trim().length() == 0) {
return;
}
// 獲取具體值
String val = null;
if (obj.containsKey(cname)) {
val = getString(obj.getString(cname));
}
if (val == null) {
return;
}
field.setAccessible(true);
// 判斷是否必填
boolean require = annotation.required();
if (require && val.isEmpty()) {
errMsgList.add(String.format("[%s]不能為空", cname));
return;
}
// 資料唯一性獲取
boolean unique = annotation.unique();
if (unique) {
if (uniqueBuilder.length() > 0) {
uniqueBuilder.append("--").append(val);
} else {
uniqueBuilder.append(val);
}
}
// 判斷是否超過最大長度
int maxLength = annotation.maxLength();
if (maxLength > 0 && val.length() > maxLength) {
errMsgList.add(String.format("[%s]長度不能超過%s個字元(當前%s個字元)", cname, maxLength, val.length()));
}
// 判斷當前屬性是否有對映關係
LinkedHashMap<String, String> kvMap = getKvMap(annotation.kv());
if (!kvMap.isEmpty()) {
boolean isMatch = false;
for (String key : kvMap.keySet()) {
if (kvMap.get(key).equals(val)) {
val = key;
isMatch = true;
break;
}
}
if (!isMatch) {
errMsgList.add(String.format("[%s]的值不正確(當前值為%s)", cname, val));
return;
}
}
// 其餘情況根據型別賦值
String fieldClassName = field.getType().getSimpleName();
try {
if ("String".equalsIgnoreCase(fieldClassName)) {
field.set(t, val);
} else if ("boolean".equalsIgnoreCase(fieldClassName)) {
field.set(t, Boolean.valueOf(val));
} else if ("int".equalsIgnoreCase(fieldClassName) || "Integer".equals(fieldClassName)) {
try {
field.set(t, Integer.valueOf(val));
} catch (NumberFormatException e) {
errMsgList.add(String.format("[%s]的值格式不正確(當前值為%s)", cname, val));
}
} else if ("double".equalsIgnoreCase(fieldClassName)) {
field.set(t, Double.valueOf(val));
} else if ("long".equalsIgnoreCase(fieldClassName)) {
field.set(t, Long.valueOf(val));
} else if ("BigDecimal".equalsIgnoreCase(fieldClassName)) {
if (StringUtils.isEmpty(val)) {
field.set(t, new BigDecimal(0));
} else {
field.set(t, new BigDecimal(val));
}
} else if ("Date".equalsIgnoreCase(fieldClassName)) {
field.set(t, DateUtil.getDate(val, DateUtil.YYYYMMDDHHMMSS));
} else {
field.set(t, val);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Map<String, JSONArray> readExcelManySheet(MultipartFile mFile, File file) throws IOException {
Workbook book = getWorkbook(mFile, file);
if (book == null) {
return Collections.emptyMap();
}
Map<String, JSONArray> map = new LinkedHashMap<>();
for (int i = 0; i < book.getNumberOfSheets(); i++) {
Sheet sheet = book.getSheetAt(i);
JSONArray arr = readSheet(sheet);
map.put(sheet.getSheetName(), arr);
}
book.close();
return map;
}
private static JSONArray readExcel(MultipartFile mFile, File file) throws IOException {
Workbook book = getWorkbook(mFile, file);
if (book == null) {
return new JSONArray();
}
JSONArray array = readSheet(book.getSheetAt(0));
book.close();
return array;
}
private static Workbook getWorkbook(MultipartFile mFile, File file) throws IOException {
boolean fileNotExist = (file == null || !file.exists());
if (mFile == null && fileNotExist) {
return null;
}
// 解析表格資料
InputStream in;
String fileName;
if (mFile != null) {
// 上傳檔案解析
in = mFile.getInputStream();
fileName = getString(mFile.getOriginalFilename()).toLowerCase();
} else {
// 本地檔案解析
in = new FileInputStream(file);
fileName = file.getName().toLowerCase();
}
Workbook book;
if (fileName.endsWith(XLSX)) {
book = new XSSFWorkbook(in);
} else if (fileName.endsWith(XLS)) {
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
book = new HSSFWorkbook(poifsFileSystem);
} else {
return null;
}
in.close();
return book;
}
private static JSONArray readSheet(Sheet sheet) {
// 首行下標
int rowStart = sheet.getFirstRowNum();
// 尾行下標
int rowEnd = sheet.getLastRowNum();
// 獲取表頭行
Row headRow = sheet.getRow(rowStart);
if (headRow == null) {
return new JSONArray();
}
int cellStart = headRow.getFirstCellNum();
int cellEnd = headRow.getLastCellNum();
Map<Integer, String> keyMap = new HashMap<>();
for (int j = cellStart; j < cellEnd; j++) {
// 獲取表頭資料
String val = getCellValue(headRow.getCell(j));
if (val != null && val.trim().length() != 0) {
keyMap.put(j, val);
}
}
// 如果表頭沒有資料則不進行解析
if (keyMap.isEmpty()) {
return (JSONArray) Collections.emptyList();
}
// 獲取每行JSON物件的值
JSONArray array = new JSONArray();
// 如果首行與尾行相同,表明只有一行,返回表頭資料
if (rowStart == rowEnd) {
JSONObject obj = new JSONObject();
// 新增行號
obj.put(ROW_NUM, 1);
for (int i : keyMap.keySet()) {
obj.put(keyMap.get(i), "");
}
array.add(obj);
return array;
}
for (int i = rowStart + 1; i <= rowEnd; i++) {
Row eachRow = sheet.getRow(i);
JSONObject obj = new JSONObject();
// 新增行號
obj.put(ROW_NUM, i + 1);
StringBuilder sb = new StringBuilder();
for (int k = cellStart; k < cellEnd; k++) {
if (eachRow != null) {
String val = getCellValue(eachRow.getCell(k));
// 所有資料新增到裡面,用於判斷該行是否為空
sb.append(val);
obj.put(keyMap.get(k), val);
}
}
if (sb.length() > 0) {
array.add(obj);
}
}
return array;
}
private static String getCellValue(Cell cell) {
// 空白或空
if (cell == null || cell.getCellType() == CellType.BLANK) {
return "";
}
// String型別
if (cell.getCellType() == CellType.STRING) {
String val = cell.getStringCellValue();
if (val == null || val.trim().length() == 0) {
return "";
}
return val.trim();
}
// 數位型別
if (cell.getCellType() == CellType.NUMERIC) {
String s = cell.getNumericCellValue() + "";
// 去掉尾巴上的小數點0
if (Pattern.matches(".*\\.0*", s)) {
return s.split("\\.")[0];
} else {
return s.trim();
}
}
// 布林值型別
if (cell.getCellType() == CellType.BOOLEAN) {
return cell.getBooleanCellValue() + "";
}
// 錯誤型別
return cell.getCellFormula();
}
public static <T> void exportTemplate(HttpServletResponse response, String fileName, Class<T> clazz, String dateFormat) {
exportTemplate(response, fileName, fileName, clazz, false, dateFormat);
}
public static <T> void exportTemplate(HttpServletResponse response, String fileName, String sheetName,
Class<T> clazz, String dateFormat) {
exportTemplate(response, fileName, sheetName, clazz, false, dateFormat);
}
public static <T> void exportTemplate(HttpServletResponse response, String fileName, Class<T> clazz,
boolean isContainExample, String dateFormat) {
exportTemplate(response, fileName, fileName, clazz, isContainExample, dateFormat);
}
public static <T> void exportTemplate(HttpServletResponse response, String fileName, String sheetName,
Class<T> clazz, boolean isContainExample, String dateFormat) {
// 獲取表頭欄位
List<ExcelClassField> headFieldList = getExcelClassFieldList(clazz);
// 獲取表頭資料和範例資料
List<List<Object>> sheetDataList = new ArrayList<>();
List<Object> headList = new ArrayList<>();
List<Object> exampleList = new ArrayList<>();
Map<Integer, List<String>> selectMap = new LinkedHashMap<>();
for (int i = 0; i < headFieldList.size(); i++) {
ExcelClassField each = headFieldList.get(i);
headList.add(each.getName());
exampleList.add(each.getExample());
LinkedHashMap<String, String> kvMap = each.getKvMap();
if (kvMap != null && kvMap.size() > 0) {
selectMap.put(i, new ArrayList<>(kvMap.values()));
}
}
sheetDataList.add(headList);
if (isContainExample) {
sheetDataList.add(exampleList);
}
// 匯出資料
export(response, fileName, sheetName, sheetDataList, selectMap, dateFormat);
}
private static <T> List<ExcelClassField> getExcelClassFieldList(Class<T> clazz) {
// 解析所有欄位
Field[] fields = clazz.getDeclaredFields();
boolean hasExportAnnotation = false;
Map<Integer, List<ExcelClassField>> map = new LinkedHashMap<>();
List<Integer> sortList = new ArrayList<>();
for (Field field : fields) {
ExcelClassField cf = getExcelClassField(field);
if (cf.getHasAnnotation() == 1) {
hasExportAnnotation = true;
}
int sort = cf.getSort();
if (map.containsKey(sort)) {
map.get(sort).add(cf);
} else {
List<ExcelClassField> list = new ArrayList<>();
list.add(cf);
sortList.add(sort);
map.put(sort, list);
}
}
Collections.sort(sortList);
// 獲取表頭
List<ExcelClassField> headFieldList = new ArrayList<>();
if (hasExportAnnotation) {
for (Integer sort : sortList) {
for (ExcelClassField cf : map.get(sort)) {
if (cf.getHasAnnotation() == 1) {
headFieldList.add(cf);
}
}
}
} else {
headFieldList.addAll(map.get(0));
}
return headFieldList;
}
private static ExcelClassField getExcelClassField(Field field) {
ExcelClassField cf = new ExcelClassField();
String fieldName = field.getName();
cf.setFieldName(fieldName);
ExcelExport annotation = field.getAnnotation(ExcelExport.class);
// 無 ExcelExport 註解情況
if (annotation == null) {
cf.setHasAnnotation(0);
cf.setName(fieldName);
cf.setSort(0);
return cf;
}
// 有 ExcelExport 註解情況
cf.setHasAnnotation(1);
cf.setName(annotation.value());
String example = getString(annotation.example());
if (!example.isEmpty()) {
if (isNumeric(example)) {
cf.setExample(Double.valueOf(example));
} else {
cf.setExample(example);
}
} else {
cf.setExample("");
}
cf.setSort(annotation.sort());
// 解析對映
String kv = getString(annotation.kv());
cf.setKvMap(getKvMap(kv));
return cf;
}
private static LinkedHashMap<String, String> getKvMap(String kv) {
LinkedHashMap<String, String> kvMap = new LinkedHashMap<>();
if (kv.isEmpty()) {
return kvMap;
}
String[] kvs = kv.split(";");
if (kvs.length == 0) {
return kvMap;
}
for (String each : kvs) {
String[] eachKv = getString(each).split("-");
if (eachKv.length != 2) {
continue;
}
String k = eachKv[0];
String v = eachKv[1];
if (k.isEmpty() || v.isEmpty()) {
continue;
}
kvMap.put(k, v);
}
return kvMap;
}
/**
* 匯出表格到本地
*
* @param file 本地檔案物件
* @param sheetData 匯出資料
*/
public static void exportFile(File file, List<List<Object>> sheetData, String dateFormat) {
if (file == null) {
System.out.println("檔案建立失敗");
return;
}
if (sheetData == null) {
sheetData = new ArrayList<>();
}
Map<String, List<List<Object>>> map = new HashMap<>();
map.put(file.getName(), sheetData);
export(null, file, file.getName(), map, null, dateFormat);
}
/**
* 匯出表格到本地
*
* @param <T> 匯出資料類似,和K型別保持一致
* @param filePath 檔案父路徑(如:D:/doc/excel/)
* @param fileName 檔名稱(不帶尾綴,如:學生表)
* @param list 匯出資料
* @throws IOException IO異常
*/
public static <T> File exportFile(String filePath, String fileName, List<T> list, String dateFormat) throws IOException {
File file = getFile(filePath, fileName);
List<List<Object>> sheetData = getSheetData(list);
exportFile(file, sheetData, dateFormat);
return file;
}
/**
* 獲取檔案
*
* @param filePath filePath 檔案父路徑(如:D:/doc/excel/)
* @param fileName 檔名稱(不帶尾綴,如:使用者表)
* @return 本地File檔案物件
*/
private static File getFile(String filePath, String fileName) throws IOException {
String dirPath = getString(filePath);
String fileFullPath;
if (dirPath.isEmpty()) {
fileFullPath = fileName;
} else {
// 判定資料夾是否存在,如果不存在,則級聯建立
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
// 獲取資料夾全名
if (dirPath.endsWith(String.valueOf(LEAN_LINE))) {
fileFullPath = dirPath + fileName + XLSX;
} else {
fileFullPath = dirPath + LEAN_LINE + fileName + XLSX;
}
}
System.out.println(fileFullPath);
File file = new File(fileFullPath);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
private static <T> List<List<Object>> getSheetData(List<T> list) {
// 獲取表頭欄位
List<ExcelClassField> excelClassFieldList = getExcelClassFieldList(list.get(0).getClass());
List<String> headFieldList = new ArrayList<>();
List<Object> headList = new ArrayList<>();
Map<String, ExcelClassField> headFieldMap = new HashMap<>();
for (ExcelClassField each : excelClassFieldList) {
String fieldName = each.getFieldName();
headFieldList.add(fieldName);
headFieldMap.put(fieldName, each);
headList.add(each.getName());
}
// 新增表頭名稱
List<List<Object>> sheetDataList = new ArrayList<>();
sheetDataList.add(headList);
// 獲取表資料
for (T t : list) {
Map<String, Object> fieldDataMap = getFieldDataMap(t);
Set<String> fieldDataKeys = fieldDataMap.keySet();
List<Object> rowList = new ArrayList<>();
for (String headField : headFieldList) {
if (!fieldDataKeys.contains(headField)) {
continue;
}
Object data = fieldDataMap.get(headField);
if (data == null) {
rowList.add("");
continue;
}
// 超防止過11位的數位自動轉換為科學計數法
String string = data.toString();
if (isNumeric(string) && string.length() > 11) {
data = new BigDecimal(string).toPlainString();
}
ExcelClassField cf = headFieldMap.get(headField);
// 判斷是否有對映關係
LinkedHashMap<String, String> kvMap = cf.getKvMap();
if (kvMap == null || kvMap.isEmpty()) {
rowList.add(data);
continue;
}
String val = kvMap.get(data.toString());
if (val == null) {
rowList.add(data);
continue;
}
if (isNumeric(val)) {
rowList.add(Double.valueOf(val));
} else {
rowList.add(val);
}
}
sheetDataList.add(rowList);
}
return sheetDataList;
}
private static <T> Map<String, Object> getFieldDataMap(T t) {
Map<String, Object> map = new HashMap<>();
Field[] fields = t.getClass().getDeclaredFields();
try {
for (Field field : fields) {
String fieldName = field.getName();
field.setAccessible(true);
Object object = field.get(t);
map.put(fieldName, object);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
return map;
}
public static void exportEmpty(HttpServletResponse response, String fileName, String dateFormat) {
List<List<Object>> sheetDataList = new ArrayList<>();
List<Object> headList = new ArrayList<>();
headList.add("匯出無資料");
sheetDataList.add(headList);
export(response, fileName, sheetDataList, dateFormat);
}
public static void export(HttpServletResponse response, String fileName, List<List<Object>> sheetDataList, String dateFormat) {
export(response, fileName, fileName, sheetDataList, null, dateFormat);
}
public static void exportManySheet(HttpServletResponse response, String fileName, Map<String, List<List<Object>>> sheetMap, String dateFormat) {
export(response, null, fileName, sheetMap, null, dateFormat);
}
public static void export(HttpServletResponse response, String fileName, String sheetName,
List<List<Object>> sheetDataList, String dateFormat) {
export(response, fileName, sheetName, sheetDataList, null, dateFormat);
}
public static void export(HttpServletResponse response, String fileName, String sheetName,
List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap, String dateFormat) {
Map<String, List<List<Object>>> map = new HashMap<>();
map.put(sheetName, sheetDataList);
export(response, null, fileName, map, selectMap, dateFormat);
}
public static <T, K> void export(HttpServletResponse response, String fileName, List<T> list, Class<K> template) {
String dateFormat = DATE_FORMAT;
// list 是否為空
boolean lisIsEmpty = list == null || list.isEmpty();
// 如果模板資料為空,且匯入的資料為空,則匯出空檔案
if (template == null && lisIsEmpty) {
exportEmpty(response, fileName, dateFormat);
return;
}
// 如果 list 資料,則匯出模板資料
if (lisIsEmpty) {
exportTemplate(response, fileName, template, dateFormat);
return;
}
// 匯出資料
List<List<Object>> sheetDataList = getSheetData(list);
export(response, fileName, sheetDataList, dateFormat);
}
public static <T, K> void export(HttpServletResponse response, String fileName, List<T> list, Class<K> template, String dateFormat) {
// list 是否為空
boolean lisIsEmpty = list == null || list.isEmpty();
// 如果模板資料為空,且匯入的資料為空,則匯出空檔案
if (template == null && lisIsEmpty) {
exportEmpty(response, fileName, dateFormat);
return;
}
// 如果 list 資料,則匯出模板資料
if (lisIsEmpty) {
exportTemplate(response, fileName, template, dateFormat);
return;
}
// 匯出資料
List<List<Object>> sheetDataList = getSheetData(list);
export(response, fileName, sheetDataList, dateFormat);
}
public static void export(HttpServletResponse response, String fileName, List<List<Object>> sheetDataList, Map<Integer, List<String>> selectMap, String dateFormat) {
export(response, fileName, fileName, sheetDataList, selectMap, dateFormat);
}
private static void export(HttpServletResponse response, File file, String fileName,
Map<String, List<List<Object>>> sheetMap, Map<Integer, List<String>> selectMap, String dateFormat) {
// 整個 Excel 表格 book 物件
SXSSFWorkbook book = new SXSSFWorkbook();
// 每個 Sheet 頁
Set<Entry<String, List<List<Object>>>> entries = sheetMap.entrySet();
for (Entry<String, List<List<Object>>> entry : entries) {
List<List<Object>> sheetDataList = entry.getValue();
Sheet sheet = book.createSheet(entry.getKey());
Drawing<?> patriarch = sheet.createDrawingPatriarch();
// 設定表頭背景色(灰色)
CellStyle headStyle = book.createCellStyle();
headStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.index);
headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headStyle.setAlignment(HorizontalAlignment.CENTER);
headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
// 設定表身背景色(預設色)
CellStyle rowStyle = book.createCellStyle();
rowStyle.setAlignment(HorizontalAlignment.CENTER);
rowStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//建立一個DataFormat物件
DataFormat format = book.createDataFormat();
rowStyle.setDataFormat(format.getFormat("@"));
// 設定表格列寬度(預設為15個位元組)
sheet.setDefaultColumnWidth(15);
// 建立合併演演算法陣列
int rowLength = sheetDataList.size();
int columnLength = sheetDataList.get(0).size();
int[][] mergeArray = new int[rowLength][columnLength];
for (int i = 0; i < sheetDataList.size(); i++) {
// 每個 Sheet 頁中的行資料
Row row = sheet.createRow(i);
List<Object> rowList = sheetDataList.get(i);
for (int j = 0; j < rowList.size(); j++) {
// 每個行資料中的單元格資料
Object o = rowList.get(j);
int v = 0;
if (o instanceof URL) {
// 如果要匯出圖片的話, 連結需要傳遞 URL 物件
setCellPicture(book, row, patriarch, i, j, (URL) o);
} else {
Cell cell = row.createCell(j);
if (i == 0) {
// 第一行為表頭行,採用灰色底背景
v = setCellValue(cell, o, headStyle, dateFormat);
} else {
// 其他行為資料行,預設白底色
v = setCellValue(cell, o, rowStyle, dateFormat);
}
}
mergeArray[i][j] = v;
}
}
// 合併單元格
mergeCells(sheet, mergeArray);
// 設定下拉選單
setSelect(sheet, selectMap);
}
// 寫資料
if (response != null) {
// 前端匯出
try {
write(response, book, fileName);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// 本地匯出
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
ByteArrayOutputStream ops = new ByteArrayOutputStream();
book.write(ops);
fos.write(ops.toByteArray());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 合併當前Sheet頁的單元格
*
* @param sheet 當前 sheet 頁
* @param mergeArray 合併單元格演演算法
*/
private static void mergeCells(Sheet sheet, int[][] mergeArray) {
// 橫向合併
for (int x = 0; x < mergeArray.length; x++) {
int[] arr = mergeArray[x];
boolean merge = false;
int y1 = 0;
int y2 = 0;
for (int y = 0; y < arr.length; y++) {
int value = arr[y];
if (value == CELL_COLUMN_MERGE) {
if (!merge) {
y1 = y;
}
y2 = y;
merge = true;
} else {
merge = false;
if (y1 > 0) {
sheet.addMergedRegion(new CellRangeAddress(x, x, (y1 - 1), y2));
}
y1 = 0;
y2 = 0;
}
}
if (y1 > 0) {
sheet.addMergedRegion(new CellRangeAddress(x, x, (y1 - 1), y2));
}
}
// 縱向合併
int xLen = mergeArray.length;
int yLen = mergeArray[0].length;
for (int y = 0; y < yLen; y++) {
boolean merge = false;
int x1 = 0;
int x2 = 0;
for (int x = 0; x < xLen; x++) {
int value = mergeArray[x][y];
if (value == CELL_ROW_MERGE) {
if (!merge) {
x1 = x;
}
x2 = x;
merge = true;
} else {
merge = false;
if (x1 > 0) {
sheet.addMergedRegion(new CellRangeAddress((x1 - 1), x2, y, y));
}
x1 = 0;
x2 = 0;
}
}
if (x1 > 0) {
sheet.addMergedRegion(new CellRangeAddress((x1 - 1), x2, y, y));
}
}
}
private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException {
/*response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String name = new String(fileName.getBytes("GBK"), "ISO8859_1") + XLSX;
response.addHeader("Content-Disposition", "attachment;filename=" + name);*/
/*String formFileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\";", formFileName));
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");*/
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
response.setContentType("multipart/form-data;charset=UTF-8");
String userAgent = request.getHeader("USER-AGENT");
try {
if (userAgent.contains("MSIE") || userAgent.contains("Edge")) {// IE瀏覽器
fileName = URLEncoder.encode(fileName, "UTF8");
} else if (userAgent.contains("Mozilla")) {// google,火狐瀏覽器
fileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
fileName = URLEncoder.encode(fileName, "UTF8");// 其他瀏覽器
}
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ServletOutputStream out = response.getOutputStream();
book.write(out);
out.flush();
out.close();
}
private static int setCellValue(Cell cell, Object o, CellStyle style, String dateFormat) {
// 設定樣式
cell.setCellStyle(style);
// 資料為空時
if (o == null) {
cell.setCellType(CellType.STRING);
cell.setCellValue("");
return CELL_OTHER;
}
// 是否為字串
if (o instanceof String) {
String s = o.toString();
if (isNumeric(s)) {
if (s.length() > 11) {
cell.setCellType(CellType.STRING);
cell.setCellValue(s);
return CELL_OTHER;
} else {
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(Double.parseDouble(s));
return CELL_OTHER;
}
} else {
cell.setCellType(CellType.STRING);
cell.setCellValue(s);
}
if (s.equals(ROW_MERGE)) {
return CELL_ROW_MERGE;
} else if (s.equals(COLUMN_MERGE)) {
return CELL_COLUMN_MERGE;
} else {
return CELL_OTHER;
}
}
// 是否為字串
if (o instanceof Integer || o instanceof Long || o instanceof Double || o instanceof Float) {
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(Double.parseDouble(o.toString()));
return CELL_OTHER;
}
// 是否為Boolean
if (o instanceof Boolean) {
cell.setCellType(CellType.BOOLEAN);
cell.setCellValue((Boolean) o);
return CELL_OTHER;
}
// 如果是BigDecimal,則預設3位小數
if (o instanceof BigDecimal) {
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(((BigDecimal) o).setScale(3, RoundingMode.HALF_UP).doubleValue());
return CELL_OTHER;
}
// 如果是Date資料,則顯示格式化資料
if (o instanceof Date) {
cell.setCellType(CellType.STRING);
cell.setCellValue(formatDate((Date) o, dateFormat));
return CELL_OTHER;
}
// 如果是其他,則預設字串型別
cell.setCellType(CellType.STRING);
cell.setCellValue(o.toString());
return CELL_OTHER;
}
private static void setCellPicture(SXSSFWorkbook wb, Row sr, Drawing<?> patriarch, int x, int y, URL url) {
// 設定圖片寬高
sr.setHeight((short) (IMG_WIDTH * IMG_HEIGHT));
// (jdk1.7版本try中定義流可自動關閉)
try (InputStream is = url.openStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buff = new byte[BYTES_DEFAULT_LENGTH];
int rc;
while ((rc = is.read(buff, 0, BYTES_DEFAULT_LENGTH)) > 0) {
outputStream.write(buff, 0, rc);
}
// 設定圖片位置
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, y, x, y + 1, x + 1);
// 設定這個,圖片會自動填滿單元格的長寬
anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);
patriarch.createPicture(anchor, wb.addPicture(outputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
} catch (Exception e) {
e.printStackTrace();
}
}
private static String formatDate(Date date, String dateFormat) {
if (date == null) {
return "";
}
if (dateFormat == null) {
dateFormat = DATE_FORMAT;
}
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return format.format(date);
}
private static void setSelect(Sheet sheet, Map<Integer, List<String>> selectMap) {
if (selectMap == null || selectMap.isEmpty()) {
return;
}
Set<Entry<Integer, List<String>>> entrySet = selectMap.entrySet();
for (Entry<Integer, List<String>> entry : entrySet) {
int y = entry.getKey();
List<String> list = entry.getValue();
if (list == null || list.isEmpty()) {
continue;
}
String[] arr = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
arr[i] = list.get(i);
}
DataValidationHelper helper = sheet.getDataValidationHelper();
CellRangeAddressList addressList = new CellRangeAddressList(1, 65000, y, y);
DataValidationConstraint dvc = helper.createExplicitListConstraint(arr);
DataValidation dv = helper.createValidation(dvc, addressList);
if (dv instanceof HSSFDataValidation) {
dv.setSuppressDropDownArrow(false);
} else {
dv.setSuppressDropDownArrow(true);
dv.setShowErrorBox(true);
}
sheet.addValidationData(dv);
}
}
private static boolean isNumeric(String str) {
if ("0.0".equals(str)) {
return true;
}
if ("".equals(str)) {
return false;
}
for (int i = str.length(); --i >= 0; ) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
private static String getString(String s) {
if (s == null) {
return "";
}
if (s.isEmpty()) {
return s;
}
return s.trim();
}
}
1.service層
/*** 車運運單資訊匯出*/
List<ExportBillVo> exportBillInfo(List<Long> billIdList);
2.impl實現類
實現類裡的程式碼,需求各位根據自己的業務場景進行改動,將需要匯出的資料查詢出來
@Override
public List<ExportBillVo> exportBillInfo(List<Long> billIdList) {
if (StringUtils.isEmpty(billIdList)) {
throw new ServiceException("車輛運單ID不能為空");
}
BillDto billDto = new BillDto();
billDto.setBillIdList(billIdList);
List<BillDto> billList = billFacadeService.queryBillList(billDto);
if (StringUtils.isEmpty(billList)) {
billList = new ArrayList<>();
}
return InvoiceWrapper.instance.convertTo(billList);
}
3.controller層
controller層的程式碼需要注意的是:
1.因為匯出Excel一般都是通過瀏覽器進行下載的,所以入參中需要加入HttpServletResponse
2.呼叫封裝的工具類ExcelUtils中的export方法就可以了
/**
* 車運運單資訊匯出
*/
@Log
@PostMapping("/exportBillInfo")
public void exportBillInfo(@RequestBody @Valid BillListDto dto, HttpServletResponse response) {
List<ExportBillVo> list = invoiceFacadeService.exportBillInfo(dto.getBillIdList());
ExcelUtils.export(response, "車運運單資訊", list, ExportBillVo.class);
}
最終匯出的Excel檔案: