import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* @Description 生成Excel工具类
* @Author sinJack
*/
@Slf4j
public class ExcelUtils {
/**
* 生成Excel
* @Params titleName 标题名称;columnName 列名称;data 导出数据集
* @Return java.io.InputStream
*/
public InputStream createExcel(String titleName,List<String> columnName,List<List<Object>> data) {
// 生成文件
ByteArrayOutputStream out = null;
ExcelWriter writer = null;
InputStream input = null;
try {
out = new ByteArrayOutputStream();
// 通过工具类创建writer,生成xlsx格式
writer = ExcelUtil.getWriter(true);
//设置标题样式
Font font1 = writer.createFont();
font1.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
CellStyle headCellStyle = writer.getHeadCellStyle();
headCellStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
headCellStyle.setFont(font1);
//设置标题,列名
createTitle(writer,titleName,columnName);
writer.passCurrentRow();
writer.passCurrentRow();
writer.passCurrentRow();
writer.write(data, false);
writer.autoSizeColumnAll();
// 设置样式
this.setCellStyle(writer, data);
this.setSizeColumn(writer.getSheet(), data.size());
//out为OutputStream,需要写出到的目标流
writer.flush(out);
input = new ByteArrayInputStream(out.toByteArray());
} catch (Exception e) {
log.error("生成文件异常:", e);
return null;
} finally {
IoUtil.close(writer);
IoUtil.close(out);
IoUtil.close(input);
}
return input;
}
/**
* 创建excel,设置标题,列名
* @Params [writer, titleName, columnName]
* @Return void
*/
private void createTitle(ExcelWriter writer,String titleName,List<String> columnName) {
writer.merge(0, 0, 0, columnName.size()-1, titleName, true);
for(int i=0;i<columnName.size();i++){
writer.merge(1,2,i,i,columnName.get(i),true);
}
}
/**
* 设置样式
*
* @param writer
* @param rows
*/
private void setCellStyle(ExcelWriter writer, List<List<Object>> rows) {
//设置内容字体
Font font1 = writer.createFont();
font1.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
CellStyle headCellStyle = writer.getHeadCellStyle();
headCellStyle.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
headCellStyle.setFont(font1);
}
/**
* 自适应宽度(中文支持)
*
* @param sheet
* @param size
*/
private void setSizeColumn(Sheet sheet, int size) {
for (int columnNum = 0; columnNum < size; columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 256;
for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row currentRow;
// 当前行未被使用过
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(columnNum) != null) {
Cell currentCell = currentRow.getCell(columnNum);
if (currentCell.getCellType() == CellType.STRING) {
int length = currentCell.getStringCellValue().getBytes(StandardCharsets.UTF_8).length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
int width = columnWidth * 256 > 255 * 256 ? 250 * 256 : columnWidth * 256;
sheet.setColumnWidth(columnNum, width);
}
}
}