pom.xml 引入jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
导出excel方法
package com.hebta.source.code.util;
import com.hebta.source.code.dto.ExcelBean;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ExcelExport {
private HSSFWorkbook wb;
private String fileName;
public ExcelExport(String fileName) {
if (wb == null)
this.wb = new HSSFWorkbook();
this.fileName = fileName;
}
public void setWorkBook(Workbook wb) {
this.wb = (HSSFWorkbook) wb;
}
public void doExportTwo(String path) throws Exception {
final OutputStream os = new FileOutputStream(new File(path + "/" + fileName));
this.wb.write(os);
os.flush();
os.close();
}
/**
*
* @param excelBeanList excel内容封装数据
* @param fileName 文件名称 【xxx.xls】
* @param filePath 文件保存地址 【/user/woke/temp/】
* @throws Exception
*/
public static void createExcel(List<ExcelBean> excelBeanList, String fileName, String filePath) throws Exception {
// 第一个对象,新建一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
for (ExcelBean excelBean : excelBeanList) {
// 设置第一个sheet的名称
HSSFSheet sheet = workbook.createSheet(excelBean.getSheetName());
// 开始添加excel第一行表头(excel中下标是0)
HSSFRow row = sheet.createRow(0);
// 设置excel宽度和高度
// sheet.setDefaultRowHeight((short) (3 * 256));
// sheet.setDefaultColumnWidth(25);
// 添加excel第一行表头信息(你想要添加的表头数据,集合类型,遍历放进去)
for (int i = 0; i < excelBean.getFieldDataList().size(); i++) {
// 创建一个单元格
HSSFCell cell = row.createCell(i);
// 设置单元格的样式,工具类在下面代码中会有放
CellStyle cellStyle= workbook.createCellStyle(); //设置样式
HSSFFont font = workbook.createFont();
font.setBold(true);
cellStyle.setFont(font);
//设置背景色
cellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
//必须设置 否则背景色不生效
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //设置垂直方法的对齐方式
cell.setCellStyle(cellStyle);
// 将数据放入excel的单元格中
HSSFRichTextString text = new HSSFRichTextString(excelBean.getFieldDataList().get(i));
cell.setCellValue(text);
}
// 开始创建excel单元格数据,从第二行开始(excel下标是1)
int rowNum = 1;
// 添加excel行数据的集合(你自己的数据集合遍历)
for (List<String> contents : excelBean.getContentList()) {
// 创建一个单元格
HSSFRow row1 = sheet.createRow(rowNum);
// 设置行的高度
row1.setHeightInPoints(20);
// 遍历你的表头数据,根据表头的数据获取对应的数据(我的数据是map的key,value形式)
for (int i = 0; i < contents.size(); i++) {
// 放入单元格中
row1.createCell(i).setCellValue(contents.get(i));
}
// 加一行,继续循环添加
rowNum++;
}
}
final ExcelExport export = new ExcelExport(fileName);
export.setWorkBook(workbook);
export.doExportTwo(filePath);
}
public static void main(String[] args) throws Exception {
List<ExcelBean> excelBeanList = new ArrayList<>();
ExcelBean excelBean = new ExcelBean();
String sheetName = "sheet名称1";
List<String> fieldDataList = Arrays.asList("姓名", "年龄", "身高");
List<List<String>> contentList = Arrays.asList(Arrays.asList("张三", "15", "154"),Arrays.asList("李四", "18", "178"),Arrays.asList("王五", "19", "180"));
excelBean.setSheetName(sheetName);
excelBean.setFieldDataList(fieldDataList);
excelBean.setContentList(contentList);
excelBeanList.add(excelBean);
createExcel(excelBeanList, "11.xls", "/user/workspace/demo/");
}
}
执行main方法之后生成结果如下: