在POI中使用quotePrefix添加单元格

我想在包含引号前缀的xlsx工作簿表单中添加一个单元格,我正在尝试使用POI库创建该表单。 如何添加此类单元格

我在maven central上找到了对CTXf.setQuotePrefix(boolean quotePrefix)的引用,但是不知道如何将它添加到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) 

谁能帮我这个

CTXfquotePrefix属性是quotePrefix的一部分,而不是XSSFCell

因此,我们必须创建一个XSSFCellStyle ,在其中设置quotePrefix ,然后将此XSSFCellStyle应用于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); } catch (IOException ioex) { } } }