当前位置:网站首页>poi根据数据创建导出excel
poi根据数据创建导出excel
2022-04-23 03:15:00 【JavaTestZhangy】
转载
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
/**
*
*/
public class ExcelExportUtil {
/**
* <p>
* 设置报表名称及样式
* </p>
*
* @param titleName
* @param sheet
* @param length
*/
public static void outputTitle(String titleName, Sheet sheet, int length) {
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, length));
Row row = sheet.createRow(0);
// 行高
row.setHeightInPoints(40);
Cell cell = row.createCell(0);
cell.setCellValue(titleName);
Workbook workbook = sheet.getWorkbook();
// 居中对齐
CellStyle centerHr = workbook.createCellStyle();
centerHr.setVerticalAlignment(VerticalAlignment.CENTER);
centerHr.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(centerHr);
// 设置字体
Font f = workbook.createFont();
f.setBold(true);
f.setFontHeightInPoints((short) 16);
cell.getCellStyle().setFont(f);
}
/**
* <p>
* 设置表头信息
* </p>
*
* @param headersInfo
* @param sheet
*/
public static void outputHeaders(String[] headersInfo, Sheet sheet) {
Row row = sheet.createRow(1);
row.setHeightInPoints(32);
// 冻结行
sheet.createFreezePane(0, row.getRowNum() + 1);
Workbook workbook = sheet.getWorkbook();
// 中文行样式
CellStyle chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
// 字体加粗
Font f1 = workbook.createFont();
f1.setBold(true);
f1.setFontHeightInPoints((short) 10);
chinaRowFontStyle.setFont(f1);
for (int i = 0; i < headersInfo.length; i++) {
sheet.setColumnWidth(i, 4000);
Cell cell = row.createCell(i);
cell.setCellValue(headersInfo[i]);
cell.setCellStyle(chinaRowFontStyle);
}
}
/**
* <p>
* 填充数据
* </p>
*
* @param headersInfo
* @param columnsInfo
* @param sheet
* @param rowIndex
*/
public static void outputColumns(String[] headersInfo, @SuppressWarnings("rawtypes") List columnsInfo, Sheet sheet,
int rowIndex) {
Row row;
@SuppressWarnings("unused")
int headerSize = headersInfo.length;
Workbook workbook = sheet.getWorkbook();
CellStyle defaultStyle = workbook.createCellStyle();
defaultStyle.setVerticalAlignment(VerticalAlignment.CENTER);
defaultStyle.setAlignment(HorizontalAlignment.CENTER);
Font f1 = workbook.createFont();
f1.setFontHeightInPoints((short) 10);
defaultStyle.setFont(f1);
for (int i = 0; i < columnsInfo.size(); i++) {
row = sheet.createRow(rowIndex + i);
Object obj = columnsInfo.get(i);
for (int j = 0; j < headersInfo.length; j++) {
Object value = getFieldValueByName(headersInfo[j], obj);
Cell cell = row.createCell(j);
if (value != null) {
if (value instanceof Date) {
// 日期类型
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
cell.setCellValue(format.format(value));
} else {
cell.setCellValue(value.toString());
}
} else {
cell.setCellValue("");
}
cell.setCellStyle(defaultStyle);
}
}
}
/**
* <p>
* 取值
* </p>
*
* @param fieldName
* @param obj
* @return
*/
private static Object getFieldValueByName(String fieldName, Object obj) {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
try {
Method method = obj.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(obj, new Object[] {});
return value;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* 设置表头信息
* </p>
*
* @param headersInfo
* @param sheet
*/
public static void outputFirstHeaders(String[] headersInfo, Sheet sheet) {
Row row = sheet.createRow(0);
row.setHeightInPoints(32);
sheet.createFreezePane(0, row.getRowNum() + 1);
Workbook workbook = sheet.getWorkbook();
// 中文行样式
CellStyle chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
// 字体加粗
Font f1 = workbook.createFont();
f1.setBold(true);
f1.setFontHeightInPoints((short) 10);
chinaRowFontStyle.setFont(f1);
for (int i = 0; i < headersInfo.length; i++) {
sheet.setColumnWidth(i, 4000);
Cell cell = row.createCell(i);
cell.setCellValue(headersInfo[i]);
cell.setCellStyle(chinaRowFontStyle);
}
}
/**
* <p>
* 设置表头信息
* </p>
*
* @param headersInfo
* @param sheet
*/
public static void outputSecondHeaders(String[] headersInfo, Sheet sheet) {
Row row = sheet.createRow(1);
row.setHeightInPoints(32);
// 冻结行
sheet.createFreezePane(0, row.getRowNum() + 2);
Workbook workbook = sheet.getWorkbook();
// 中文行样式
CellStyle chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
// 字体加粗
Font f1 = workbook.createFont();
f1.setBold(true);
f1.setFontHeightInPoints((short) 10);
chinaRowFontStyle.setFont(f1);
for (int i = 0; i < headersInfo.length; i++) {
sheet.setColumnWidth(i, 4000);
Cell cell = row.createCell(i);
cell.setCellValue(headersInfo[i]);
cell.setCellStyle(chinaRowFontStyle);
}
}
}
版权声明
本文为[JavaTestZhangy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_23490959/article/details/84876210
边栏推荐
- 2022年度Top9的任务管理系统
- The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
- Experiment 5 components and event handling
- Fundamentals of software testing and development
- 使用DFS来解决“字典序排数”问题
- Comprehensive calculation of employee information
- Peut recevoir plusieurs paramètres de type de données - paramètres variables
- 2022t elevator repair test simulation 100 questions and online simulation test
- 編碼電機PID調試(速度環|比特置環|跟隨)
- 软件测试相关知识~
猜你喜欢

Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

Experiment 6 input / output stream

Laravel new route file

Student achievement management

2022A特种设备相关管理(电梯)上岗证题库及模拟考试

This new feature of C 11, I would like to call it the strongest!

Impact of AOT and single file release on program performance

Drawing polygons with < polygon / > circular array in SVG tag

LoadRunner - performance testing tool
随机推荐
[new version release] componentone added Net 6 and blazor platform control support
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
使用DFS来解决“字典序排数”问题
Establishing and traversing binary tree
2022g2 boiler stoker examination question bank and online simulation examination
C syntax pattern matching [switch expression]
EasyUI's combobox implements three-level query
Laravel8- use JWT
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
A set of combination boxing to create an idea eye protection scheme
Maui initial experience: Cool
Impact of AOT and single file release on program performance
. net core current limiting control - aspnetcoreratelimit
Load view Caton
手机连接电脑后,QT的QDIR怎么读取手机文件路径
队列的存储和循环队列
js递归树结构计算每个节点的叶子节点的数量并且输出
ASP. Net 6 middleware series - execution sequence
C language to achieve address book - (static version)