当前位置: 首页 > 面试题库 >

在POI中添加带有quotePrefix的单元格

常甫
2023-03-14
问题内容

我想在包含引号前缀的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财产的一部分XSSFCellStyle,而不是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);
   fileOut.close();

  } catch (IOException ioex) {
  }
 }
}


 类似资料:
  • 我使用的是Apache POI,我需要在一个单元格或合并区域的范围内放置边框。我正在合并三行五列的单元格。但我无法添加边界。那么我该怎么做呢?

  • 我想让标题行有不同的颜色。到目前为止,我只是对字体有不同的颜色,因为背景颜色不能像我希望的那样工作。我的输出文件应为xls格式。 我尝试了命令,但这是igno。有没有另一种给背景着色的方法?

  • 问题内容: 我正在使用POI生成Excel文件。我需要为工作表中的特定单元格添加边框。 我该怎么做? 问题答案: 以单元格中使用的样式设置边框即可完成此操作。例:

  • 有人知道怎么做到这一点吗?提前谢了。

  • 问题内容: 如何在Swift中的tableView单元中以编程方式嵌入?我就是那样做 问题答案: 这是您可以在单元格上嵌入的方法。 这是切换调用贝克方法 @LeoDabus 。 注意:如果您可能有多个,则应创建一个CustomCell子类并配置您的内部 方法而不是表视图方法。当将可重用的队列释放出队列时,将其投射到您的CustomCell中这是@LeoDabus的示例

  • 背景:当使用Microsoft Word时,可以定义字段,然后用一些值替换这些字段。 编辑:某些字段的XML如下所示(在本例中是文档的作者)