ability to customize Excel-to-HTML CSS prefixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1189180 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sergey Vladimirov 2011-10-26 13:02:34 +00:00
parent 5cba03af8e
commit ef237c3eb6

View File

@ -125,6 +125,14 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
private String cssClassContainerDiv = null; private String cssClassContainerDiv = null;
private String cssClassPrefixCell = "c";
private String cssClassPrefixDiv = "d";
private String cssClassPrefixRow = "r";
private String cssClassPrefixTable = "t";
private final String cssClassTable; private final String cssClassTable;
private Map<Short, String> excelStyleToClass = new LinkedHashMap<Short, String>(); private Map<Short, String> excelStyleToClass = new LinkedHashMap<Short, String>();
@ -136,7 +144,9 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
public ExcelToHtmlConverter( Document doc ) public ExcelToHtmlConverter( Document doc )
{ {
htmlDocumentFacade = new HtmlDocumentFacade( doc ); htmlDocumentFacade = new HtmlDocumentFacade( doc );
cssClassTable = htmlDocumentFacade.getOrCreateCssClass( "t", "border-collapse:collapse;border-spacing:0;" ); cssClassTable = htmlDocumentFacade.getOrCreateCssClass(
cssClassPrefixTable,
"border-collapse:collapse;border-spacing:0;" );
} }
protected String buildStyle( HSSFWorkbook workbook, HSSFCellStyle cellStyle ) protected String buildStyle( HSSFWorkbook workbook, HSSFCellStyle cellStyle )
@ -233,6 +243,26 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
} }
} }
public String getCssClassPrefixCell()
{
return cssClassPrefixCell;
}
public String getCssClassPrefixDiv()
{
return cssClassPrefixDiv;
}
public String getCssClassPrefixRow()
{
return cssClassPrefixRow;
}
public String getCssClassPrefixTable()
{
return cssClassPrefixTable;
}
public Document getDocument() public Document getDocument()
{ {
return htmlDocumentFacade.getDocument(); return htmlDocumentFacade.getDocument();
@ -248,7 +278,8 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
return knownClass; return knownClass;
String cssStyle = buildStyle( workbook, cellStyle ); String cssStyle = buildStyle( workbook, cellStyle );
String cssClass = htmlDocumentFacade.getOrCreateCssClass( "c", cssStyle ); String cssClass = htmlDocumentFacade.getOrCreateCssClass(
cssClassPrefixCell, cssStyle );
excelStyleToClass.put( cellStyleKey, cssClass ); excelStyleToClass.put( cellStyleKey, cssClass );
return cssClass; return cssClass;
} }
@ -399,7 +430,7 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
innerDivStyle.append( "pt;white-space:nowrap;" ); innerDivStyle.append( "pt;white-space:nowrap;" );
ExcelToHtmlUtils.appendAlign( innerDivStyle, ExcelToHtmlUtils.appendAlign( innerDivStyle,
cellStyle.getAlignment() ); cellStyle.getAlignment() );
htmlDocumentFacade.addStyleClass( outerDiv, "d", htmlDocumentFacade.addStyleClass( outerDiv, cssClassPrefixDiv,
innerDivStyle.toString() ); innerDivStyle.toString() );
innerDiv.appendChild( text ); innerDiv.appendChild( text );
@ -633,8 +664,9 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
continue; continue;
Element tableRowElement = htmlDocumentFacade.createTableRow(); Element tableRowElement = htmlDocumentFacade.createTableRow();
htmlDocumentFacade.addStyleClass( tableRowElement, "r", "height:" htmlDocumentFacade.addStyleClass( tableRowElement,
+ ( row.getHeight() / 20f ) + "pt;" ); cssClassPrefixRow, "height:" + ( row.getHeight() / 20f )
+ "pt;" );
int maxRowColumnNumber = processRow( mergedRanges, row, int maxRowColumnNumber = processRow( mergedRanges, row,
tableRowElement ); tableRowElement );
@ -691,9 +723,10 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
{ {
// prepare CSS classes for later usage // prepare CSS classes for later usage
this.cssClassContainerCell = htmlDocumentFacade this.cssClassContainerCell = htmlDocumentFacade
.getOrCreateCssClass( "c", "padding:0;margin:0;align:left;vertical-align:top;" ); .getOrCreateCssClass( cssClassPrefixCell,
"padding:0;margin:0;align:left;vertical-align:top;" );
this.cssClassContainerDiv = htmlDocumentFacade.getOrCreateCssClass( this.cssClassContainerDiv = htmlDocumentFacade.getOrCreateCssClass(
"d", "position:relative;" ); cssClassPrefixDiv, "position:relative;" );
} }
for ( int s = 0; s < workbook.getNumberOfSheets(); s++ ) for ( int s = 0; s < workbook.getNumberOfSheets(); s++ )
@ -705,6 +738,26 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
htmlDocumentFacade.updateStylesheet(); htmlDocumentFacade.updateStylesheet();
} }
public void setCssClassPrefixCell( String cssClassPrefixCell )
{
this.cssClassPrefixCell = cssClassPrefixCell;
}
public void setCssClassPrefixDiv( String cssClassPrefixDiv )
{
this.cssClassPrefixDiv = cssClassPrefixDiv;
}
public void setCssClassPrefixRow( String cssClassPrefixRow )
{
this.cssClassPrefixRow = cssClassPrefixRow;
}
public void setCssClassPrefixTable( String cssClassPrefixTable )
{
this.cssClassPrefixTable = cssClassPrefixTable;
}
/** /**
* Allows converter to wrap content into two additional DIVs with tricky * Allows converter to wrap content into two additional DIVs with tricky
* styles, so it will wrap across empty cells (like in Excel). * styles, so it will wrap across empty cells (like in Excel).