gpt4 book ai didi

java - XSSF Excel 命名样式

转载 作者:行者123 更新时间:2023-11-30 02:22:29 28 4
gpt4 key购买 nike

我目前正在使用 Apache POI 库在 Java 中生成 excel 文件。

这就是我想知道的:在 Excel 中,可以创建新的单元格样式并将其添加到工作簿中。这些样式是可重复使用的,并且可以从样式表中进行选择。

使用 Apache POI,您可以在构建工作簿时执行类似的操作。您可以创建一个新的 XSSFCellstyle,将其附加到工作簿,并且可以应用到任意数量的单元格。但是,这些样式不可重复使用。如果我在 Excel 中打开生成的工作簿,并更改其中一种单元格样式,我将永远无法将其更改回我在 XSSF 中生成的未命名样式。这些样式不会添加到工作簿的样式表中。

我只是想知道,是否有任何方法可以在 apache POI 工作簿中创建命名样式,然后在 Excel 中打开文档后可以看到并重新使用该样式?

编辑:经过进一步调查,似乎有一种使用 HSSF 来做到这一点的方法,我们可以使用:

cellStyle.setUserStyleName("Header")

但我找不到任何有关 XSSF 等效信息的信息。有人知道是否可行吗?

最佳答案

如果使用 Office OpenXML 文件格式 *.xlsx,这并不那么容易。但以下内容对我有用。

apache poi FAQ-N10025 中所述,需要所有模式的完整 jar ooxml-schemas-1.3.jar .

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;

import java.lang.reflect.Field;

public class CreateExcelNamedXSSFCellStyle {

static void setNamedCellStyle(XSSFCellStyle style, String name) throws Exception {

Field _stylesSource = XSSFCellStyle.class.getDeclaredField("_stylesSource");
_stylesSource.setAccessible(true);
StylesTable stylestable = (StylesTable)_stylesSource.get(style);
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);

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();
//XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("Mappe1.xlsx"));

XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(style, "My Custom Style 1");

style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(style, "My Custom Style 2");

style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(style, "My Custom Style 3");

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

workbook.write(new FileOutputStream("CreateExcelNamedXSSFCellStyle.xlsx"));
workbook.close();

}
}
<小时/>

代码适应当前apache poi 5.2.2:

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;

import java.lang.reflect.Field;

public class CreateExcelNamedXSSFCellStyle {

static void setNamedCellStyle(XSSFCellStyle style, String name) throws Exception {

Field _stylesSource = XSSFCellStyle.class.getDeclaredField("_stylesSource");
_stylesSource.setAccessible(true);
StylesTable stylestable = (StylesTable)_stylesSource.get(style);
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);

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();
//XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("Mappe1.xlsx"));

XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{(byte)255, 0, 0}, null));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(style, "My Custom Style 1");

style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{0, (byte)255, 0}, null));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(style, "My Custom Style 2");

style = workbook.createCellStyle();
style.setFillForegroundColor(new XSSFColor(new byte[]{0, 0, (byte)255}, null));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
setNamedCellStyle(style, "My Custom Style 3");

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

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

}
}

关于java - XSSF Excel 命名样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46422103/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com