import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
import org.apache.poi.xssf.model.SharedStrings;
+import org.apache.poi.xssf.model.Styles;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.xml.sax.ContentHandler;
* @throws SAXException if parsing the XML data fails.
*/
public void processSheet(
- StylesTable styles,
+ Styles styles,
SharedStrings strings,
SheetContentsHandler sheetHandler,
InputStream sheetInputStream) throws IOException, SAXException {
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
-import org.apache.poi.xssf.model.Comments;
-import org.apache.poi.xssf.model.CommentsTable;
-import org.apache.poi.xssf.model.SharedStrings;
-import org.apache.poi.xssf.model.StylesTable;
+import org.apache.poi.xssf.model.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
/**
* Table with the styles used for formatting
*/
- private StylesTable stylesTable;
+ private Styles stylesTable;
/**
* Table with cell comments
* @param strings Table of shared strings
*/
public XSSFSheetXMLHandler(
- StylesTable styles,
+ Styles styles,
CommentsTable comments,
SharedStrings strings,
SheetContentsHandler sheetContentsHandler,
* @param strings Table of shared strings
*/
public XSSFSheetXMLHandler(
- StylesTable styles,
+ Styles styles,
SharedStrings strings,
SheetContentsHandler sheetContentsHandler,
DataFormatter dataFormatter,
* @param strings Table of shared strings
*/
public XSSFSheetXMLHandler(
- StylesTable styles,
+ Styles styles,
SharedStrings strings,
SheetContentsHandler sheetContentsHandler,
boolean formulasNotResults) {
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
-import org.apache.poi.xssf.model.Comments;
-import org.apache.poi.xssf.model.CommentsTable;
-import org.apache.poi.xssf.model.SharedStrings;
-import org.apache.poi.xssf.model.StylesTable;
+import org.apache.poi.xssf.model.*;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
*/
public void processSheet(
SheetContentsHandler sheetContentsExtractor,
- StylesTable styles,
+ Styles styles,
CommentsTable comments,
SharedStrings strings,
InputStream sheetInputStream)
--- /dev/null
+package org.apache.poi.xssf.model;
+
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFFont;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
+import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
+
+public interface Styles {
+
+ /**
+ * Get number format string given its id
+ *
+ * @param fmtId number format id
+ * @return number format code
+ */
+ String getNumberFormatAt(short fmtId);
+
+ /**
+ * Puts <code>fmt</code> in the numberFormats map if the format is not
+ * already in the the number format style table.
+ * Does nothing if <code>fmt</code> is already in number format style table.
+ *
+ * @param fmt the number format to add to number format style table
+ * @return the index of <code>fmt</code> in the number format style table
+ * @throws IllegalStateException if adding the number format to the styles table
+ * would exceed the max allowed.
+ */
+ int putNumberFormat(String fmt);
+
+ /**
+ * Add a number format with a specific ID into the numberFormats map.
+ * If a format with the same ID already exists, overwrite the format code
+ * with <code>fmt</code>
+ * This may be used to override built-in number formats.
+ *
+ * @param index the number format ID
+ * @param fmt the number format code
+ */
+ void putNumberFormat(short index, String fmt);
+
+ /**
+ * Remove a number format from the style table if it exists.
+ * All cell styles with this number format will be modified to use the default number format.
+ *
+ * @param index the number format id to remove
+ * @return true if the number format was removed
+ */
+ boolean removeNumberFormat(short index);
+
+ /**
+ * Remove a number format from the style table if it exists
+ * All cell styles with this number format will be modified to use the default number format
+ *
+ * @param fmt the number format to remove
+ * @return true if the number format was removed
+ */
+ boolean removeNumberFormat(String fmt);
+
+ XSSFFont getFontAt(int idx);
+
+ /**
+ * Records the given font in the font table.
+ * Will re-use an existing font index if this
+ * font matches another, EXCEPT if forced
+ * registration is requested.
+ * This allows people to create several fonts
+ * then customise them later.
+ * Note - End Users probably want to call
+ * {@link XSSFFont#registerTo(StylesTable)}
+ */
+ int putFont(XSSFFont font, boolean forceRegistration);
+
+ /**
+ * Records the given font in the font table.
+ * Will re-use an existing font index if this
+ * font matches another.
+ */
+ int putFont(XSSFFont font);
+
+ /**
+ *
+ * @param idx style index
+ * @return XSSFCellStyle or null if idx is out of bounds for xfs array
+ */
+ XSSFCellStyle getStyleAt(int idx);
+
+ int putStyle(XSSFCellStyle style);
+
+ XSSFCellBorder getBorderAt(int idx);
+
+ /**
+ * Adds a border to the border style table if it isn't already in the style table
+ * Does nothing if border is already in borders style table
+ *
+ * @param border border to add
+ * @return the index of the added border
+ */
+ int putBorder(XSSFCellBorder border);
+
+ XSSFCellFill getFillAt(int idx);
+
+ /**
+ * Adds a fill to the fill style table if it isn't already in the style table
+ * Does nothing if fill is already in fill style table
+ *
+ * @param fill fill to add
+ * @return the index of the added fill
+ */
+ int putFill(XSSFCellFill fill);
+
+ int getNumCellStyles();
+
+ int getNumDataFormats();
+}
/**
* Table of styles shared across all sheets in a workbook.
*/
-public class StylesTable extends POIXMLDocumentPart {
+public class StylesTable extends POIXMLDocumentPart implements Styles {
private final SortedMap<Short, String> numberFormats = new TreeMap<>();
private final List<XSSFFont> fonts = new ArrayList<>();
private final List<XSSFCellFill> fills = new ArrayList<>();
* @param fmtId number format id
* @return number format code
*/
+ @Override
public String getNumberFormatAt(short fmtId) {
return numberFormats.get(fmtId);
}
* @throws IllegalStateException if adding the number format to the styles table
* would exceed the {@link #MAXIMUM_NUMBER_OF_DATA_FORMATS} allowed.
*/
+ @Override
public int putNumberFormat(String fmt) {
// Check if number format already exists
if (numberFormats.containsValue(fmt)) {
* @param index the number format ID
* @param fmt the number format code
*/
+ @Override
public void putNumberFormat(short index, String fmt) {
numberFormats.put(index, fmt);
}
* @param index the number format id to remove
* @return true if the number format was removed
*/
+ @Override
public boolean removeNumberFormat(short index) {
String fmt = numberFormats.remove(index);
boolean removed = (fmt != null);
* @param fmt the number format to remove
* @return true if the number format was removed
*/
+ @Override
public boolean removeNumberFormat(String fmt) {
short id = getNumberFormatId(fmt);
return removeNumberFormat(id);
}
+ @Override
public XSSFFont getFontAt(int idx) {
return fonts.get(idx);
}
* Note - End Users probably want to call
* {@link XSSFFont#registerTo(StylesTable)}
*/
+ @Override
public int putFont(XSSFFont font, boolean forceRegistration) {
int idx = -1;
if(!forceRegistration) {
fonts.add(font);
return idx;
}
+
+ @Override
public int putFont(XSSFFont font) {
return putFont(font, false);
}
* @param idx style index
* @return XSSFCellStyle or null if idx is out of bounds for xfs array
*/
+ @Override
public XSSFCellStyle getStyleAt(int idx) {
int styleXfId = 0;
return new XSSFCellStyle(idx, styleXfId, this, theme);
}
+
+ @Override
public int putStyle(XSSFCellStyle style) {
CTXf mainXF = style.getCoreXf();
return xfs.indexOf(mainXF);
}
+ @Override
public XSSFCellBorder getBorderAt(int idx) {
return borders.get(idx);
}
* @param border border to add
* @return the index of the added border
*/
+ @Override
public int putBorder(XSSFCellBorder border) {
int idx = borders.indexOf(border);
if (idx != -1) {
return borders.size() - 1;
}
+ @Override
public XSSFCellFill getFillAt(int idx) {
return fills.get(idx);
}
* @param fill fill to add
* @return the index of the added fill
*/
+ @Override
public int putFill(XSSFCellFill fill) {
int idx = fills.indexOf(fill);
if (idx != -1) {
/**
* get the size of cell styles
*/
- public int getNumCellStyles(){
+ @Override
+ public int getNumCellStyles() {
// Each cell style has a unique xfs entry
// Several might share the same styleXfs entry
return xfs.size();
/**
* @return number of data formats in the styles table
*/
+ @Override
public int getNumDataFormats() {
return numberFormats.size();
}
--- /dev/null
+package org.apache.poi.xssf.model;
+
+import org.apache.poi.xssf.usermodel.XSSFColor;
+
+public interface Themes {
+ /**
+ * Convert a theme "index" (as used by fonts etc) into a color.
+ * @param idx A theme "index"
+ * @return The mapped XSSFColor, or null if not mapped.
+ */
+ XSSFColor getThemeColor(int idx);
+
+ /**
+ * If the colour is based on a theme, then inherit
+ * information (currently just colours) from it as
+ * required.
+ */
+ void inheritFromThemeAsRequired(XSSFColor color);
+}
* Class that represents theme of XLSX document. The theme includes specific
* colors and fonts.
*/
-public class ThemesTable extends POIXMLDocumentPart {
+public class ThemesTable extends POIXMLDocumentPart implements Themes {
public enum ThemeElement {
LT1(0, "Lt1"),
DK1(1,"Dk1"),
* @param idx A theme "index"
* @return The mapped XSSFColor, or null if not mapped.
*/
+ @Override
public XSSFColor getThemeColor(int idx) {
// Theme color references are NOT positional indices into the color scheme,
// i.e. these keys are NOT the same as the order in which theme colors appear
* information (currently just colours) from it as
* required.
*/
+ @Override
public void inheritFromThemeAsRequired(XSSFColor color) {
if(color == null) {
// Nothing for us to do