import lombok.Data;
import java.util.List;
@Data
public class ExcelExp {
private String fileName;
private String[] handers;
private List<String[]> dataset;
public ExcelExp(String fileName, String[] handers, List<String[]> dataset) {
this.fileName = fileName;
this.handers = handers;
this.dataset = dataset;
}
}
import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.*;
@Slf4j
public class ExcelExportUtils {
public static void exportExcel(String upload, String fileName, List<ExcelExp> mysheets) {
HSSFWorkbook wb = new HSSFWorkbook();
List<ExcelExp> sheets = mysheets;
HSSFCellStyle style = wb.createCellStyle();
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setAlignment(HorizontalAlignment.CENTER);
HSSFFont fontStyle = wb.createFont();
fontStyle.setFontName("微软雅黑");
fontStyle.setFontHeightInPoints((short) 10);
style.setFont(fontStyle);
style.setWrapText(true);
for (ExcelExp excel : sheets) {
HSSFSheet sheet = wb.createSheet(excel.getFileName());
sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, excel.getHanders().length - 1));
Row rowTitle = sheet.createRow(0);
Cell cellOne = rowTitle.createCell(0);
cellOne.setCellValue(excel.getFileName() + "-" + DateUtils.getDate("yyyyMMdd"));
HSSFCellStyle cellTitleStyle = wb.createCellStyle();
cellTitleStyle.setAlignment(HorizontalAlignment.CENTER);
cellTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont titleFont = wb.createFont();
titleFont.setFontName("黑体");
titleFont.setFontHeightInPoints((short) 12);
cellTitleStyle.setFont(titleFont);
cellOne.setCellStyle(cellTitleStyle);
String[] handers = excel.getHanders();
HSSFRow rowFirst = sheet.createRow(1);
for (int i = 0; i < handers.length; i++) {
HSSFCell cell = rowFirst.createCell(i);
cell.setCellValue(handers[i]);
cell.setCellStyle(cellTitleStyle);
sheet.setColumnWidth(i, 6000);
sheet.setDefaultRowHeight((short) 400);
}
List<String[]> dataset = excel.getDataset();
for (int i = 0; i < dataset.size(); i++) {
String[] data = dataset.get(i);
HSSFRow row = sheet.createRow(i + 2);
for (int j = 0; j < data.length; j++) {
if (data[0].matches("[\u4e00-\u9fa5]+")) {
HSSFCell cellDate = row.createCell(j);
cellDate.setCellValue(data[j]);
cellDate.setCellStyle(cellTitleStyle);
sheet.setColumnWidth(j, 6000);
sheet.setDefaultRowHeight((short) 400);
} else {
HSSFCell cell = row.createCell(j);
cell.setCellValue(data[j]);
cell.setCellStyle(style);
}
}
}
}
try {
FileOutputStream output = new FileOutputStream(new File(upload + fileName));
wb.write(output);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)