
me.chyxion.xls.css.support.BackgroundApplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of table-to-xls Show documentation
Show all versions of table-to-xls Show documentation
Convert HTML Table To Excel
package me.chyxion.xls.css.support;
import java.util.Map;
import java.util.HashMap;
import me.chyxion.xls.css.CssUtils;
import me.chyxion.xls.css.CssApplier;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
/**
* @version 0.0.1
* @since 0.0.1
* @author Shaun Chyxion
* [email protected]
* Oct 24, 2014 5:03:32 PM
*/
public class BackgroundApplier implements CssApplier {
/**
* {@inheritDoc}
*/
public Map parse(Map style) {
Map mapRtn = new HashMap();
String bg = style.get(BACKGROUND);
String bgColor = null;
if (StringUtils.isNotBlank(bg)) {
for (String bgAttr : bg.split("(?<=\\)|\\w|%)\\s+(?=\\w)")) {
if ((bgColor = CssUtils.processColor(bgAttr)) != null) {
mapRtn.put(BACKGROUND_COLOR, bgColor);
break;
}
}
}
bg = style.get(BACKGROUND_COLOR);
if (StringUtils.isNotBlank(bg) &&
(bgColor = CssUtils.processColor(bg)) != null) {
mapRtn.put(BACKGROUND_COLOR, bgColor);
}
if (bgColor != null) {
bgColor = mapRtn.get(BACKGROUND_COLOR);
if ("#ffffff".equals(bgColor)) {
mapRtn.remove(BACKGROUND_COLOR);
}
}
return mapRtn;
}
/**
* {@inheritDoc}
*/
public void apply(HSSFCell cell, HSSFCellStyle cellStyle, Map style) {
String bgColor = style.get(BACKGROUND_COLOR);
if (StringUtils.isNotBlank(bgColor)) {
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(
CssUtils.parseColor(cell.getSheet().getWorkbook(),
bgColor).getIndex());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy