話不多說直接上代碼:
maven
<!--操作excol表格-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
javaBean
@ToString
@Data
class User{
String name;
String sex;
Integer age;
String bir;
}
啓動類
//要寫入的對象
Workbook workbook = ExcelWriter.exportData(users);
// 以文件的形式輸出工作簿對象
FileOutputStream fileOut = null;
try {
//地址是我的桌面
String exportFilePath = "C:\\Users\\StaryL\\Desktop\\write.xlsx";
File exportFile = new File(exportFilePath);
if (!exportFile.exists()) {
exportFile.createNewFile();
}
//生成輸出流
fileOut = new FileOutputStream(exportFilePath);
//寫入數據
workbook.write(fileOut);
fileOut.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (null != fileOut) {
fileOut.close();
}
if (null != workbook) {
workbook.close();
}
} catch (IOException e) {
System.out.println("關閉輸出流時發生錯誤,錯誤原因:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
創建excel對象
/**
* 生成Excel並寫入數據信息
* @param users 數據列表
* @return 寫入數據後的工作簿對象
*/
public static Workbook exportData(List<User2> users) {
// 生成xlsx的Excel
// Workbook workbook = new SXSSFWorkbook();
// 如需生成xls的Excel,請使用下面的工作簿對象,注意後續輸出時文件後綴名也需更改爲xls
Workbook workbook = new HSSFWorkbook();
// 生成Sheet表,寫入第一行的列頭
Sheet sheet = buildDataSheet(workbook);
//構建每行的數據內容
int rowNum = 1;
for (Iterator<User2> it = users.iterator(); it.hasNext(); ) {
User2 user = it.next();
if (user == null) {
continue;
}
//輸出行數據
Row row = sheet.createRow(rowNum++);
//姓名
row.createCell(0).setCellValue(user.getName());
//年齡
row.createCell(1).setCellValue(user.getAge());
//所在城市
row.createCell(2).setCellValue(user.getLocation());
//職業
row.createCell(3).setCellValue(user.getJob());
//生日
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
String format = simpleDateFormat.format(user.getBri());
row.createCell(4).setCellValue(format);
}
return workbook;
}
創建列頭
private static List<String> CELL_HEADS; //列頭
static{
// 類裝載時就載入指定好的列頭信息,如有需要,可以考慮做成動態生成的列頭
CELL_HEADS = new ArrayList<>();
CELL_HEADS.add("姓名");
CELL_HEADS.add("年齡");
CELL_HEADS.add("居住城市");
CELL_HEADS.add("職業");
CELL_HEADS.add("生日");
}
創建一頁對象
private static Sheet buildDataSheet(Workbook workbook) {
//創建一頁
Sheet sheet = workbook.createSheet("信息表");
for (int i = 0; i < CELL_HEADS.size(); i++) {
//設置每個格子默認寬
sheet.setColumnWidth(i, 4000);
}
// 設置默認一行行高
sheet.setDefaultRowHeight((short) 1000);
// 構建頭單元格樣式
CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
// 寫入第一行各列的數據
Row head = sheet.createRow(0);
for (int j = 0; j < CELL_HEADS.size(); j++) {
//單個格子
Cell cell = head.createCell(j);
cell.setCellValue(CELL_HEADS.get(j));
cell.setCellStyle(cellStyle);
}
return sheet;
}
設置第一行列頭樣式
private static CellStyle buildHeadCellStyle(Workbook workbook) {
//樣式
CellStyle style = workbook.createCellStyle();
//對齊方式設置
style.setAlignment(HorizontalAlignment.CENTER);
//邊框顏色和寬度設置
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下邊框
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左邊框
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右邊框
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上邊框
//設置背景顏色
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//粗體字設置
Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);
return style;
}