|
|
|
@ -9,15 +9,10 @@ import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
|
|
import org.apache.poi.ss.usermodel.BorderStyle;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
|
@ -38,7 +33,7 @@ import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import com.ruoyi.common.annotation.Excel;
|
|
|
|
@ -59,8 +54,89 @@ import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class ExcelUtil<T>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* src:定义下载的文件路径
|
|
|
|
|
*
|
|
|
|
|
* @param mapArrayList
|
|
|
|
|
* @param src
|
|
|
|
|
*/
|
|
|
|
|
public static boolean createExcel(List<Map<String, Object>> mapArrayList, String src) {
|
|
|
|
|
System.out.println("数据转成Excel...");
|
|
|
|
|
|
|
|
|
|
//获取数据源的 key, 用于获取列数及设置标题
|
|
|
|
|
Map<String, Object> map = mapArrayList.get(0);
|
|
|
|
|
Set<String> stringSet = map.keySet();
|
|
|
|
|
ArrayList<String> headList = new ArrayList<>(stringSet);
|
|
|
|
|
|
|
|
|
|
// 定义一个新的工作簿
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook();
|
|
|
|
|
// 创建一个Sheet页
|
|
|
|
|
XSSFSheet sheet = wb.createSheet("First sheet");
|
|
|
|
|
//设置行高
|
|
|
|
|
sheet.setDefaultRowHeight((short) (2 * 256));
|
|
|
|
|
//设置列宽
|
|
|
|
|
sheet.setColumnWidth(0, 4000);
|
|
|
|
|
sheet.setColumnWidth(1, 4000);
|
|
|
|
|
sheet.setColumnWidth(2, 4000);
|
|
|
|
|
XSSFFont font = wb.createFont();
|
|
|
|
|
font.setFontName("宋体");
|
|
|
|
|
font.setFontHeightInPoints((short) 16);
|
|
|
|
|
//获得表格第一行
|
|
|
|
|
XSSFRow row = sheet.createRow(0);
|
|
|
|
|
|
|
|
|
|
//chen 优化标题获取 根据数据源信息给第一行每一列设置标题
|
|
|
|
|
for (int i = 0; i < headList.size(); i++) {
|
|
|
|
|
XSSFCell cell = row.createCell(i);
|
|
|
|
|
cell.setCellValue(headList.get(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
XSSFRow rows;
|
|
|
|
|
XSSFCell cells;
|
|
|
|
|
//循环拿到的数据给所有行每一列设置对应的值
|
|
|
|
|
for (int i = 0; i < mapArrayList.size(); i++) {
|
|
|
|
|
|
|
|
|
|
// 在这个sheet页里创建一行
|
|
|
|
|
rows = sheet.createRow(i + 1);
|
|
|
|
|
//chen 优化值获取 给该行数据赋值
|
|
|
|
|
for (int j = 0; j < headList.size(); j++) {
|
|
|
|
|
String value;
|
|
|
|
|
if (mapArrayList.get(i).get(headList.get(j)) != null) {
|
|
|
|
|
value = mapArrayList.get(i).get(headList.get(j)).toString();
|
|
|
|
|
} else {
|
|
|
|
|
value = "";
|
|
|
|
|
}
|
|
|
|
|
cells = rows.createCell(j);
|
|
|
|
|
cells.setCellValue(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
String FileName = UUID.randomUUID().toString() + "_" + src + ".xlsx";
|
|
|
|
|
String downloadPath = Global.getDownloadPath() + FileName;
|
|
|
|
|
File desc = new File(downloadPath);
|
|
|
|
|
if (!desc.getParentFile().exists())
|
|
|
|
|
{
|
|
|
|
|
desc.getParentFile().mkdirs();
|
|
|
|
|
}
|
|
|
|
|
//File file = new File(src);
|
|
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(desc);
|
|
|
|
|
wb.write(fileOutputStream);
|
|
|
|
|
wb.close();
|
|
|
|
|
fileOutputStream.close();
|
|
|
|
|
return true;
|
|
|
|
|
// AjaxResult ajax = AjaxResult.success();
|
|
|
|
|
// return ajax;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
//return AjaxResult.error(e.getMessage());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|