POI excel导出
Posted antlord
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POI excel导出相关的知识,希望对你有一定的参考价值。
*******************************
excel表格导出,使用POI实现
*******************************
实现导出步骤
——配置导出excel模板,推荐使用03版xls格式,可以兼容高级版本
——处理导出数据List<Map<String, String>>
private List<Map<String, String>> getData(){
List<Map<String, String>> data = new ArrayList<>();
Map<String, String> map = null;
for (int i = 0; i < 5; i++) {
map = new HashMap<String, String>();
map.put("listNo", i + "");
map.put("userName", "name" + i);
map.put("userAge", "" + (i + 20));
data.add(map);
}
return data;
}
——获取模板xls文件,通过输入流读到HSSFWorkbook 对象中
private HSSFWorkbook workbookTemplate() {
File file = new File("f:/etom/pmms/src/test/resources/bdRoute.xls");
InputStream is = null;
HSSFWorkbook workbook = null;
try {
is = new FileInputStream(file);
workbook = new HSSFWorkbook(is);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return workbook;
}
——通过页签名获取页签
private HSSFSheet getSheet(HSSFWorkbook workbook) {
String sheetName = "用户信息";
HSSFSheet sheet = workbook.getSheet(sheetName);
if (null != sheet) {
return sheet;
}
return null;
}
——获取规则,如${userName}
private String getValue(HSSFCell cell){
int type = cell.getCellType();
switch (type) {
case Cell.CELL_TYPE_NUMERIC:
return Double.toString(cell.getNumericCellValue());
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "";
}
}
——设置规则,如${userName}
private void setCellRule(HSSFRow firstDataRow, HSSFRow newRow) {
HSSFCell cellInRule = null;
HSSFCell newCell = null;
for (int i = 0, cellNum = firstDataRow.getLastCellNum(); i < cellNum; i++) {
cellInRule = firstDataRow.getCell(i);
newCell = newRow.createCell(i);
HSSFCellStyle style = cellInRule.getCellStyle();
newCell.setCellStyle(style);
String rule = getValue(cellInRule);
newCell.setCellValue(rule);
}
}
——设置页签模板,将内容行复制,行数与数据data的条数一致
// loop复制行的根据,rowCount复制多少行,上面截图中,复制行是从行号为4开始
private void setSheet(HSSFSheet sheet, int loop, int rowCount) {
HSSFRow newRow = null;
HSSFRow firstDataRow = sheet.getRow(loop - 1);
for (int i = loop, maxRow = loop + rowCount; i < maxRow - 1; i++) {
newRow = sheet.createRow(i);
setCellRule(firstDataRow, newRow);
}
}
——具体设置单元格内容
private void setCellValue(HSSFRow row, Map<String, String> rowData) {
for (int i = 0, cellNum = row.getLastCellNum(); i < cellNum; i++) {
HSSFCell cell = row.getCell(i);
if (null == cell) {
continue;
}
String oldValue = getValue(cell);
Pattern pattern = Pattern.compile(patternRule);
Matcher matcher = pattern.matcher(oldValue);
if (matcher.find()) {
String key = matcher.group(1);
String value = rowData.getOrDefault(key, "");
cell.setCellValue(value);
}
}
}
——设置行内容(通过设置单元格内容)
// 设置内容,单元的范围是从第loop行开始,到第loop+data.size()行结束
private void setValue(HSSFSheet sheet, int loop, List<Map<String, String>> data) {
HSSFRow row = null;
for (int i = loop - 1, rowNum = loop + data.size(), index = 0; i < rowNum - 1; i++) {
row = sheet.getRow(i);
setCellValue(row, data.get(index++));
}
}
——生成excel文件,格式为xls
private void createExcel(HSSFWorkbook workbook,String targetPath){
File excelFile = new File(targetPath);
OutputStream os = null;
try {
os = new FileOutputStream(excelFile);
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != os) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
——测试
@Test
public void test(){
List<Map<String, String>> data = getData(); // 获取导出的数据
String templatePath = "f:/etom/pmms/src/test/resources/user.xls"; // 模板的地址
HSSFWorkbook workbook = workbookTemplate(templatePath); // 将模板转化为HSSFWorkbook对象
HSSFSheet sheet = getSheet(workbook); // 获取模板页签
setSheet(sheet, 5, data.size()); // 设置页签
setValue(sheet, 5, data); // 设置单元格内容
Long timeMillis = System.currentTimeMillis(); // 以当前时间命名导出文件名
String targetPath = String.format("f:/etom/pmms/src/test/resources/%s.xls", Long.toString(timeMillis)); // 导出文件xls地址
createExcel(workbook, targetPath); // 导出文件
}
——结果展示
——合并单元格
private void setRegion(HSSFSheet sheet){
int firstRow = 1; // 单元起始行
int lastRow = 2; // 单元格终止行
int firstCol = 3; // 单元格起始列
int lastCol = 4; // 单元格终止列
CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheet.addMergedRegion(cellRangeAddress);
}
——插入图片(图片已保存在资源中)
public void insertBatch(HSSFWorkbook workbook) throws IOException {
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 获取页签绘画组件
// 定义位置坐标对象HSSFClientAnchor,param1:向右偏移,param2:向下偏移,param3:向左偏移,param4:向上偏移,param5:起始列,param6:起始行,param7:终点列,param8:终点行
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 7, (short) 7, (short) 20, (short) 20);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); File picture = new File("f:/etom/pmms/pmms/src/test/resources/user.png"); ImageIO.write(ImageIO.read(picture), "png", baos); // 通过图片流处理工具ImageIO将图片读取后保存在byte流中
patriarch.createPicture(anchor, workbook.addPicture(baos.toByteArray(), 6)); // 绘画组件执行插入图片,param1:坐标对象,param2:图片信息,param3:图片格式 6代表png格式 File target = new File("f:/etom/pmms/pmms/src/test/resources/userPicture.xls"); workbook.write(new FileOutputStream(target)); }
以上是关于POI excel导出的主要内容,如果未能解决你的问题,请参考以下文章