import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
return useDivsToSpan;
}
- protected boolean processCell( HSSFWorkbook workbook, HSSFCell cell,
- Element tableCellElement, int normalWidthPx, int maxSpannedWidthPx,
- float normalHeightPt )
+ protected boolean processCell( HSSFCell cell, Element tableCellElement,
+ int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt )
{
final HSSFCellStyle cellStyle = cell.getCellStyle();
final short cellStyleIndex = cellStyle.getIndex();
if ( cellStyleIndex != 0 )
{
+ HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
String mainCssClass = getStyleClassName( workbook, cellStyle );
if ( wrapInDivs )
{
/**
* @return maximum 1-base index of column that were rendered, zero if none
*/
- protected int processRow( HSSFWorkbook workbook, HSSFSheet sheet,
- CellRangeAddress[][] mergedRanges, HSSFRow row,
+ protected int processRow( CellRangeAddress[][] mergedRanges, HSSFRow row,
Element tableRowElement )
{
+ final HSSFSheet sheet = row.getSheet();
final short maxColIx = row.getLastCellNum();
if ( maxColIx <= 0 )
return 0;
if ( !isOutputHiddenColumns() && sheet.isColumnHidden( colIx ) )
continue;
- CellRangeAddress range = ExcelToHtmlUtils.getCellRangeAddress(
+ CellRangeAddress range = ExcelToHtmlUtils.getMergedRange(
mergedRanges, row.getRowNum(), colIx );
if ( range != null
boolean emptyCell;
if ( cell != null )
{
- emptyCell = processCell( workbook, cell, tableCellElement,
+ emptyCell = processCell( cell, tableCellElement,
getColumnWidth( sheet, colIx ), divWidthPx,
row.getHeight() / 20f );
}
tableRowNumberCellElement.appendChild( text );
}
- protected void processSheet( HSSFWorkbook workbook, HSSFSheet sheet )
+ protected void processSheet( HSSFSheet sheet )
{
processSheetHeader( htmlDocumentFacade.getBody(), sheet );
Element tableBody = htmlDocumentFacade.createTableBody();
- 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 );
- }
- }
+ final CellRangeAddress[][] mergedRanges = ExcelToHtmlUtils
+ .buildMergedRangesMap( sheet );
final List<Element> emptyRowElements = new ArrayList<Element>(
physicalNumberOfRows );
tableRowElement.getTagName(), "r", "height:"
+ ( row.getHeight() / 20f ) + "pt;" ) );
- int maxRowColumnNumber = processRow( workbook, sheet, mergedRanges,
- row, tableRowElement );
+ int maxRowColumnNumber = processRow( mergedRanges, row,
+ tableRowElement );
if ( maxRowColumnNumber == 0 )
{
for ( int s = 0; s < workbook.getNumberOfSheets(); s++ )
{
HSSFSheet sheet = workbook.getSheetAt( s );
- processSheet( workbook, sheet );
+ processSheet( sheet );
}
htmlDocumentFacade.updateStylesheet();
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
+import java.util.Arrays;
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.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
}
}
+ /**
+ * 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 )
{
final String borderStyle;
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 )
{
StringBuilder stringBuilder = new StringBuilder( 7 );
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 )
{
return str == null || str.length() == 0;