private final HSSFDataFormatter _formatter = new HSSFDataFormatter();
- private Map<String, String> cssStyleToClass = new LinkedHashMap<String, String>();
+ private String cssClassContainerCell = null;
+
+ private String cssClassContainerDiv = null;
+
+ private final String cssClassTable;
private Map<Short, String> excelStyleToClass = new LinkedHashMap<Short, String>();
private boolean outputRowNumbers = true;
- private final Element stylesElement;
-
private boolean useDivsToSpan = false;
public ExcelToHtmlConverter( Document doc )
{
htmlDocumentFacade = new HtmlDocumentFacade( doc );
-
- stylesElement = doc.createElement( "style" );
- stylesElement.setAttribute( "type", "text/css" );
- htmlDocumentFacade.getHead().appendChild( stylesElement );
+ cssClassTable = htmlDocumentFacade.getOrCreateCssClass( "table", "t",
+ "border-collapse:collapse;border-spacing:0;" );
}
protected String buildStyle( HSSFWorkbook workbook, HSSFCellStyle cellStyle )
protected String getStyleClassName( HSSFWorkbook workbook,
HSSFCellStyle cellStyle )
{
- String knownClass = excelStyleToClass.get( Short.valueOf( cellStyle
- .getIndex() ) );
- if ( knownClass != null )
- return knownClass;
+ final Short cellStyleKey = Short.valueOf( cellStyle.getIndex() );
- String cssStyle = buildStyle( workbook, cellStyle );
- knownClass = cssStyleToClass.get( cssStyle );
+ String knownClass = excelStyleToClass.get( cellStyleKey );
if ( knownClass != null )
- {
- excelStyleToClass.put( Short.valueOf( cellStyle.getIndex() ),
- knownClass );
return knownClass;
- }
- knownClass = "c" + cellStyle.getIndex();
- cssStyleToClass.put( cssStyle, knownClass );
- excelStyleToClass.put( Short.valueOf( cellStyle.getIndex() ),
- knownClass );
- return knownClass;
+ String cssStyle = buildStyle( workbook, cellStyle );
+ String cssClass = htmlDocumentFacade.getOrCreateCssClass( "td", "c",
+ cssStyle );
+ excelStyleToClass.put( cellStyleKey, cssClass );
+ return cssClass;
}
public boolean isOutputColumnHeaders()
final short cellStyleIndex = cellStyle.getIndex();
if ( cellStyleIndex != 0 )
{
- tableCellElement.setAttribute( "class",
- getStyleClassName( workbook, cellStyle ) );
+ String mainCssClass = getStyleClassName( workbook, cellStyle );
+ if ( !noText && isUseDivsToSpan() )
+ {
+ tableCellElement.setAttribute( "class", mainCssClass + " "
+ + cssClassContainerCell );
+ }
+ else
+ {
+ tableCellElement.setAttribute( "class", mainCssClass );
+ }
+
if ( noText )
{
/*
if ( !noText && isUseDivsToSpan() )
{
- tableCellElement.setAttribute( "style",
- "padding:0;margin:0;align:left;vertical-align:top;" );
- Element outerDiv = htmlDocumentFacade.getDocument().createElement(
- "div" );
- outerDiv.setAttribute( "style", "position:relative;" );
-
- Element innerDiv = htmlDocumentFacade.getDocument().createElement(
- "div" );
+ Element outerDiv = htmlDocumentFacade.createBlock();
+ outerDiv.setAttribute( "class", this.cssClassContainerDiv );
+
+ Element innerDiv = htmlDocumentFacade.createBlock();
StringBuilder innerDivStyle = new StringBuilder();
innerDivStyle.append( "position:absolute;min-width:" );
innerDivStyle.append( normalWidthPx );
innerDivStyle.append( "pt;white-space:nowrap;" );
ExcelToHtmlUtils.appendAlign( innerDivStyle,
cellStyle.getAlignment() );
- innerDiv.setAttribute( "style", innerDivStyle.toString() );
+ innerDiv.setAttribute(
+ "class",
+ htmlDocumentFacade.getOrCreateCssClass(
+ outerDiv.getTagName(), "d",
+ innerDivStyle.toString() ) );
innerDiv.appendChild( text );
outerDiv.appendChild( innerDiv );
return;
Element table = htmlDocumentFacade.createTable();
- table.setAttribute( "class", "t" );
+ table.setAttribute( "class", cssClassTable );
Element tableBody = htmlDocumentFacade.createTableBody();
continue;
Element tableRowElement = htmlDocumentFacade.createTableRow();
- tableRowElement.setAttribute( "style",
- "height:" + ( row.getHeight() / 20f ) + "pt;" );
+ tableRowElement.setAttribute(
+ "class",
+ htmlDocumentFacade.getOrCreateCssClass(
+ tableRowElement.getTagName(), "r", "height:"
+ + ( row.getHeight() / 20f ) + "pt;" ) );
int maxRowColumnNumber = processRow( workbook, sheet, row,
tableRowElement );
processDocumentInformation( summaryInformation );
}
+ if ( isUseDivsToSpan() )
+ {
+ // prepare CSS classes for later usage
+ this.cssClassContainerCell = htmlDocumentFacade
+ .getOrCreateCssClass( "td", "c",
+ "padding:0;margin:0;align:left;vertical-align:top;" );
+ this.cssClassContainerDiv = htmlDocumentFacade.getOrCreateCssClass(
+ "div", "d", "position:relative;" );
+ }
+
for ( int s = 0; s < workbook.getNumberOfSheets(); s++ )
{
HSSFSheet sheet = workbook.getSheetAt( s );
processSheet( workbook, sheet );
}
- stylesElement
- .appendChild( htmlDocumentFacade
- .createText( "table.t{border-collapse:collapse;border-spacing:0;}\n" ) );
- if ( !cssStyleToClass.isEmpty() )
- {
- for ( Map.Entry<String, String> entry : cssStyleToClass.entrySet() )
- {
- stylesElement.appendChild( htmlDocumentFacade.createText( "td."
- + entry.getValue() + "{" + entry.getKey() + "}\n" ) );
- }
- }
+ htmlDocumentFacade.updateStylesheet();
}
public void setOutputColumnHeaders( boolean outputColumnHeaders )
==================================================================== */
package org.apache.poi.hwpf.converter;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class HtmlDocumentFacade
{
-
protected final Element body;
protected final Document document;
protected final Element head;
protected final Element html;
+ /**
+ * Map from tag name, to map linking known styles and css class names
+ */
+ private Map<String, Map<String, String>> stylesheet = new LinkedHashMap<String, Map<String, String>>();
+ private Element stylesheetElement;
+
protected Element title;
protected Text titleText;
body = document.createElement( "body" );
head = document.createElement( "head" );
+ stylesheetElement = document.createElement( "style" );
+ stylesheetElement.setAttribute( "type", "text/css" );
html.appendChild( head );
html.appendChild( body );
+ head.appendChild( stylesheetElement );
- body.setAttribute( "style", "white-space-collapsing: preserve; " );
+ body.setAttribute(
+ "class",
+ getOrCreateCssClass( "body", "b",
+ "white-space-collapsing: preserve; " ) );
}
public void addAuthor( String value )
head.appendChild( meta );
}
+ public Element createBlock()
+ {
+ return document.createElement( "div" );
+ }
+
public Element createHeader1()
{
return document.createElement( "h1" );
return head;
}
+ public String getOrCreateCssClass( String tagName, String classNamePrefix,
+ String style )
+ {
+ if ( !stylesheet.containsKey( tagName ) )
+ stylesheet.put( tagName, new LinkedHashMap<String, String>( 1 ) );
+
+ Map<String, String> styleToClassName = stylesheet.get( tagName );
+ String knownClass = styleToClassName.get( style );
+ if ( knownClass != null )
+ return knownClass;
+
+ String newClassName = classNamePrefix + ( styleToClassName.size() + 1 );
+ styleToClassName.put( style, newClassName );
+ return newClassName;
+ }
+
public String getTitle()
{
if ( title == null )
this.titleText.setData( titleText );
}
+
+ public void updateStylesheet()
+ {
+ StringBuilder stringBuilder = new StringBuilder();
+ for ( Map.Entry<String, Map<String, String>> byTag : stylesheet
+ .entrySet() )
+ {
+ String tagName = byTag.getKey();
+ for ( Map.Entry<String, String> byStyle : byTag.getValue()
+ .entrySet() )
+ {
+ String style = byStyle.getKey();
+ String className = byStyle.getValue();
+
+ stringBuilder.append( tagName + "." + className + "{" + style
+ + "}\n" );
+ }
+ }
+ stylesheetElement.setTextContent( stringBuilder.toString() );
+ }
}