simplify internal API

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148095 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-07-18 23:21:46 +00:00
parent 6e5b818c94
commit 637d4f232a
2 changed files with 89 additions and 67 deletions

View File

@ -19,7 +19,6 @@ package org.apache.poi.hssf.converter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -384,9 +383,8 @@ public class ExcelToHtmlConverter
return useDivsToSpan; return useDivsToSpan;
} }
protected boolean processCell( HSSFWorkbook workbook, HSSFCell cell, protected boolean processCell( HSSFCell cell, Element tableCellElement,
Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt )
float normalHeightPt )
{ {
final HSSFCellStyle cellStyle = cell.getCellStyle(); final HSSFCellStyle cellStyle = cell.getCellStyle();
@ -464,6 +462,7 @@ public class ExcelToHtmlConverter
final short cellStyleIndex = cellStyle.getIndex(); final short cellStyleIndex = cellStyle.getIndex();
if ( cellStyleIndex != 0 ) if ( cellStyleIndex != 0 )
{ {
HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
String mainCssClass = getStyleClassName( workbook, cellStyle ); String mainCssClass = getStyleClassName( workbook, cellStyle );
if ( wrapInDivs ) if ( wrapInDivs )
{ {
@ -616,10 +615,10 @@ public class ExcelToHtmlConverter
/** /**
* @return maximum 1-base index of column that were rendered, zero if none * @return maximum 1-base index of column that were rendered, zero if none
*/ */
protected int processRow( HSSFWorkbook workbook, HSSFSheet sheet, protected int processRow( CellRangeAddress[][] mergedRanges, HSSFRow row,
CellRangeAddress[][] mergedRanges, HSSFRow row,
Element tableRowElement ) Element tableRowElement )
{ {
final HSSFSheet sheet = row.getSheet();
final short maxColIx = row.getLastCellNum(); final short maxColIx = row.getLastCellNum();
if ( maxColIx <= 0 ) if ( maxColIx <= 0 )
return 0; return 0;
@ -640,7 +639,7 @@ public class ExcelToHtmlConverter
if ( !isOutputHiddenColumns() && sheet.isColumnHidden( colIx ) ) if ( !isOutputHiddenColumns() && sheet.isColumnHidden( colIx ) )
continue; continue;
CellRangeAddress range = ExcelToHtmlUtils.getCellRangeAddress( CellRangeAddress range = ExcelToHtmlUtils.getMergedRange(
mergedRanges, row.getRowNum(), colIx ); mergedRanges, row.getRowNum(), colIx );
if ( range != null if ( range != null
@ -695,7 +694,7 @@ public class ExcelToHtmlConverter
boolean emptyCell; boolean emptyCell;
if ( cell != null ) if ( cell != null )
{ {
emptyCell = processCell( workbook, cell, tableCellElement, emptyCell = processCell( cell, tableCellElement,
getColumnWidth( sheet, colIx ), divWidthPx, getColumnWidth( sheet, colIx ), divWidthPx,
row.getHeight() / 20f ); row.getHeight() / 20f );
} }
@ -732,7 +731,7 @@ public class ExcelToHtmlConverter
tableRowNumberCellElement.appendChild( text ); tableRowNumberCellElement.appendChild( text );
} }
protected void processSheet( HSSFWorkbook workbook, HSSFSheet sheet ) protected void processSheet( HSSFSheet sheet )
{ {
processSheetHeader( htmlDocumentFacade.getBody(), sheet ); processSheetHeader( htmlDocumentFacade.getBody(), sheet );
@ -745,49 +744,8 @@ public class ExcelToHtmlConverter
Element tableBody = htmlDocumentFacade.createTableBody(); Element tableBody = htmlDocumentFacade.createTableBody();
CellRangeAddress[][] mergedRanges = new CellRangeAddress[1][]; final CellRangeAddress[][] mergedRanges = ExcelToHtmlUtils
for ( int m = 0; m < sheet.getNumMergedRegions(); m++ ) .buildMergedRangesMap( sheet );
{
final CellRangeAddress cellRangeAddress = sheet.getMergedRegion( m );
final int requiredHeight = cellRangeAddress.getLastRow() + 1;
if ( mergedRanges.length < requiredHeight )
{
CellRangeAddress[][] newArray = new CellRangeAddress[requiredHeight][];
System.arraycopy( mergedRanges, 0, newArray, 0,
mergedRanges.length );
mergedRanges = newArray;
}
for ( int r = cellRangeAddress.getFirstRow(); r <= cellRangeAddress
.getLastRow(); r++ )
{
final int requiredWidth = cellRangeAddress.getLastColumn() + 1;
CellRangeAddress[] rowMerged = mergedRanges[r];
if ( rowMerged == null )
{
rowMerged = new CellRangeAddress[requiredWidth];
mergedRanges[r] = rowMerged;
}
else
{
final int rowMergedLength = rowMerged.length;
if ( rowMergedLength < requiredWidth )
{
final CellRangeAddress[] newRow = new CellRangeAddress[requiredWidth];
System.arraycopy( rowMerged, 0, newRow, 0,
rowMergedLength );
mergedRanges[r] = newRow;
rowMerged = newRow;
}
}
Arrays.fill( rowMerged, cellRangeAddress.getFirstColumn(),
cellRangeAddress.getLastColumn() + 1, cellRangeAddress );
}
}
final List<Element> emptyRowElements = new ArrayList<Element>( final List<Element> emptyRowElements = new ArrayList<Element>(
physicalNumberOfRows ); physicalNumberOfRows );
@ -809,8 +767,8 @@ public class ExcelToHtmlConverter
tableRowElement.getTagName(), "r", "height:" tableRowElement.getTagName(), "r", "height:"
+ ( row.getHeight() / 20f ) + "pt;" ) ); + ( row.getHeight() / 20f ) + "pt;" ) );
int maxRowColumnNumber = processRow( workbook, sheet, mergedRanges, int maxRowColumnNumber = processRow( mergedRanges, row,
row, tableRowElement ); tableRowElement );
if ( maxRowColumnNumber == 0 ) if ( maxRowColumnNumber == 0 )
{ {
@ -873,7 +831,7 @@ public class ExcelToHtmlConverter
for ( int s = 0; s < workbook.getNumberOfSheets(); s++ ) for ( int s = 0; s < workbook.getNumberOfSheets(); s++ )
{ {
HSSFSheet sheet = workbook.getSheetAt( s ); HSSFSheet sheet = workbook.getSheetAt( s );
processSheet( workbook, sheet ); processSheet( sheet );
} }
htmlDocumentFacade.updateStylesheet(); htmlDocumentFacade.updateStylesheet();

View File

@ -19,8 +19,10 @@ package org.apache.poi.hssf.converter;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
@ -60,6 +62,61 @@ public class ExcelToHtmlUtils
} }
} }
/**
* Creates a map (i.e. two-dimensional array) filled with ranges. Allow fast
* retrieving {@link CellRangeAddress} of any cell, if cell is contained in
* range.
*
* @see #getMergedRange(CellRangeAddress[][], int, int)
*/
public static CellRangeAddress[][] buildMergedRangesMap( HSSFSheet sheet )
{
CellRangeAddress[][] mergedRanges = new CellRangeAddress[1][];
for ( int m = 0; m < sheet.getNumMergedRegions(); m++ )
{
final CellRangeAddress cellRangeAddress = sheet.getMergedRegion( m );
final int requiredHeight = cellRangeAddress.getLastRow() + 1;
if ( mergedRanges.length < requiredHeight )
{
CellRangeAddress[][] newArray = new CellRangeAddress[requiredHeight][];
System.arraycopy( mergedRanges, 0, newArray, 0,
mergedRanges.length );
mergedRanges = newArray;
}
for ( int r = cellRangeAddress.getFirstRow(); r <= cellRangeAddress
.getLastRow(); r++ )
{
final int requiredWidth = cellRangeAddress.getLastColumn() + 1;
CellRangeAddress[] rowMerged = mergedRanges[r];
if ( rowMerged == null )
{
rowMerged = new CellRangeAddress[requiredWidth];
mergedRanges[r] = rowMerged;
}
else
{
final int rowMergedLength = rowMerged.length;
if ( rowMergedLength < requiredWidth )
{
final CellRangeAddress[] newRow = new CellRangeAddress[requiredWidth];
System.arraycopy( rowMerged, 0, newRow, 0,
rowMergedLength );
mergedRanges[r] = newRow;
rowMerged = newRow;
}
}
Arrays.fill( rowMerged, cellRangeAddress.getFirstColumn(),
cellRangeAddress.getLastColumn() + 1, cellRangeAddress );
}
}
return mergedRanges;
}
public static String getBorderStyle( short xlsBorder ) public static String getBorderStyle( short xlsBorder )
{ {
final String borderStyle; final String borderStyle;
@ -111,18 +168,6 @@ public class ExcelToHtmlUtils
return borderWidth; return borderWidth;
} }
public static CellRangeAddress getCellRangeAddress(
CellRangeAddress[][] mergedRanges, int rowNumber, int columnNumber )
{
CellRangeAddress[] mergedRangeRowInfo = rowNumber < mergedRanges.length ? mergedRanges[rowNumber]
: null;
CellRangeAddress cellRangeAddress = mergedRangeRowInfo != null
&& columnNumber < mergedRangeRowInfo.length ? mergedRangeRowInfo[columnNumber]
: null;
return cellRangeAddress;
}
public static String getColor( HSSFColor color ) public static String getColor( HSSFColor color )
{ {
StringBuilder stringBuilder = new StringBuilder( 7 ); StringBuilder stringBuilder = new StringBuilder( 7 );
@ -168,6 +213,25 @@ public class ExcelToHtmlUtils
return pixels; return pixels;
} }
/**
* @param mergedRanges
* map of sheet merged ranges built with
* {@link #buildMergedRangesMap(HSSFSheet)}
* @return {@link CellRangeAddress} from map if cell with specified row and
* column numbers contained in found range, <tt>null</tt> otherwise
*/
public static CellRangeAddress getMergedRange(
CellRangeAddress[][] mergedRanges, int rowNumber, int columnNumber )
{
CellRangeAddress[] mergedRangeRowInfo = rowNumber < mergedRanges.length ? mergedRanges[rowNumber]
: null;
CellRangeAddress cellRangeAddress = mergedRangeRowInfo != null
&& columnNumber < mergedRangeRowInfo.length ? mergedRangeRowInfo[columnNumber]
: null;
return cellRangeAddress;
}
static boolean isEmpty( String str ) static boolean isEmpty( String str )
{ {
return str == null || str.length() == 0; return str == null || str.length() == 0;