簡體   English   中英

Apache POI - 試圖用 POI 改變重音 colors

[英]Apache POI - Trying to change the accent colors with POI

I'm trying to change the accent colors in POI because we have the need to put some corporate identity design colors into the excel reports for different clients, so that the pivot tables and charts are based on that.

有沒有試過這樣:


    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");

但沒有運氣。 有沒有辦法做到這一點?

對我來說,另一種方法也可能是導出特定樣式 als “.thmx”並在創建報告時導入樣式。 但是對於這個解決方案,我發現 POI 沒有可能性。

希望有人可以幫助我。 謝謝你。

ThemesTable提供getThemeColor方法,該方法返回從 theme theme*.xlm XSSFColor 但這是一個新生成的XSSFColor 對此的更改不會影響主題表中的配色方案,並且在編寫工作簿時不會存儲在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();

 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM