POI之Excel操作

时间:2020-05-22
本文章向大家介绍POI之Excel操作,主要包括POI之Excel操作使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、Workbook 相关操作

1.1 创建 Workbook 对象

//  操作 Excel2003之前(包括2003)的版本,扩展名是.xls,单个 Sheet 导出的行数最多为65535行
Workbook workbook = new HSSFWorkbook();
//  操作 Excel2007后(包括2007)的版本,扩展名是.xlsx;
Workbook workbook = new XSSFWorkbook();

二、Sheet 相关操作

2.1 创建 Sheet 对象

// 创建默认名称的 sheet
Sheet sheet = workbook.createSheet();
// 创建指定名称的 sheet
Sheet sheet = workbook.createSheet("计划输出表");

2.2 设置页边距

// 页边距(上) = 0
sheet.setMargin(Sheet.TopMargin, (double) (0));
// 页边距(下) = 0
sheet.setMargin(Sheet.BottomMargin, (double) (0));
// 页边距(左) = 0
sheet.setMargin(Sheet.LeftMargin, (double) (0));
// 页边距(右) = 0
sheet.setMargin(Sheet.RightMargin, (double) 0);
// 页眉边距 = 0.3
sheet.setMargin(Sheet.HeaderMargin, (double) (0.12));
// 页脚边距 = 0.3
sheet.setMargin(Sheet.FooterMargin, (double) (0.12));
// 设置水平对齐方式
sheet.setHorizontallyCenter(true);
// 设置垂直对齐方式
sheet.setVerticallyCenter(true); 

三、打印相关操作

3.1 创建 PrintSetup 对象

 PrintSetup printSetup =  sheet.getPrintSetup();

3.2 打印相关设置

// 设置纸张大小为A4
printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
// 设置打印质量为300点
printSetup.setVResolution((short) 300);
// 设置缩放比例
printSetup.setScale((short) 85);
// 设置打印方向,true:横向,false:纵向
printSetup.setLandscape(true);
// 设置页宽
printSetup.setFitWidth((short) 1);
// 设置页高
printSetup.setFitHeight((short) 0);
// 设置打印起始页码
printSetup.setPageStrart((short) 0);
// 打印网格线
printSetup.setPrintGridlines(false); 
// 单色打印
printSetup.setNoColor(false);
// 按草稿方式打印
printSetup.setDraft(false);
// 打印顺序
printSetup.setLeftToRight(false);

3.3 页眉和页脚

// 获取页眉对象
Header header = sheet.getHeader();
// 设置页眉显示位置
header.setLeft("显示内容");
header.setRight("显示内容");
header.setCenter("显示内容");
// 获取页脚对象
Footer footer=sheet.getFooter();
// 设置页脚显示位置
footer.setLeft("显示内容");
footer.setRight("显示内容");
footer.setCenter("显示内容");

3.4 打印区域

workbook.setPrintArea((int) sheetIndex,  (int) startColumn,  (int) endColumn,  (int) startRow,  (int) endRow);

参数说明:

* sheetIndex–从0开始的sheet的索引编号  
* startColumn-打印区域的开始列号  
* endColumn- 打印区域的结束列号  
* startRow-打印区域的开始行号  
* endRow- 打印区域的结束行号

3.5 打印标题

HSSFWorkbook#setRepeatingRowsAndColumns((int) sheetIndex,  (int) startColumn,  (int) endColumn,  (int) startRow,  (int) endRow); 

参数说明:

* sheetIndex–从0开始的sheet的索引编号  
* startColumn-打印区域的开始列号  
* endColumn- 打印区域的结束列号  
* startRow-打印区域的开始行号  
* endRow- 打印区域的结束行号

实例:

//设置左端标题列
workbook.setRepeatingRowsAndColumns(0,0,1,-1,-1); 
//设置顶端标题行
workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4); 
// 同时设置左端和顶端标题
workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1);

四、Row和Cell相关操作

4.1 Row 对象

// 创建指定行的对象,从0开始
Row row = sheet.createRow(int rownum);
// 设置行高
row.setHeight(short height);

4.2 Cell 对象

// 创建列对象,从0开始
Cell cell = row.createCell(int column);
// 移除列对象
row.removeCell(Cell cell);
// 设置列的内容
cell.setCellValue("内容");
// 设置列的样式
cell.setCellStyle(CellStyle style);

4.3 合并单元格

// 创建合并单元格对象
CellRangeAddress cellRangeAddress = new CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol);
// 增加到 sheet 中
sheet.addMergedRegion(cellRangeAddress);
// 给合并单元格设置边框
RegionUtil.setBorderTop(BorderStyle.THIN, orderingCompanyCellRangeAddress, sheet);
RegionUtil.setBorderBottom(BorderStyle.THIN, orderingCompanyCellRangeAddress, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, orderingCompanyCellRangeAddress, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, orderingCompanyCellRangeAddress, sheet);

五、样式相关操作

5.1 字体

// 创建字体对象
Font font = workbook.createFont();
// 设置字体名称 
font.setFontName("宋体");
// 设置字体大小
font.setFontHeightInPoints((short) fontSize);
// 设置是否加粗
font.setBold(true);
// 设置字体高度
font.setFontHeightInPoints(short height);

5.2 单元格样式

// 创建单元格样式对象
CellStyle cellStyle = workbook.createCellStyle();
// 设置样式字体
cellStyle.setFont(font);
// 设置内容水平对齐方式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 设置内容垂直对齐方式
cellStyle.setVerticalAlignment(HorizontalAlignment.CENTER);
// 设置自动换行
cellStyle.setWrapText(true);
// 设置单元格内容垂直对其方式为居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); 
// 设置单元格顶部边框的边框类型
cellStyle.setBorderTop(BorderStyle.THIN);
// 设置单元格左边边框的边框类型
cellStyle.setBorderLeft(BorderStyle.THIN);
// 设置单元格右边边框的边框类型
cellStyle.setBorderRight(BorderStyle.THIN);
// 设置单元格底部边框的边框类型
cellStyle.setBorderBottom(BorderStyle.THIN);

5.3 富文本样式(一个单元格多个样式)

// 创建富文本样式对象
HSSFRichTextString hssfRichTextString = new HSSFRichTextString(wholeStr);
// 设置指定长度字符串的字体
hssfRichTextString.applyFont(int startIndex, int endIndex, Font font);
// 赋值给单元格对象
cell.setCellValue(hssfRichTextString);

六、导出

6.1 浏览器导出

HttpServletResponse response = getHttpServletResponse();
// 设置响应头相关属性
response.setContentType("application/vnd.ms-excel");
response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
// 导出文件格式为.xls
response.setHeader("Content-disposition", "attachment; filename=" + new String(URLEncoder.encode(excelName, "UTF-8").getBytes("UTF-8"),"ISO8859-1") + ".xls");
// 导出文件格式为.xlsx
response.setHeader("Content-disposition", "attachment; filename=" + new String(URLEncoder.encode(excelName, "UTF-8").getBytes("UTF-8"),"ISO8859-1") + ".xlsx");
// 获取输出流对象
OutputStream outputStream = response.getOutputStream();
// 开始输出
workbook.write(outputStream);
outputStream.flush();
// 关闭流出流
outputStream.close();

原文地址:https://www.cnblogs.com/markLogZhu/p/12935111.html