在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)
谁能帮我这个
CTXf
和quotePrefix
属性是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) { } } }
- 使用Apache POI 4.0运行官方示例LineChars和ScatterChart时出现问题
- HSSFWorkbook vs XSSFWorkbook vs SXSSFWorkbook – Apache-poi
- 使用apache poi的PatternSyntaxException
- apache POI – 获取生成的excel文件的大小
- 使用页码指示为XWPFDocument创建目录
- 如何将.docx的段落复制到另一个.docx withJava并保留样式
- Java如何使用JFileChooser来保存Apache POI创建的excel文件
- Apache-POI在Excel中设置值,但是在我手动按下处理条中的enter之前,另一个单元格的公式无法使用该值
- 阿帕奇poi子弹和编号