在 POI 中添加带有 quotePrefix 的单元格 [英] Adding cell with quotePrefix in POI

查看:29
本文介绍了在 POI 中添加带有 quotePrefix 的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在包含引号前缀的 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) 

谁能帮我解决这个问题

推荐答案

CTXfquotePrefix 属性是 XSSFCellStyle 的一部分而不是 XSSFCell.

The CTXf and also the quotePrefix property is part of the XSSFCellStyle and not the XSSFCell.

所以我们必须创建一个 XSSFCellStyle,设置 quotePrefix 在那里,然后将这个 XSSFCellStyle 应用到 quotePrefix代码>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 的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆