在Java中使用Apache POI导入导出Excel(六)

本文将继续介绍POI的使用,上接在Java中使用Apache POI导入导出Excel(五)

使用Apache POI组件操作Excel(六)

43、隐藏和取消隐藏行

使用 Excel,可以通过选择该行(或行)来隐藏工作表上的行, 右键单击鼠标右键,然后从出现的弹出菜单中选择 'Hide'。

要使用 POI 进行模拟,只需在 XSSFRow 或 HSSFRow(该方法在两个类都实现的 ss.usermodel.Row 接口上定义),如下所示:

java 复制代码
Workbook workbook = new XSSFWorkbook(); 

Sheet sheet = workbook.createSheet(0);

Row row = workbook.createRow(0);

row.setZeroHeight();

如果现在将文件保存到光盘中,则第一个工作表上的第一行将不可见。

使用 Excel,可以通过选择上面的行和下面的行来取消隐藏以前隐藏的行 隐藏的那个,然后按住 Ctrl 键、Shift 和按 数字 9 然后再发布它们。

要使用 POI 模拟此行为,请执行以下操作:

java 复制代码
Workbook workbook = WorkbookFactory.create(new File(.......));
Sheet = workbook.getSheetAt(0);

Iterator<Row> row Iter = sheet.iterator();

while(rowIter.hasNext()) {
  Row row = rowIter.next();

  if(row.getZeroHeight()) {
    row.setZeroHeight(false);
  }
}

如果现在将文件保存到光盘中,则工作簿的第一个工作表上以前隐藏的任何行现在都将可见。

该示例说明了两个功能。首先,只需调用 setZeroHeight() 即可取消隐藏一行 方法并传递布尔值 'false'。其次,它说明了如何测试行是否被隐藏。 只需调用 getZeroHeight() 方法,如果该行被隐藏,它将返回 'true',否则返回 'false'。

44、设置单元格属性

有时,创建具有基本样式的电子表格,然后将特殊样式应用于某些单元格会更容易或更高效 例如,在单元格区域周围绘制边框或设置区域的填充。CellUtil.setCellProperties 允许您在不创建 电子表格中一堆不必要的中间样式。

属性将创建为 Map,并按以下方式应用于单元格。

java 复制代码
Workbook workbook = new XSSFWorkbook(); 

Sheet sheet = workbook.createSheet("Sheet1");

Map<String, Object> properties = new HashMap<String, Object>();

// border around a cell
properties.put(CellUtil.BORDER_TOP, BorderStyle.MEDIUM);
properties.put(CellUtil.BORDER_BOTTOM, BorderStyle.MEDIUM);
properties.put(CellUtil.BORDER_LEFT, BorderStyle.MEDIUM);
properties.put(CellUtil.BORDER_RIGHT, BorderStyle.MEDIUM);

// Give it a color (RED)
properties.put(CellUtil.TOP_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.LEFT_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex());

// Apply the borders to the cell at B2
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
CellUtil.setCellStyleProperties(cell, properties);

// Apply the borders to a 3x3 region starting at D4
for (int ix=3; ix <= 5; ix++) {
  row = sheet.createRow(ix);

  for (int iy = 3; iy <= 5; iy++) {
    cell = row.createCell(iy);
    CellUtil.setCellStyleProperties(cell, properties);
  }
}

注意:这不会替换单元格的属性,它会将您放入 Map 的属性与 Cell 的现有样式属性。如果属性已存在,则将其替换为新属性。如果属性没有 存在,则添加它。此方法不会删除 CellStyle 属性。

45、绘图边框

在 Excel 中,只需按一下按钮,即可在整个工作簿区域上应用一组边框。The PropertyTemplate object 使用定义的方法和常量来模拟此操作,以允许绘制 top、bottom、left、right、horizontal、 垂直、内部、外部或单元格范围周围的所有边框。其他方法允许应用颜色 到边境。

它的工作原理是这样的:你创建一个 PropertyTemplate 对象,它是你希望应用于 表。然后,向 PropertyTemplate 添加边框和颜色,最后将其应用于所需的任何工作表 那组边界。您可以创建多个 PropertyTemplate 对象并将它们应用于单个图纸,也可以 将同一 PropertyTemplate 对象应用于多个工作表。它就像一个预先打印的表格。

枚举:

边框样式

定义边框的外观,是粗的还是细的、实线还是虚线、单边还是双边。 此枚举替换已弃用的 CellStyle.BORDER_XXXXX 常量。PropertyTemplate 不会 支持旧样式的 BORDER_XXXXX 常量。特殊值 BorderStyle.NONE 将从 一个 Cell 。

边界范围

描述 BorderStyle 将应用于的区域部分。例如,TOP、BOTTOM、INSIDE 或 OUTSIDE。 特殊值 BorderExtent.NONE 将从 PropertyTemplate 中删除边框。应用模板后, 不会对 PropertyTemplate 中不存在 border 属性的单元格边框进行任何更改。

java 复制代码
// draw borders (three 3x3 grids)
PropertyTemplate pt = new PropertyTemplate();

// #1) these borders will all be medium in default color
pt.drawBorders(new CellRangeAddress(1, 3, 1, 3),
        BorderStyle.MEDIUM, BorderExtent.ALL);

// #2) these cells will have medium outside borders and thin inside borders
pt.drawBorders(new CellRangeAddress(5, 7, 1, 3),
        BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.THIN,
        BorderExtent.INSIDE);

// #3) these cells will all be medium weight with different colors for the
//     outside, inside horizontal, and inside vertical borders. The center
//     cell will have no borders.
pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
        BorderStyle.MEDIUM, IndexedColors.RED.getIndex(),
        BorderExtent.OUTSIDE);
pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
        BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(),
        BorderExtent.INSIDE_VERTICAL);
pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),
        BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(),
        BorderExtent.INSIDE_HORIZONTAL);
pt.drawBorders(new CellRangeAddress(10, 10, 2, 2),
        BorderStyle.NONE,
        BorderExtent.ALL);

// apply borders to sheet
Workbook wb = new XSSFWorkbook();

Sheet sh = wb.createSheet("Sheet1");

pt.applyBorders(sh);

注意:最后一个 pt.drawBorders() 调用使用 BorderStyle.NONE 从范围中删除边框。喜欢 setCellStyleProperties 时,applyBorders 方法会合并单元格样式的属性,因此现有边框 仅当它们被其他内容替换时才会被更改,或者仅当它们被替换为 要从边框中删除颜色,请使用 IndexedColor.AUTOMATIC.getIndex()。

此外,若要从 PropertyTemplate 对象中删除边框或颜色,请使用 BorderExtent.NONE。

这还不适用于对角线边界。

46、创建数据透视表

数据透视表是电子表格文件的一项强大功能。您可以使用以下代码创建数据透视表。

java 复制代码
XSSFWorkbook wb = new XSSFWorkbook();

XSSFSheet sheet = wb.createSheet();

//Create some data to build the pivot table on
setCellData(sheet);

XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:D4"), new CellReference("H5"));

//Configure the pivot table
//Use first column as row label
pivotTable.addRowLabel(0);

//Sum up the second column
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);

//Set the third column as filter
pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2);

//Add filter on forth column
pivotTable.addReportFilter(3);

47、具有多种样式的单元格(富文本字符串)

应用一组文本格式(颜色、样式、字体等) 添加到单元格中,您应该为工作簿创建一个 CellStyle,然后应用于单元格。

java 复制代码
// XSSF Example
XSSFCell cell = row.createCell(1);

XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");

XSSFFont font1 = wb.createFont();
font1.setBold(true);
font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));

rt.applyFont(0, 10, font1);

XSSFFont font2 = wb.createFont();
font2.setItalic(true);
font2.setUnderline(XSSFFont.U_DOUBLE);
font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));

rt.applyFont(10, 19, font2);

XSSFFont font3 = wb.createFont();

font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));

rt.append(" Jumped over the lazy dog", font3);

cell.setCellValue(rt);

要将不同的格式应用于单元格的不同部分,您需要 需要使用 RichTextString, 这允许在单元格内设置文本部分的样式。

相关推荐
CircleMouse6 小时前
如何设置wps单元格下拉选项设置
excel·wps
zhangjin122211 小时前
kettle插件-excel插件,kettle读取excel动态表头,kettle根据列名读取excel
excel·kettle·kettle excel插件·kettle 动态excel
远洪1 天前
excel 找出两列不同的数据
excel
pcplayer1 天前
非常好用的 Excel 读写控件
excel·delphi·office
Navicat中国1 天前
使用 Navicat 导入向导导入 Excel 数据时,系统提示导入成功,表中也能看到数据,但行数统计显示为 0,这是什么原因?
数据库·excel·导入
穿着内裤的外星人1 天前
触控精灵远程读写Excel步骤配置
excel
是孑然呀2 天前
【小记】excel vlookup一对多(第二篇)
excel
开开心心就好2 天前
专为视障人士设计的免费辅助工具
windows·计算机视觉·计算机外设·excel·散列表·推荐算法·csdn开发云
transformer_WSZ2 天前
excel两列数据绘制折线图
excel·折线图
蒋胜山2 天前
Excel 练习题(5)
经验分享·excel