From b442d2a74afa604bfce862746eeed7a95acd694f Mon Sep 17 00:00:00 2001 From: Sergey Vladimirov Date: Wed, 31 Aug 2011 11:36:07 +0000 Subject: [PATCH] fixed width for tables in FO git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1163583 13f79535-47bb-0310-9956-ffa450edef68 --- .../converter/AbstractExcelConverter.java | 6 ++ .../hssf/converter/ExcelToFoConverter.java | 78 ++++++++++--------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java index 16a5ce5f35..fc1302f0aa 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/AbstractExcelConverter.java @@ -45,6 +45,12 @@ public abstract class AbstractExcelConverter .getColumnWidth( columnIndex ) ); } + protected static int getDefaultColumnWidth( HSSFSheet sheet ) + { + return ExcelToHtmlUtils.getColumnWidthInPx( sheet + .getDefaultColumnWidth() ); + } + protected final HSSFDataFormatter _formatter = new HSSFDataFormatter(); private FontReplacer fontReplacer = new DefaultFontReplacer(); diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java index 06b3486924..3cf07c56aa 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToFoConverter.java @@ -134,15 +134,12 @@ public class ExcelToFoConverter extends AbstractExcelConverter this.foDocumentFacade = new FoDocumentFacade( document ); } - protected String createPageMaster( HSSFSheet sheet, int maxSheetColumns, - String pageMasterName ) + protected String createPageMaster( float tableWidthIn, String pageMasterName ) { final float paperHeightIn; final float paperWidthIn; { - float requiredWidthIn = ExcelToFoUtils - .getColumnWidthInPx( getSheetWidth( sheet, maxSheetColumns ) ) - / DPI + 2; + float requiredWidthIn = tableWidthIn + 2; if ( requiredWidthIn < PAPER_A4_WIDTH_INCHES ) { @@ -182,23 +179,6 @@ public class ExcelToFoConverter extends AbstractExcelConverter return foDocumentFacade.getDocument(); } - protected int getSheetWidth( HSSFSheet sheet, int maxSheetColumns ) - { - int width = 0; - if ( isOutputRowNumbers() ) - { - width += sheet.getDefaultColumnWidth(); - } - - for ( int columnIndex = 0; columnIndex < maxSheetColumns; columnIndex++ ) - { - if ( !isOutputHiddenColumns() && sheet.isColumnHidden( columnIndex ) ) - continue; - width += sheet.getColumnWidth( columnIndex ); - } - return width; - } - protected boolean processCell( HSSFWorkbook workbook, HSSFCell cell, Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt ) @@ -388,8 +368,6 @@ public class ExcelToFoConverter extends AbstractExcelConverter StringBuilder borderStyle = new StringBuilder(); borderStyle.append( ExcelToHtmlUtils.getBorderWidth( xlsBorder ) ); - borderStyle.append( ' ' ); - borderStyle.append( ExcelToHtmlUtils.getBorderStyle( xlsBorder ) ); final HSSFColor color = workbook.getCustomPalette().getColor( borderColor ); @@ -397,6 +375,9 @@ public class ExcelToFoConverter extends AbstractExcelConverter { borderStyle.append( ' ' ); borderStyle.append( ExcelToHtmlUtils.getColor( color ) ); + + borderStyle.append( ' ' ); + borderStyle.append( ExcelToHtmlUtils.getBorderStyle( xlsBorder ) ); } cellTarget.setAttribute( "border-" + type, borderStyle.toString() ); @@ -476,13 +457,24 @@ public class ExcelToFoConverter extends AbstractExcelConverter /** * Creates COLGROUP element with width specified for all columns. (Except * first if {@link #isOutputRowNumbers()}==true) + * + * @return table width in inches */ - protected void processColumnWidths( HSSFSheet sheet, int maxSheetColumns, + protected float processColumnWidths( HSSFSheet sheet, int maxSheetColumns, Element table ) { + float tableWidth = 0; + if ( isOutputRowNumbers() ) { - table.appendChild( foDocumentFacade.createTableColumn() ); + final float columnWidthIn = getDefaultColumnWidth( sheet ) / DPI; + + final Element rowNumberColumn = foDocumentFacade + .createTableColumn(); + rowNumberColumn.setAttribute( "column-width", columnWidthIn + "in" ); + table.appendChild( rowNumberColumn ); + + tableWidth += columnWidthIn; } for ( int c = 0; c < maxSheetColumns; c++ ) @@ -490,11 +482,17 @@ public class ExcelToFoConverter extends AbstractExcelConverter if ( !isOutputHiddenColumns() && sheet.isColumnHidden( c ) ) continue; + final float columnWidthIn = getColumnWidth( sheet, c ) / DPI; + Element col = foDocumentFacade.createTableColumn(); - col.setAttribute( "column-width", - String.valueOf( getColumnWidth( sheet, c ) / DPI ) + "in" ); + col.setAttribute( "column-width", columnWidthIn + "in" ); table.appendChild( col ); + + tableWidth += columnWidthIn; } + + table.setAttribute( "width", tableWidth + "in" ); + return tableWidth; } protected void processDocumentInformation( @@ -524,9 +522,6 @@ public class ExcelToFoConverter extends AbstractExcelConverter final short maxColIx = row.getLastCellNum(); if ( maxColIx <= 0 ) { - Element emptyCellElement = foDocumentFacade.createTableCell(); - emptyCellElement.appendChild( foDocumentFacade.createBlock() ); - tableRowElement.appendChild( emptyCellElement ); return 0; } @@ -644,7 +639,7 @@ public class ExcelToFoConverter extends AbstractExcelConverter return tableRowNumberCellElement; } - protected int processSheet( HSSFWorkbook workbook, HSSFSheet sheet, + protected float processSheet( HSSFWorkbook workbook, HSSFSheet sheet, Element flow ) { final int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); @@ -654,6 +649,8 @@ public class ExcelToFoConverter extends AbstractExcelConverter processSheetName( sheet, flow ); Element table = foDocumentFacade.createTable(); + table.setAttribute( "table-layout", "fixed" ); + Element tableBody = foDocumentFacade.createTableBody(); final CellRangeAddress[][] mergedRanges = ExcelToHtmlUtils @@ -679,6 +676,13 @@ public class ExcelToFoConverter extends AbstractExcelConverter int maxRowColumnNumber = processRow( workbook, mergedRanges, row, tableRowElement ); + if ( tableRowElement.getChildNodes().getLength() == 0 ) + { + Element emptyCellElement = foDocumentFacade.createTableCell(); + emptyCellElement.appendChild( foDocumentFacade.createBlock() ); + tableRowElement.appendChild( emptyCellElement ); + } + if ( maxRowColumnNumber == 0 ) { emptyRowElements.add( tableRowElement ); @@ -699,7 +703,7 @@ public class ExcelToFoConverter extends AbstractExcelConverter maxSheetColumns = Math.max( maxSheetColumns, maxRowColumnNumber ); } - processColumnWidths( sheet, maxSheetColumns, table ); + float tableWidthIn = processColumnWidths( sheet, maxSheetColumns, table ); if ( isOutputColumnHeaders() ) { @@ -709,7 +713,7 @@ public class ExcelToFoConverter extends AbstractExcelConverter table.appendChild( tableBody ); flow.appendChild( table ); - return maxSheetColumns; + return tableWidthIn; } protected void processSheetName( HSSFSheet sheet, Element flow ) @@ -756,11 +760,11 @@ public class ExcelToFoConverter extends AbstractExcelConverter pageSequence, "xsl-region-body" ); HSSFSheet sheet = workbook.getSheetAt( s ); - int maxSheetColumns = processSheet( workbook, sheet, flow ); + float tableWidthIn = processSheet( workbook, sheet, flow ); - if ( maxSheetColumns != 0 ) + if ( tableWidthIn != 0 ) { - createPageMaster( sheet, maxSheetColumns, pageMasterName ); + createPageMaster( tableWidthIn, pageMasterName ); foDocumentFacade.addPageSequence( pageSequence ); } } -- 2.39.5