有沒有辦法在 Apache POI 中使用默認的 Excel 單元格樣式(見下文),類似于
uj5u.com熱心網友回復:
如果“使用默認 Excel 單元格樣式的方法”意味著能夠使用命名單元格樣式設定單元格樣式apache poi
,那么不,這是不可能的。
單元格樣式模板正是如此,單元格樣式的模板。如果在 Excel GUI 中選擇,GUI 會根據所選模板在作業簿級別創建單元格樣式,然后將該單元格樣式應用于單元格。不可能只將單元格樣式的名稱存盤到 Excel 檔案中,然后期望 Excel 相應地設定單元格樣式。Apache poi
必須做同樣的事情。但是它必須知道單個模板定義了什么。但這似乎沒有記錄在任何地方。CellStyle 類狀態:
附件 H 包含一個 cellStyles 串列,其相應的格式記錄是隱含的,而不是明確保存在檔案中的。在這種情況下,在 cellStyle 記錄上寫入了 builtinId 屬性,但沒有寫入相應的格式記錄。
但我無法在任何地方找到該附件 H。也許我太笨了,找不到它。
我們唯一能做的就是創建自己的自定義命名單元格樣式,然后可以使用。
而且,如果我們builtinId
以某種方式獲得了內置的單元格樣式模板,那么我們可以創建單元格樣式來覆寫那些內置的單元格樣式模板。但是要獲得builtinId
內置的單元格樣式模板只能通過試驗/錯誤進行,因為它們沒有記錄:
創建一個使用一些內置單元格樣式模板的 Excel 檔案。將該檔案另存為*.xlsx
檔案。然后解壓縮該*.xlsx
檔案并查看/xl/styles.xml
. 看看builtinId
他們有什么:
<cellStyles count="4">
<cellStyle name="Gut" xfId="1" builtinId="26"/>
<cellStyle name="Neutral" xfId="3" builtinId="28"/>
<cellStyle name="Schlecht" xfId="2" builtinId="27"/>
<cellStyle name="Standard" xfId="0" builtinId="0"/>
</cellStyles>
以下完整的示例兩者兼而有之。它創建三個命名的自定義單元格樣式,然后在單元格樣式模板串列中可見。
它還覆寫具有 builtinId="26" 的命名單元格樣式。這是名為“Good”的單元格樣式。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyles;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyleXfs;
public class CreateExcelNamedXSSFCellStyle {
static void setNamedCellStyle(XSSFWorkbook workbook, XSSFCellStyle style, String name, long builtinId) throws Exception {
StylesTable stylestable = workbook.getStylesSource();
CTStylesheet ctstylesheet = stylestable.getCTStylesheet();
CTCellStyles ctcellstyles = ctstylesheet.getCellStyles();
CTXf ctxfcore = style.getCoreXf();
if (ctcellstyles == null) {
ctcellstyles = ctstylesheet.addNewCellStyles();
ctcellstyles.setCount(2);
CTCellStyle ctcellstyle = ctcellstyles.addNewCellStyle(); //CellStyle for default built-in cell style
ctcellstyle.setXfId(0);
ctcellstyle.setBuiltinId(0);
ctcellstyle = ctcellstyles.addNewCellStyle();
ctcellstyle.setXfId(1);
ctcellstyle.setName(name);
ctxfcore.setXfId(1);
} else {
long stylescount = ctcellstyles.getCount();
ctcellstyles.setCount(stylescount 1);
CTCellStyle ctcellstyle = ctcellstyles.addNewCellStyle();
ctcellstyle.setXfId(stylescount);
ctcellstyle.setName(name);
if (builtinId > -1) ctcellstyle.setBuiltinId(builtinId);
ctxfcore.setXfId(stylescount);
}
CTXf ctxfstyle = CTXf.Factory.newInstance();
ctxfstyle.setNumFmtId(ctxfcore.getNumFmtId());
ctxfstyle.setFontId(ctxfcore.getFontId());
ctxfstyle.setFillId(ctxfcore.getFillId());
ctxfstyle.setBorderId(ctxfcore.getBorderId());
stylestable.putCellStyleXf(ctxfstyle);
}
static XSSFCellStyle getNamedCellStyle(XSSFWorkbook workbook, String name) {
StylesTable stylestable = workbook.getStylesSource();
CTStylesheet ctstylesheet = stylestable.getCTStylesheet();
CTCellStyles ctcellstyles = ctstylesheet.getCellStyles();
if (ctcellstyles != null) {
int i = 0;
XSSFCellStyle style = null;
while((style = stylestable.getStyleAt(i )) != null) {
CTXf ctxfcore = style.getCoreXf();
long xfid = ctxfcore.getXfId();
for (CTCellStyle ctcellstyle : ctcellstyles.getCellStyleList()) {
if (ctcellstyle.getXfId() == xfid && name.equals(ctcellstyle.getName())) {
return style;
}
}
}
}
return workbook.getCellStyleAt(0); //if nothing found return default cell style
}
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
//The following creates three named custom cell styles
IndexedColorMap colorMap = workbook.getStylesSource().getIndexedColors();
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{(byte)255, 0, 0}, colorMap));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(workbook, style, "My Custom Style 1", -1);
style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{0, (byte)255, 0}, colorMap));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(workbook, style, "My Custom Style 2", -1);
style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{0, 0, (byte)255}, colorMap));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(workbook, style, "My Custom Style 3", -1);
//The following uses the custom named cell styles
XSSFSheet sheet = workbook.createSheet("TestSheet");
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < 3; i ) {
XSSFCell cell = row.createCell(i);
style = getNamedCellStyle(workbook, "My Custom Style " (i 1));
cell.setCellStyle(style);
}
row = sheet.createRow(2);
XSSFCell cell = row.createCell(0);
style = getNamedCellStyle(workbook, "not found");
cell.setCellStyle(style);
//The following overwrites the named cell style having builtinId="26". This is the cell style named "Good".
style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{(byte)198, (byte)239, (byte)206}, colorMap));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(workbook, style, "Good", 26);
cell = row.createCell(1);
style = getNamedCellStyle(workbook, "Good");
cell.setCellStyle(style);
FileOutputStream out = new FileOutputStream("CreateExcelNamedXSSFCellStyle.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/470788.html
標籤:爪哇 擅长 apache-poi