` Controller层
@PostMapping(value = "/exportPlanDemandList")
@ApiOperation("需求条目导出")
@AdviceLog(value = "需求条目导出", type = LogType.OPERATE, operateType = LogOperateType.EXPORT)
public void exportDemandList(@RequestBody DemandPlanListVo vo, OutputStream out) {
try {
String sheetName = "需求条目";
// 获取所需数据的分页
Pager pager = demandPlanListService.getDemandListPage(vo);
List<Map<String, Object>> list = ConvertUtils.listConvert(pager.getRows());
String[] excelTilte = BasisFormationConstant.excelDemandList2;
String[] headerTitle = Arrays.stream(excelTilte).map(e -> e.split(":")[1]).toArray(size -> new String[size]);
String[] headerValues = Arrays.stream(excelTilte).map(e -> e.split(":")[0]).toArray(size -> new String[size]);
ExcelUtil.exportExcel2007WithSXSSF(sheetName, headerTitle, headerValues, list, out, null);
} catch (Exception e) {
e.printStackTrace();
}
}
复制代码
public class ConvertUtils {
private static final String CHINA_GREEN = "绿色";
private static final String ENG_GREEN = "green";
private static final String CHINA_RED = "红色";
private static final String ENG_RED = "red";
private static final String CHINA_ORANGE = "橙色";
private static final String ENG_ORANGE = "orange";
private static final String CHINA_PURPLE = "紫色";
private static final String ENG_PURPLE = "purple";
private static final String CHINA_YELLOW = "黄色";
private static final String ENG_YELLOW = "yellow";
private static final String CHINA_BLUE = "蓝色";
private static final String ENG_BLUE = "#0c60aa";
public static <E, T> List<T> convertList(List<E> sources, Class<T> c) {
if (CollectionUtils.isEmpty(sources)) {
return new ArrayList<T>();
}
List<T> list = new ArrayList<T>();
for (E source : sources) {
list.add(convertBean(source, c));
}
return list;
}
public static <T, E> T convertBean(E param, Class<T> tClass) {
try {
if (param == null) {
return tClass.newInstance();
}
T instance = tClass.newInstance();
BeanUtils.copyProperties(param, instance);
return instance;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
public static <T> List<Map<String, Object>> listConvert(List<T> list) {
List<Map<String, Object>> list_map = new ArrayList<Map<String, Object>>();
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(list)) {
list.forEach(item -> {
Map<String, Object> map = null;
try {
map = (Map<String, Object>) PropertyUtils.describe(item);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
list_map.add(map);
});
}
return list_map;
}
public static String convertBinaryColor(String color){
if(StringUtil.isNotEmpty(color)){
color=color.replace(CHINA_GREEN,ENG_GREEN);
color=color.replace(CHINA_RED,ENG_RED);
color=color.replace(CHINA_ORANGE,ENG_ORANGE);
color=color.replace(CHINA_PURPLE,ENG_PURPLE);
color=color.replace(CHINA_YELLOW,ENG_YELLOW);
color=color.replace(CHINA_BLUE,ENG_BLUE);
}
return color;
}
public static String convert(TSUser user){
if(user.getSecurityLevel() != null){
if (Globals.SecretLevel_One.shortValue() == user.getSecurityLevel())
{
return "非密";
}
else if (Globals.SecretLevel_Two.shortValue() == user.getSecurityLevel())
{
return "一般";
}
else if (Globals.SecretLevel_Three.shortValue() == user.getSecurityLevel())
{
return "重要";
}
else if (Globals.SecretLevel_Four.shortValue() == user.getSecurityLevel())
{
return "核心";
}
else
{
return "非密";
}
}
return "空";
}
public static String escapeStr(String value)
{
StringBuffer result=new StringBuffer();
if(value == null){
return null;
}else{
char content[] = new char[value.length()];
value.getChars(0, value.length(), content, 0);
result = new StringBuffer(content.length + 50);
for(int i = 0; i < content.length; i++)
switch(content[i])
{
case 34:
result.append(""");
break;
case 38:
result.append("&");
break;
case 39:
result.append("'");
break;
case 60:
result.append("<");
break;
case 62:
result.append(">");
break;
default:
result.append(content[i]);
break;
}
}
return result.toString();
}
}
复制代码
public class BasisFormationConstant {
public static final String[] excelDemandList2 = new String[] {
"num:编号",
"demandName:需求名称",
"demandCategoryName:需求类别",
"demandLevel:对应层级",
"demandTypeName:需求验证类型",
"demandProduct:承接产品",
"remark:说明",
"resourceName:需求来源",
"verifyStatus:验证状态",
"demandStatus:需求状态",
"isIcon:是否包含图标",
"createTime:创建时间"
};
}
复制代码
public class ExcelUtil {
public final static String EXCEL_FILE_2003 = "2003";
public final static String EXCEl_FILE_2007 = "2007";
public final static String EXCEl_FILE_2007_SXSSF = "2007_SXSSF";
public static void exportExcel(String title, String[] headersField, List<Map<String, Object>> dataList, OutputStream out,
String version) throws IOException {
if (StringUtils.isEmpty(version) || EXCEL_FILE_2003.equals(version.trim())) {
exportExcel2003(title, null, headersField, dataList, out, "yyyy-MM-dd HH:mm:ss");
} else if(EXCEl_FILE_2007_SXSSF.equals(version.trim())) {
exportExcel2007WithSXSSF(title, null, headersField, dataList, out, "yyyy-MM-dd HH:mm:ss");
} else {
exportExcel2007(title, null, headersField, dataList, out, "yyyy-MM-dd HH:mm:ss");
}
}
public static void exportExcel(String title, String[] headers, String[] headersField, List<Map<String, Object>> dataList,
OutputStream out, String version) throws IOException {
if (StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())) {
exportExcel2003(title, headers, headersField, dataList, out, "yyyy-MM-dd HH:mm:ss");
} else if(EXCEl_FILE_2007_SXSSF.equals(version.trim())) {
exportExcel2007WithSXSSF(title, headers, headersField, dataList, out, "yyyy-MM-dd HH:mm:ss");
} else {
exportExcel2007(title, headers, headersField, dataList, out, "yyyy-MM-dd HH:mm:ss");
}
}
public static void exportExcel2007WithSXSSF(String title, String[] headers, String[] headersField,
List<Map<String, Object>> dataList, OutputStream out, String pattern) throws IOException {
SXSSFWorkbook workbook = null;
try {
workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet(title);
sheet.setDefaultColumnWidth(30);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
org.apache.poi.ss.usermodel.Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
headerStyle.setFont(font);
CellStyle dataStyle = workbook.createCellStyle();
org.apache.poi.ss.usermodel.Font font2 = workbook.createFont();
dataStyle.setFont(font2);
Row row = sheet.createRow(0);
Cell cellHeader;
if(headers!=null){
for (int i = 0; i < headers.length; i++) {
cellHeader = row.createCell(i);
cellHeader.setCellStyle(headerStyle);
cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
}
}
Iterator<Map<String, Object>> it = dataList.iterator();
Map<String, Object> map;
int index = 0;
Object value;
String textValue;
Cell cell;
Matcher matcher;
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
SimpleDateFormat sdf=null;
if(StringUtils.isNotEmpty(pattern)){
sdf = new SimpleDateFormat(pattern);
}else{
sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
map = (Map<String,Object>) it.next();
for (int n = 0; n < headersField.length; n++) {
cell = row.createCell(n);
cell.setCellStyle(dataStyle);
value = map.get(headersField[n]);
textValue = null;
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
textValue = String.valueOf((Float) value);
cell.setCellValue(textValue);
} else if (value instanceof Double) {
textValue = String.valueOf((Double) value);
cell.setCellValue(textValue);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
}
if (value instanceof Boolean) {
textValue = "是";
if (!(Boolean) value) {
textValue = "否";
}
} else if (value instanceof Date) {
textValue = sdf.format((Date) value);
} else {
if (value != null) {
textValue = value.toString();
}
}
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(textValue);
}
}
}
}
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally {
if(workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void exportExcel2007(String title, String[] headers, String[] headersField,
List<Map<String, Object>> dataList, OutputStream out, String pattern) throws IOException {
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(title);
sheet.setDefaultColumnWidth(30);
XSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(new XSSFColor(new Color(217, 217, 217)));
XSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setColor(new XSSFColor(Color.BLACK));
font.setFontHeightInPoints((short) 16);
headerStyle.setFont(font);
XSSFCellStyle dataStyle = workbook.createCellStyle();
XSSFFont font2 = workbook.createFont();
dataStyle.setFont(font2);
XSSFRow row = sheet.createRow(0);
XSSFCell cellHeader;
if(headers!=null){
for (int i = 0; i < headers.length; i++) {
cellHeader = row.createCell(i);
cellHeader.setCellStyle(headerStyle);
cellHeader.setCellValue(new XSSFRichTextString(headers[i]));
}
}
Iterator<Map<String, Object>> it = dataList.iterator();
Map<String, Object> map;
int index = 0;
Object value;
String textValue;
XSSFCell cell;
XSSFRichTextString richString;
Matcher matcher;
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
SimpleDateFormat sdf=null;
if(StringUtils.isNotEmpty(pattern)){
sdf = new SimpleDateFormat(pattern);
}else{
sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
map = (Map<String,Object>) it.next();
for (int n = 0; n < headersField.length; n++) {
cell = row.createCell(n);
cell.setCellStyle(dataStyle);
value = map.get(headersField[n]);
textValue = null;
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
textValue = String.valueOf((Float) value);
cell.setCellValue(textValue);
} else if (value instanceof Double) {
textValue = String.valueOf((Double) value);
cell.setCellValue(textValue);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
}
if (value instanceof Boolean) {
textValue = "是";
if (!(Boolean) value) {
textValue = "否";
}
} else if (value instanceof Date) {
textValue = sdf.format((Date) value);
} else {
if (value != null) {
textValue = value.toString();
}
}
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
cell.setCellValue(Double.parseDouble(textValue));
} else {
richString = new XSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}
}
}
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally {
if(workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void exportExcel2003(String title, String[] headers, String[] headersField,
List<Map<String, Object>> dataList, OutputStream out, String pattern) throws IOException {
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(title);
sheet.setDefaultColumnWidth(20);
HSSFCellStyle headerStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);
headerStyle.setFont(font);
HSSFCellStyle dataStyle = workbook.createCellStyle();
HSSFFont font2 = workbook.createFont();
dataStyle.setFont(font2);
HSSFRow row = sheet.createRow(0);
HSSFCell cellHeader;
if(headers!=null && headers.length>0){
for (int i = 0; i < headers.length; i++) {
cellHeader = row.createCell(i);
cellHeader.setCellStyle(headerStyle);
cellHeader.setCellValue(new HSSFRichTextString(headers[i]));
}
}
Iterator<Map<String,Object>> it = dataList.iterator();
Map<String,Object> map;
int index = 0;
Object value;
String textValue;
HSSFCell cell;
HSSFRichTextString richString;
Matcher matcher;
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
map = (Map<String,Object>) it.next();
for (int n = 0; n < headersField.length; n++) {
cell = row.createCell(n);
cell.setCellStyle(dataStyle);
value = map.get(headersField[n]);
textValue = null;
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
textValue = String.valueOf((Float) value);
cell.setCellValue(textValue);
} else if (value instanceof Double) {
textValue = String.valueOf((Double) value);
cell.setCellValue(textValue);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
}
if (value instanceof Boolean) {
textValue = "是";
if (!(Boolean) value) {
textValue = "否";
}
} else if (value instanceof Date) {
textValue = sdf.format((Date) value);
} else {
if (value != null) {
textValue = value.toString();
}
}
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
cell.setCellValue(Double.parseDouble(textValue));
} else {
richString = new HSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}
}
}
workbook.write(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
throw e;
}finally {
if(workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码