人员导出
parent
0ad6157c26
commit
783b9fc6bf
@ -0,0 +1,41 @@
|
||||
package com.engine.organization.util.excel;
|
||||
|
||||
public enum BooleanEnum {
|
||||
|
||||
True0("是", Boolean.TRUE),
|
||||
True1("Y", Boolean.TRUE),
|
||||
True2("TRUE", Boolean.TRUE),
|
||||
True3("1", Boolean.TRUE),
|
||||
True4("YES", Boolean.TRUE),
|
||||
True5("T", Boolean.TRUE),
|
||||
False0("否", Boolean.FALSE),
|
||||
False1("N", Boolean.FALSE),
|
||||
False2("FALSE", Boolean.FALSE),
|
||||
False3("0", Boolean.FALSE),
|
||||
False4("NO", Boolean.FALSE),
|
||||
False5("F", Boolean.FALSE);
|
||||
|
||||
private String name;
|
||||
private Boolean value;
|
||||
|
||||
private BooleanEnum(String name, Boolean value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Boolean getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Boolean value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
package com.engine.organization.util.excel;
|
||||
|
||||
/**
|
||||
* 注释
|
||||
*/
|
||||
public class ExcelComment {
|
||||
int dx1 = 0;
|
||||
int dy1 = 0;
|
||||
int dx2 = 0;
|
||||
int dy2 = 0;
|
||||
int col1 = 0;
|
||||
int row1 = 0;
|
||||
int col2 = 0;
|
||||
int row2 = 0;
|
||||
String content;
|
||||
|
||||
public int getDx1() {
|
||||
return dx1;
|
||||
}
|
||||
|
||||
public void setDx1(int dx1) {
|
||||
this.dx1 = dx1;
|
||||
}
|
||||
|
||||
public int getDy1() {
|
||||
return dy1;
|
||||
}
|
||||
|
||||
public void setDy1(int dy1) {
|
||||
this.dy1 = dy1;
|
||||
}
|
||||
|
||||
public int getDx2() {
|
||||
return dx2;
|
||||
}
|
||||
|
||||
public void setDx2(int dx2) {
|
||||
this.dx2 = dx2;
|
||||
}
|
||||
|
||||
public int getDy2() {
|
||||
return dy2;
|
||||
}
|
||||
|
||||
public void setDy2(int dy2) {
|
||||
this.dy2 = dy2;
|
||||
}
|
||||
|
||||
public int getCol1() {
|
||||
return col1;
|
||||
}
|
||||
|
||||
public void setCol1(int col1) {
|
||||
this.col1 = col1;
|
||||
}
|
||||
|
||||
public int getRow1() {
|
||||
return row1;
|
||||
}
|
||||
|
||||
public void setRow1(int row1) {
|
||||
this.row1 = row1;
|
||||
}
|
||||
|
||||
public int getCol2() {
|
||||
return col2;
|
||||
}
|
||||
|
||||
public void setCol2(int col2) {
|
||||
this.col2 = col2;
|
||||
}
|
||||
|
||||
public int getRow2() {
|
||||
return row2;
|
||||
}
|
||||
|
||||
public void setRow2(int row2) {
|
||||
this.row2 = row2;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public ExcelComment(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2, String content) {
|
||||
this.dx1 = dx1;
|
||||
this.dy1 = dy1;
|
||||
this.dx2 = dx2;
|
||||
this.dy2 = dy2;
|
||||
this.col1 = col1;
|
||||
this.row1 = row1;
|
||||
this.col2 = col2;
|
||||
this.row2 = row2;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public ExcelComment(int col1, int row1, int col2, int row2, String content) {
|
||||
this.col1 = col1;
|
||||
this.row1 = row1;
|
||||
this.col2 = col2;
|
||||
this.row2 = row2;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public ExcelComment() {
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.engine.organization.util.excel;
|
||||
import org.apache.commons.lang3.exception.ContextedRuntimeException;
|
||||
|
||||
public class ExcelParseException extends ContextedRuntimeException{
|
||||
|
||||
private static final long serialVersionUID = -8696742623977630854L;
|
||||
|
||||
public ExcelParseException(String message) {
|
||||
super(message);
|
||||
this.msgCode = DEFAULT_CODE;
|
||||
}
|
||||
|
||||
public ExcelParseException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.msgCode = DEFAULT_CODE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认异常编码
|
||||
*/
|
||||
private static final String DEFAULT_CODE = "EXCP0000";
|
||||
|
||||
/**
|
||||
* 异常编码
|
||||
*/
|
||||
private String msgCode;
|
||||
|
||||
public String getMsgCode() {
|
||||
return msgCode;
|
||||
}
|
||||
|
||||
public void setMsgCode(String msgCode) {
|
||||
this.msgCode = msgCode;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,196 @@
|
||||
package com.engine.organization.util.excel;
|
||||
|
||||
import com.engine.organization.util.OrganizationDateUtil;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.apache.poi.xssf.usermodel.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class ExcelUtil {
|
||||
/**
|
||||
* 生成excel
|
||||
*
|
||||
* @param rowList
|
||||
* @return
|
||||
*/
|
||||
public static XSSFWorkbook genWorkbook(List<List<String>> rowList, String sheetName) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
// 设置title样式
|
||||
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
||||
XSSFFont titleFont = workbook.createFont();
|
||||
titleFont.setFontName("仿宋");
|
||||
titleFont.setFontHeightInPoints((short) 15);
|
||||
titleCellStyle.setFont(titleFont);
|
||||
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
|
||||
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
|
||||
|
||||
// 设置主体样式
|
||||
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
||||
XSSFFont font = workbook.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
||||
cellStyle.setFont(font);// 选择需要用到的字体格式
|
||||
cellStyle.setWrapText(true);
|
||||
|
||||
XSSFSheet sheet = workbook.createSheet(sheetName);
|
||||
//自适应宽度
|
||||
sheet.autoSizeColumn(0, true);
|
||||
//默认列宽
|
||||
sheet.setDefaultColumnWidth(40);
|
||||
//默认行高
|
||||
sheet.setDefaultRowHeightInPoints(18);
|
||||
|
||||
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
||||
List<String> infoList = rowList.get(rowIndex);
|
||||
XSSFRow row = sheet.createRow(rowIndex);
|
||||
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
||||
XSSFCell cell = row.createCell(cellIndex);
|
||||
cell.setCellType(CellType.STRING);
|
||||
if (rowIndex == 0) {
|
||||
cell.setCellStyle(titleCellStyle);
|
||||
} else {
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
cell.setCellValue(infoList.get(cellIndex));
|
||||
// sheet.setColumnWidth(cellIndex, 35 * 256);
|
||||
}
|
||||
}
|
||||
return workbook;
|
||||
}
|
||||
|
||||
|
||||
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
// 设置title样式
|
||||
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
||||
XSSFFont titleFont = workbook.createFont();
|
||||
titleFont.setFontName("仿宋");
|
||||
titleFont.setFontHeightInPoints((short) 15);
|
||||
titleCellStyle.setFont(titleFont);
|
||||
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
|
||||
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
|
||||
|
||||
// 设置主体样式
|
||||
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
||||
XSSFFont font = workbook.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
||||
cellStyle.setFont(font);// 选择需要用到的字体格式
|
||||
cellStyle.setWrapText(true);
|
||||
|
||||
XSSFSheet sheet = workbook.createSheet(sheetName);
|
||||
//自适应宽度
|
||||
sheet.autoSizeColumn(0, true);
|
||||
//默认列宽
|
||||
sheet.setDefaultColumnWidth(100);
|
||||
//默认行高
|
||||
sheet.setDefaultRowHeightInPoints(18);
|
||||
|
||||
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
||||
List<Object> infoList = rowList.get(rowIndex);
|
||||
XSSFRow row = sheet.createRow(rowIndex);
|
||||
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
||||
XSSFCell cell = row.createCell(cellIndex);
|
||||
if (rowIndex == 0) {
|
||||
cell.setCellStyle(titleCellStyle);
|
||||
} else {
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
Object o = infoList.get(cellIndex);
|
||||
if (o instanceof String) {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
} else if (o instanceof Boolean) {
|
||||
cell.setCellType(CellType.BOOLEAN);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
} else if (o instanceof Date) {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(OrganizationDateUtil.getFormatLocalDate((Date) o));
|
||||
} else {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(o == null ? "" : o.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return workbook;
|
||||
}
|
||||
|
||||
|
||||
public static XSSFWorkbook genWorkbookV2(List<List<Object>> rowList, String sheetName, List<ExcelComment> comments) {
|
||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||
|
||||
// 设置title样式
|
||||
XSSFCellStyle titleCellStyle = workbook.createCellStyle();
|
||||
XSSFFont titleFont = workbook.createFont();
|
||||
titleFont.setFontName("仿宋");
|
||||
titleFont.setFontHeightInPoints((short) 15);
|
||||
titleCellStyle.setFont(titleFont);
|
||||
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
titleCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());//背景色
|
||||
titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
|
||||
|
||||
// 设置主体样式
|
||||
XSSFCellStyle cellStyle = workbook.createCellStyle();
|
||||
XSSFFont font = workbook.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);// 设置字体大小
|
||||
cellStyle.setFont(font);// 选择需要用到的字体格式
|
||||
cellStyle.setWrapText(true);
|
||||
|
||||
XSSFSheet sheet = workbook.createSheet(sheetName);
|
||||
//自适应宽度
|
||||
sheet.autoSizeColumn(0, true);
|
||||
//默认列宽
|
||||
sheet.setDefaultColumnWidth(20);
|
||||
//默认行高
|
||||
sheet.setDefaultRowHeightInPoints(18);
|
||||
|
||||
for (int rowIndex = 0; rowIndex < rowList.size(); rowIndex++) {
|
||||
List<Object> infoList = rowList.get(rowIndex);
|
||||
XSSFRow row = sheet.createRow(rowIndex);
|
||||
for (int cellIndex = 0; cellIndex < infoList.size(); cellIndex++) {
|
||||
XSSFCell cell = row.createCell(cellIndex);
|
||||
if (rowIndex == 0) {
|
||||
cell.setCellStyle(titleCellStyle);
|
||||
} else {
|
||||
cell.setCellStyle(cellStyle);
|
||||
}
|
||||
Object o = infoList.get(cellIndex);
|
||||
if (o instanceof String) {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
} else if (o instanceof Boolean) {
|
||||
cell.setCellType(CellType.BOOLEAN);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
} else if (o instanceof Date) {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(OrganizationDateUtil.getFormatLocalDate((Date) o));
|
||||
} else {
|
||||
cell.setCellType(CellType.STRING);
|
||||
cell.setCellValue(String.valueOf(o));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(comments)) {
|
||||
for (ExcelComment c : comments) {
|
||||
XSSFDrawing patr = sheet.createDrawingPatriarch();
|
||||
XSSFComment cellComment = patr.createCellComment(new XSSFClientAnchor(c.dx1, c.dy1, c.dx2, c.dy2, c.col1, c.row1, c.col2, c.row2));
|
||||
cellComment.setString(c.content);
|
||||
}
|
||||
}
|
||||
return workbook;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue