我想在 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)
谁能帮我这个
The CTXf
还有quotePrefix
财产是一部分XSSFCellStyle
而不是XSSFCell
.
所以我们必须创建一个XSSFCellStyle
,设置quotePrefix
there然后应用这个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) {
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)