前端网站页面模板,wordpress后台html,网站如何推广方案策划,怎么给企业制作网站本文将继续介绍POI的使用#xff0c;上接在Java中使用Apache POI导入导出Excel#xff08;五#xff09;
使用Apache POI组件操作Excel#xff08;六#xff09;
43、隐藏和取消隐藏行
使用 Excel#xff0c;可以通过选择该行#xff08;或行#xff09;来隐藏工作表…本文将继续介绍POI的使用上接在Java中使用Apache POI导入导出Excel五
使用Apache POI组件操作Excel六
43、隐藏和取消隐藏行
使用 Excel可以通过选择该行或行来隐藏工作表上的行 右键单击鼠标右键然后从出现的弹出菜单中选择 Hide。
要使用 POI 进行模拟只需在 XSSFRow 或 HSSFRow该方法在两个类都实现的 ss.usermodel.Row 接口上定义如下所示
Workbook workbook new XSSFWorkbook(); Sheet sheet workbook.createSheet(0);Row row workbook.createRow(0);row.setZeroHeight();
如果现在将文件保存到光盘中则第一个工作表上的第一行将不可见。
使用 Excel可以通过选择上面的行和下面的行来取消隐藏以前隐藏的行 隐藏的那个然后按住 Ctrl 键、Shift 和按 数字 9 然后再发布它们。
要使用 POI 模拟此行为请执行以下操作
Workbook workbook WorkbookFactory.create(new File(.......));
Sheet workbook.getSheetAt(0);IteratorRow 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并按以下方式应用于单元格。 Workbook workbook new XSSFWorkbook(); Sheet sheet workbook.createSheet(Sheet1);MapString, Object properties new HashMapString, 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 ix3; 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 属性的单元格边框进行任何更改。
// 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、创建数据透视表
数据透视表是电子表格文件的一项强大功能。您可以使用以下代码创建数据透视表。
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然后应用于单元格。
// 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 这允许在单元格内设置文本部分的样式。