try {
FileInputStream file=new FileInputStream(pathToFile);
XSSFWorkbook wb=new XSSFWorkbook(file);
XSSFSheet sheet0=wb.getSheetAt(0);
float sizeOfPage=0;
for(int i =0 ; i<end; i++) { //here end should be equal to the last row before
//the automatic page break gets inserted
sizeOfPage=sizeOfPage + sheet0.getRow(i).getHeightInPoints();
}
System.out.println(sizeOfPage); //this is the value you need to get
//calculated
wb.close();
file.close();
}
catch (IOException e) {
}
float length=0; //the length of the whole document
int i =0;
while(i<sheet0MakeQuotationMod.getLastRowNum()-1) { //note that the indexing is
//different in the excel and
//the code, I had trouble
//getting this working....
length=length + sheet0MakeQuotationMod.getRow(i).getHeightInPoints();
i++;
}
float lengthFull=length
int counter=0;
while(length>=sizeOfPage) { //to see how many pages
length/=sizeOfPage;
counter++;
}
double spaceLeft=lengthFull-(sizeOfPage*counter); //to see how much space is left
//on the last page
if(spaceLeft<spaceINeed) { //if this holds, it means that the part I want to be on
//the same page would get separated. I calculated this
//with the same code as above.
//this row break is being applied where I want the segment to be separated
//so that the segment gets shifted to the next page.
sheet0MakeQuotationMod.setRowBreak(indexWhereIWantItToBeSeparated);
}
2条答案
按热度按时间piah890a1#
如果你使用
XSSFSheet
,那么你可以使用sheet.getLastRow()
,如果你使用其他的,那么使用sheet.getPhysicalNumberOfRows()
。所有的Sheet接口的实现都有这个。wqsoz72f2#
我想出了一个解决方案,但可能不是100%傻瓜证明。Excel将显示自己的自动分页符(虚线)时,单击视图-〉分页符视图。我发现在某个地方,Apache POI无法检测到这些分页符时,他们是页面格式相关的,换句话说,它不知道的“页面”的大小,你会想要的。我试图让它来检测,但我没有成功。
不过,我的方法和解决方案如下:
1.我首先混合excel文件(在excel中)以获得自动分页符插入的位置,然后我在java中使用以下代码计算这些行的总高度:
1.然后,我在下面的代码中使用了值sizeOfPage来让代码检测何时需要添加额外的分页符。在这里,我的目标是确定要保持完整的段是否适合,而不插入分页符。我的段位于文档的末尾。
希望这有帮助!如果有什么不清楚的地方,请询问,我会尽力澄清。:)