简体   繁体   English

有没有办法使用 Java 中的 Apache POI 库水平创建分割平面? [EXCEL + APACHE POI]

[英]Is there a way to create a split plane horizontally using the Apache POI library in Java? [EXCEL + APACHE POI]

using the createSplitPlane() method in XSSFSheet it is only able to do a 4 pane split.使用 XSSFSheet 中的 createSplitPlane() 方法只能进行 4 窗格拆分。 Is there a way to do a 2 plane split instead horizontally?有没有办法做一个 2 平面拆分而不是水平?

When using Sheet.createSplitPane one sets following parameters:使用Sheet.createSplitPane时,设置以下参数:

  • xSplitPos - Horizontal position of split (in 1/20th of a point). xSplitPos - 分割的水平位置(点的 1/20)。
  • ySplitPos - Vertical position of split (in 1/20th of a point). ySplitPos - 分割的垂直位置(点的 1/20)。
  • leftmostColumn - Left column visible in right pane. leftmostColumn - 在右窗格中可见的左列。
  • topRow - Top row visible in bottom pane topRow - 在底部窗格中可见的顶行
  • activePane - Active pane. activePane - 活动窗格。 One of: PANE_LOWER_RIGHT, PANE_UPPER_RIGHT, PANE_LOWER_LEFT, PANE_UPPER_LEFT.以下之一:PANE_LOWER_RIGHT、PANE_UPPER_RIGHT、PANE_LOWER_LEFT、PANE_UPPER_LEFT。

So if you set xSplitPos to 0 and leftmostColumn to 0 , then you should get what you want.因此,如果您将xSplitPos设置为0并将leftmostColumn设置为0 ,那么您应该得到您想要的。

But there is a bug with activePane .但是activePane有一个错误。

The constant fields in Sheet are as follows: Sheet中的常量字段如下:

PANE_LOWER_RIGHT  0
PANE_UPPER_RIGHT  1
PANE_LOWER_LEFT   2
PANE_UPPER_LEFT   3

But the corresponding values in org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane are:但是org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane中的对应值是:

INT_BOTTOM_RIGHT 1
INT_TOP_RIGHT    2
INT_BOTTOM_LEFT  3
INT_TOP_LEFT     4

So the first are 0-based while the second are 1-based.所以第一个是基于 0 的,而第二个是基于 1 的。

That's why one needs using +1 to each of the Sheet constants.这就是为什么需要对每个工作Sheet常量使用+1的原因。

Example:例子:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelSplitPane {
    
 public static void main(String[] args) throws Exception {

  try (
       Workbook workbook = new XSSFWorkbook(); FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {
       //Workbook workbook = new HSSFWorkbook(); FileOutputStream fileout = new FileOutputStream("Excel.xls") ) {

   Sheet sheet = workbook.createSheet();
   
   sheet.createSplitPane(0, 100*20, 0, 9, Sheet.PANE_UPPER_LEFT+1);
   
   workbook.write(fileout);
  }
 }
}

Result:结果:

在此处输入图像描述

And when using并且在使用时

...
sheet.createSplitPane(100*20, 0, 3, 0, Sheet.PANE_UPPER_LEFT+1);
...

the result looks like:结果如下:

在此处输入图像描述

This is tested and works using apache poi 4.1.1 as well as apache poi 5.2.2 .这是使用apache poi 4.1.1apache poi 5.2.2测试和工作的。

There are additional issues in HSSF using HSSFSheet.createSplitPane .使用HSSFSheet.createSplitPaneHSSF中还有其他问题。 But that's another question.但这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM