问题描述
我想在xlsx工作簿工作表中添加一个包含引号前缀的单元格,而我正在尝试使用POI库创建该工作表.如何添加这种类型的单元格
I want to add a cell in xlsx workbooks sheet containing the quote prefix, and i am trying to create that sheet using POI library. How do I add this type of cell
我在Maven Central上使用CTXf.setQuotePrefix(boolean quotePrefix)找到了对此的引用,但不知道如何将其添加到XSSFCell中.
I found a reference to this with CTXf.setQuotePrefix(boolean quotePrefix) on maven central, but dont know how to add this to the XSSFCell
我尝试使用下面的代码
XSSFCell cell=row.createCell(cellIndex);
CTXfImpl ctxf= new CTXfImpl(XmlObject.Factory.newInstance().schemaType());
ctxf.setQuotePrefix(true);
cell.getCTCell().set(ctxf);
cell.setCellValue(data);
获取异常
Exception in thread "main" java.lang.NullPointerException
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTXfImpl.setQuotePrefix(Unknown Source)
有人可以帮我吗
推荐答案
CTXf
和quotePrefix
属性是XSSFCellStyle
而不是XSSFCell
的一部分.
The CTXf
and also the quotePrefix
property is part of the XSSFCellStyle
and not the XSSFCell
.
因此,我们必须创建一个XSSFCellStyle
,设置quotePrefix
有,然后将此XSSFCellStyle
应用于XSSFCell
.
So we must create a XSSFCellStyle
, set the quotePrefix
there and then apply this XSSFCellStyle
to the XSSFCell
.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
class WriteQuotePrefix {
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
CellStyle style = wb.createCellStyle();
((XSSFCellStyle)style).getCoreXf().setQuotePrefix(true);
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue("1234");
FileOutputStream fileOut = new FileOutputStream("WriteQuotePrefix.xlsx");
wb.write(fileOut);
fileOut.close();
} catch (IOException ioex) {
}
}
}
这篇关于在POI中添加带有quotePrefix的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!