package cc.mrbird.common.utils.poi;
import cc.mrbird.common.annotation.ExportConfig;
import cc.mrbird.common.utils.poi.convert.ExportConvert;
import cc.mrbird.common.utils.poi.pojo.ExportItem;
import com.csvreader.CsvWriter;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtils {
private Logger log = LoggerFactory.getLogger(this.getClass());
private Class<?> mClass = null;
private HttpServletResponse mResponse = null;
// 分Sheet机制:每个Sheet最多多少条数据
private Integer mMaxSheetRecords = 10000;
// 缓存数据格式器实例,避免多次使用反射进行实例化
private Map<String, ExportConvert> mConvertInstanceCache = new HashMap<>();
protected ExcelUtils() {
}
private ExcelUtils(Class<?> clazz) {
this(clazz, null);
}
private ExcelUtils(Class<?> clazz, HttpServletResponse response) {
this.mResponse = response;
this.mClass = clazz;
}
/**
* 用于生成本地文件
*
* @param clazz 实体Class对象
* @return ExcelUtils
*/
public static ExcelUtils builder(Class<?> clazz) {
return new ExcelUtils(clazz);
}
/**
* 用于浏览器导出
*
* @param clazz 实体Class对象
* @param response 原生HttpServletResponse对象
* @return ExcelUtils
*/
public static ExcelUtils export(Class<?> clazz, HttpServletResponse response) {
return new ExcelUtils(clazz, response);
}
/**
* 分Sheet机制:每个Sheet最多多少条数据(默认10000)
*
* @param size 数据条数
* @return this
*/
public ExcelUtils setMaxSheetRecords(Integer size) {
this.mMaxSheetRecords = size;
return this;
}
/**
* 导出Excel(此方式需依赖浏览器实现文件下载,故应先使用export()构造器)
*
* @param data 数据集合
* @param sheetName 工作表名字
* @return true-操作成功,false-操作失败
*/
public boolean toExcel(List<?> data, String sheetName) {
requiredexportParams();
try {
return toExcel(data, sheetName, mResponse.getOutputStream());
} catch (IOException e) {
log.error(e.getMessage());
}
return false;
}
/**
* 针对转换方法的默认实现(提供默认样式和文件命名规则)
*
* @param data 数据集合
* @param sheetName 工作表名字
* @param out 输出流
* @return true-操作成功,false-操作失败
*/
public boolean toExcel(List<?> data, String sheetName, OutputStream out) {
return toExcel(data, sheetName, new ExportHandler() {
@Override
public CellStyle headCellStyle(SXSSFWorkbook wb) {
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
cellStyle.setFillForegroundColor((short) 12);
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);// 填充模式
cellStyle.setBorderTop(CellStyle.BORDER_THIN);// 上边框为细边框
cellStyle.setBorderRight(CellStyle.BORDER_THIN);// 右边框为细边框
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);// 下边框为细边框
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);// 左边框为细边框
cellStyle.setAlignment(CellStyle.ALIGN_LEFT);// 对齐
cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
cellStyle.setFillBackgroundColor(HSSFColor.GREEN.index);
font.setBoldweight(Font.BOLDWEIGHT_NORMAL);
// font.setFontHeightInPoints((short) 12);// 字体大小
font.setColor(HSSFColor.WHITE.index);
// 应用标题字体到标题样式
cellStyle.setFont(font);
return cellStyle;
}
@Override
public String exportFileName(String sheetName) {
return String.format("导出-%s-%s", sheetName, System.currentTimeMillis());
}
}, out);
}
public boolean toExcel(List<?> data, String sheetName, ExportHandler handler, OutputStream out) {
requiredbuilderParams();
if (data == null || data.isEmpty()) {
return false;
}
// 导出列查询。
ExportConfig currentExportConfig;
ExportItem currentExportItem;
List<ExportItem> exportItems = new ArrayList<>();
for (Field field : mClass.getDeclaredFields()) {
currentExportConfig = field.getAnnotation(ExportConfig.class);
if (currentExportConfig != null) {
currentExportItem = new ExportItem().setField(field.getName())
.setDisplay("field".equals(currentExportConfig.value()) ? field.getName()
: currentExportConfig.value())
.setWidth(currentExportConfig.width()).setConvert(currentExportConfig.convert())
.setColor(currentExportConfig.color()).setReplace(currentExportConfig.replace());
exportItems.add(currentExportItem);
}
}
// 创建新的工作薄。
SXSSFWorkbook wb = POIUtils.newSXSSFWorkbook();
double sheetNo = Math.ceil((double) data.size() / mMaxSheetRecords);// 取出一共有多少个sheet.
// =====多sheet生成填充数据=====
int index = 0;
while (index <= (sheetNo == 0.0 ? sheetNo : sheetNo - 1)) {
SXSSFSheet sheet = POIUtils.newSXSSFSheet(wb, sheetName + (index == 0 ? "" : "_" + index));
// 创建表头
SXSSFRow headerRow = POIUtils.newSXSSFRow(sheet, 0);
for (int i = 0; i < exportItems.size(); i++) {
SXSSFCell cell = POIUtils.newSXSSFCell(headerRow, i);
POIUtils.setColumnWidth(sheet, i, exportItems.get(i).getWidth(), exportItems.get(i).getDisplay());
cell.setCellValue(exportItems.get(i).getDisplay());
CellStyle style = handler.headCellStyle(wb);
if (style != null) {
cell.setCellStyle(style);
}
}
SXSSFRow bodyRow;
String cellValue;
SXSSFCell cell;
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
style.setFont(font);
// 产生数据行
if (!data.isEmpty()) {
int startNo = index * mMaxSheetRecords;
int endNo = Math.min(startNo + mMaxSheetRecords, data.size());
int i = startNo;
while (i < endNo) {
bodyRow = POIUtils.newSXSSFRow(sheet, i + 1 - startNo);
for (int j = 0; j < exportItems.size(); j++) {
// 处理单元格值
cellValue = exportItems.get(j).getReplace();
if ("".equals(cellValue)) {
try {
cellValue = BeanUtils.getProperty(data.get(i), exportItems.get(j).getField());
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ssm权限管理系统.rar (413个子文件)
app.css 253KB
material-design-iconic-font.min.css 69KB
animate.min.css 52KB
style.min.css 27KB
jquery.scrollbar.css 22KB
sweetalert2.css 19KB
_all.css 14KB
daterangepicker.css 7KB
bootstrap-table.css 7KB
login.css 6KB
multiple-select.css 4KB
purple.css 2KB
orange.css 2KB
yellow.css 2KB
green.css 2KB
pink.css 1KB
blue.css 1KB
grey.css 1KB
aero.css 1KB
minimal.css 1KB
autocomplete.css 1KB
red.css 1KB
jquery.treegrid.css 913B
ip2region.db 2.6MB
Material-Design-Iconic-Font.eot 41KB
Roboto-Medium-webfont.eot 21KB
Roboto-Regular-webfont.eot 21KB
Roboto-Bold-webfont.eot 20KB
Roboto-Light-webfont.eot 20KB
throbber.gif 2KB
.gitattributes 27B
menuIcon.html 132KB
profile.html 15KB
500.html 10KB
404.html 10KB
403.html 10KB
userAdd.html 6KB
menuAdd.html 6KB
job.html 6KB
dict.html 5KB
login.html 5KB
user.html 4KB
menu.html 4KB
log.html 4KB
log.html 4KB
jobAdd.html 4KB
role.html 3KB
header.html 3KB
dept.html 3KB
dictAdd.html 3KB
social_regist.html 3KB
roleAdd.html 3KB
updatePassword.html 2KB
deptAdd.html 2KB
movieDetail.html 2KB
index.html 2KB
terminal.html 2KB
common.html 2KB
aside.html 1KB
info.html 1KB
weather.html 1KB
movieComments.html 980B
essay.html 938B
yuwen.html 938B
article.html 938B
bind_success.html 892B
unbind_success.html 836B
coming.html 595B
hot.html 592B
online.html 392B
paint.html 368B
main.html 314B
footer.html 138B
favicon.ico 4KB
ExcelUtils.java 13KB
FebsSecurityConfig.java 10KB
UserController.java 9KB
RedisInfo.java 9KB
MenuServiceImpl.java 7KB
UserServiceImpl.java 7KB
MyUser.java 7KB
MenuController.java 7KB
SocialController.java 6KB
RedisConfig.java 6KB
SessionController.java 6KB
FebsSocialWebAutoConfiguration.java 6KB
JobServiceImpl.java 6KB
JobController.java 6KB
SqlStatementInterceptor.java 6KB
LogAspect.java 6KB
FileUtils.java 6KB
HttpUtils.java 6KB
QQUserInfo.java 5KB
ScheduleUtils.java 5KB
RedisController.java 5KB
DeptController.java 5KB
SmsCodeFilter.java 5KB
RoleController.java 4KB
WeiXinOAuth2Template.java 4KB
Job.java 4KB
共 413 条
- 1
- 2
- 3
- 4
- 5
资源评论
一枚渣渣程序猿
- 粉丝: 50
- 资源: 328
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功