Adding cell with quotePrefix in POI

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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

I found a reference to this with CTXf.setQuotePrefix(boolean quotePrefix) on maven central, but dont know how to add this to the XSSFCell

I have tried using below code

XSSFCell cell=row.createCell(cellIndex); CTXfImpl ctxf= new CTXfImpl(XmlObject.Factory.newInstance().schemaType()); ctxf.setQuotePrefix(true); cell.getCTCell().set(ctxf); cell.setCellValue(data); 

getting exception

Exception in thread "main" java.lang.NullPointerException     at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTXfImpl.setQuotePrefix(Unknown Source)  

Can anyone help me with this

回答1:

The CTXf and also the quotePrefix property is part of the XSSFCellStyle and not the 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);    } catch (IOException ioex) {   }  } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!