话不多说直接上代码:
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;
}