当前位置:网站首页>POI create and export Excel based on data
POI create and export Excel based on data
2022-04-23 03:18:00 【JavaTestZhangy】
Reprint
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>
* Set report name and style
* </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 height
row.setHeightInPoints(40);
Cell cell = row.createCell(0);
cell.setCellValue(titleName);
Workbook workbook = sheet.getWorkbook();
// Align center
CellStyle centerHr = workbook.createCellStyle();
centerHr.setVerticalAlignment(VerticalAlignment.CENTER);
centerHr.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(centerHr);
// Set the font
Font f = workbook.createFont();
f.setBold(true);
f.setFontHeightInPoints((short) 16);
cell.getCellStyle().setFont(f);
}
/**
* <p>
* Set header information
* </p>
*
* @param headersInfo
* @param sheet
*/
public static void outputHeaders(String[] headersInfo, Sheet sheet) {
Row row = sheet.createRow(1);
row.setHeightInPoints(32);
// Freeze line
sheet.createFreezePane(0, row.getRowNum() + 1);
Workbook workbook = sheet.getWorkbook();
// Chinese line style
CellStyle chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
// Bold font
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>
* Fill in the data
* </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) {
// The date type
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
cell.setCellValue(format.format(value));
} else {
cell.setCellValue(value.toString());
}
} else {
cell.setCellValue("");
}
cell.setCellStyle(defaultStyle);
}
}
}
/**
* <p>
* Value
* </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>
* Set header information
* </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();
// Chinese line style
CellStyle chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
// Bold font
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>
* Set header information
* </p>
*
* @param headersInfo
* @param sheet
*/
public static void outputSecondHeaders(String[] headersInfo, Sheet sheet) {
Row row = sheet.createRow(1);
row.setHeightInPoints(32);
// Freeze line
sheet.createFreezePane(0, row.getRowNum() + 2);
Workbook workbook = sheet.getWorkbook();
// Chinese line style
CellStyle chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle = workbook.createCellStyle();
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
chinaRowFontStyle.setVerticalAlignment(VerticalAlignment.CENTER);
chinaRowFontStyle.setAlignment(HorizontalAlignment.CENTER);
// Bold font
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://yzsam.com/2022/04/202204230314477681.html
边栏推荐
- Mysql database
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- Charles uses three ways to modify requests and responses
- Log4net is in Net core usage
- Configuration table and page information automatically generate curd operation page
- 软件测试相关知识~
- 移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
- 全新的ORM框架——BeetlSQL介绍
- ThreadLocal 测试多线程变量实例
- Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
猜你喜欢
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
Blazor University (12) - component lifecycle
It can receive multiple data type parameters - variable parameters
Ide-idea-problem
Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
一套组合拳,打造一款 IDEA 护眼方案
2022 P cylinder filling training test questions and simulation test
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
Configure automatic implementation of curd projects
2022T电梯修理考试模拟100题及在线模拟考试
随机推荐
Preview of converting doc and PDF to SWF file
Do you really understand hashcode and equals???
MySQL port is occupied when building xampp
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
MySQL installation pit
ASP. Net 6 middleware series - execution sequence
yes. Net future
Improvement of ref and struct in C 11
可以接收多種數據類型參數——可變參數
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
編碼電機PID調試(速度環|比特置環|跟隨)
Configuration table and page information automatically generate curd operation page
Maui initial experience: Cool
Configure automatic implementation of curd projects
A comprehensive understanding of static code analysis
svg标签中利用<polygon/>循环数组绘制多边形
If the deep replication of objects is realized through C #?
OLED多级菜单记录
场景题:A系统如何使用B系统的页面
一文了解全面静态代码分析