1.最近写了一个导出工具类,可以适用于不同环境下的导出工具
package com.pcdtech.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class ExportExcelUtil {
private SimpleDateFormat timeSdf = new SimpleDateFormat("yyyyMMddHHmmss");
private SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd");
private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
@Value("${file.upload.path}")
private String file_upload_path;
/**
* 导出功能
* @param headZhList 表头中文名 // 就是中文列头
* @param headEnList 表头字段名 // 数据对应的字段名
* @param dataList 数据集合
* @param response 相应
* @throws Exception
*/
public Map<String,Object> exportExcel(List<String> headZhList,List<String> headEnList, List dataList,String legalRepCode) throws Exception {
if (UtilValidate.isEmpty(headZhList) || UtilValidate.isEmpty(headEnList) ||
headEnList.size() != headEnList.size() ||
UtilValidate.isEmpty(dataList)) {
log.error("数据错误 headZhList:" + headZhList + " headEnList:" + headEnList + " dataList:" + dataList);
Map<String,Object> map = new HashMap<>();
return map;
}
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet();
// 设置表格默认列宽度为30个字节
sheet.setDefaultColumnWidth((short) 30);
Row headRow = sheet.createRow(0);
Cell headCell;
//表头信息
for(int i =0;i<headZhList.size();i++){
headCell = headRow.createCell(i);
headCell.setCellValue(headZhList.get(i));
}
//数据信息
for (int i =0;i<dataList.size();i++) {
Row dataRow = sheet.createRow(i+1);
Cell dataCell;
for(int j = 0;j< headEnList.size();j++){
dataCell = dataRow.createCell(j);
Map<String,Object> genericValue = (Map<String, Object>) dataList.get(i);
Object convertValue = genericValue.get(headEnList.get(j));
setCellValue(convertValue,dataCell);
}
}
String fileTimeSdf= timeSdf.format(new Date());
File toFile = new File(fileTimeSdf+".xlsx");
OutputStream os = new FileOutputStream(toFile);
workbook.write(os);
MultipartFile cMultiFile = new MockMultipartFile("file", toFile.getName(),null, new FileInputStream(toFile));
Map<String ,Object> map = upload(cMultiFile,legalRepCode);
return map;
}
private String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
private Map<String, Object> upload(MultipartFile file,String legalRepCode){
Map<String, Object> retMap = new HashMap<String, Object>();
String fileName = file.getOriginalFilename();
int endIndex = fileName.lastIndexOf(".");
int length = fileName.length();
String fileType = fileName.substring(endIndex + 1, length);
String filePartentName = fileName.substring(0, endIndex);
String filename_path = filePartentName + "_" + getCurrentTime() + "." + fileType;
String absolatePath = File.separator + legalRepCode + File.separator + (sdf.format(new Date())) + File.separator + filename_path;//file.getOriginalFilename();
String filePath = file_upload_path + absolatePath;
try {
File isfile = new File(file_upload_path + File.separator + legalRepCode + File.separator + (sdf.format(new Date())));
if (!isfile.exists()) {
boolean mkFlg = isfile.mkdirs();
if (!mkFlg) {
throw new FileNotFoundException();
}
}
byte[] bytes = file.getBytes();
File fileToSave = new File(filePath);
FileCopyUtils.copy(bytes, fileToSave);
retMap.put("status", "000");
retMap.put("desc", "success");
retMap.put("fileurlList", "/ehr-file-service/file/viewController?picturePath=" + URLEncoder.encode(absolatePath));
retMap.put("absolatePath", filePath);
return retMap;
}catch (Exception e) {
retMap.put("status", "999");
retMap.put("desc", "error");
log.error(e.getMessage(), e);
return retMap;
}
}
private void setCellValue(Object value,Cell cell){
if (value == null) {
cell.setCellValue("");
}
if (value instanceof Integer) {
int intValue = (Integer) value;
cell.setCellValue(intValue);
} else if (value instanceof Float) {
float fValue = (Float) value;
cell.setCellValue(fValue);
} else if (value instanceof Double) {
double dValue = (Double) value;
cell.setCellValue(dValue);
} else if (value instanceof Long) {
long longValue = (Long) value;
cell.setCellValue(longValue);
} else if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String textValue = sdf.format(date);
cell.setCellValue(textValue);
} else {
// 其它数据类型都当作字符串简单处理
String textValue = value==null? "":value.toString();
cell.setCellValue(textValue);
}
}
}