Apache POI - 尝试使用 POI 更改强调色

Apache POI - Trying to change the accent colors with POI

我正在尝试更改 POI 中的强调色,因为我们需要将一些企业标识设计颜色放入不同客户的 excel 报告中,以便数据透视表和图表基于那。

有没有这样试过:


    StylesTable st = workbook.getStylesSource();
    ThemesTable theme = st.getTheme();
    // get one of the accent colors -> work
    XSSFColor accent1 = theme.getThemeColor(4);
    // trying to set a new color -> doesn't work
    st.getTheme().getThemeColor(i).setARGBHex("ED7D31");

但运气不好。有办法实现吗?

我的另一种方法是将特定样式导出为“.thmx”并在创建报告时导入样式。但是对于这个解决方案,我发现 POI 不可能。

希望有人能帮助我。谢谢。

ThemesTable 提供 getThemeColor 方法,其中 returns 从 theme*.xlm 生成一个 XSSFColor。但这是新生成的XSSFColor。对此的更改不会影响主题 table 中的配色方案,并且在编写工作簿时不会存储在 theme*.xlm 中。

ThemesTable 直到现在才提供 setThemeColor 方法。所以我们需要使用 org.openxmlformats.schemas.drawingml.x2006.main.*.

的底层低级方法来编程我们自己的方法

以下使用电流 apache poi 5.0.0 对我有用。它取代了 accent1 配色方案中的 RGB 颜色。

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.ThemesTable;
import org.apache.poi.xssf.model.ThemesTable.ThemeElement;

import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor;

import java.lang.reflect.Field;

class ExcelSetThemeColor {

 static void setThemeColor(ThemesTable xssfTheme, int idx, XSSFColor color) throws Exception {
  Field _theme = ThemesTable.class.getDeclaredField("theme");
  _theme.setAccessible(true);
  ThemeDocument theme = (ThemeDocument)_theme.get(xssfTheme);
  CTColorScheme colorScheme = theme.getTheme().getThemeElements().getClrScheme();
  CTColor ctColor = CTColor.Factory.newInstance();
  CTSRgbColor rgbColor = ctColor.addNewSrgbClr();
  rgbColor.setVal(color.getRGB());
  switch (ThemeElement.byId(idx)) {
   case LT1: colorScheme.setLt1(ctColor); break;
   case DK1: colorScheme.setDk1(ctColor); break;
   case LT2: colorScheme.setLt2(ctColor); break;
   case DK2: colorScheme.setDk2(ctColor); break;
   case ACCENT1: colorScheme.setAccent1(ctColor); break;
   case ACCENT2: colorScheme.setAccent2(ctColor); break;
   case ACCENT3: colorScheme.setAccent3(ctColor); break;
   case ACCENT4: colorScheme.setAccent4(ctColor); break;
   case ACCENT5: colorScheme.setAccent5(ctColor); break;
   case ACCENT6: colorScheme.setAccent6(ctColor); break;
   case HLINK: colorScheme.setHlink(ctColor); break;
   case FOLHLINK: colorScheme.setFolHlink(ctColor); break;
   default: ;
  }
 }

 public static void main(String[] args) throws Exception {
  XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("./ExcelTemplate.xlsx"));

  StylesTable st = workbook.getStylesSource();
  ThemesTable theme = st.getTheme();
  // get one of the accent colors -> work
  XSSFColor accent1 = theme.getThemeColor(4);
System.out.println(accent1.getARGBHex());

  // trying to set a new color
  accent1.setARGBHex("ED7D31");

  setThemeColor(theme, 4, accent1);
System.out.println(st.getTheme().getThemeColor(4).getARGBHex());

 
  FileOutputStream out = new FileOutputStream("./Excel.xlsx");
  workbook.write(out);
  out.close();
  workbook.close();

 }
}