poi xml模板 操作pdf_尝试使用Apache poi制作简单的PDF文档

I see the internet is riddled with people complaining about apache's pdf products, but I cannot find my particular usecase here. I am trying to do a simple Hello World with apache poi. Right now my code is as follows:

public ByteArrayOutputStream export() throws IOException {

//Blank Document

XWPFDocument document = new XWPFDocument();

//Write the Document in file system

ByteArrayOutputStream out = new ByteArrayOutputStream();;

//create table

XWPFTable table = document.createTable();

XWPFStyles styles = document.createStyles();

styles.setSpellingLanguage("English");

//create first row

XWPFTableRow tableRowOne = table.getRow(0);

tableRowOne.getCell(0).setText("col one, row one");

tableRowOne.addNewTableCell().setText("col two, row one");

tableRowOne.addNewTableCell().setText("col three, row one");

//create second row

XWPFTableRow tableRowTwo = table.createRow();

tableRowTwo.getCell(0).setText("col one, row two");

tableRowTwo.getCell(1).setText("col two, row two");

tableRowTwo.getCell(2).setText("col three, row two");

//create third row

XWPFTableRow tableRowThree = table.createRow();

tableRowThree.getCell(0).setText("col one, row three");

tableRowThree.getCell(1).setText("col two, row three");

tableRowThree.getCell(2).setText("col three, row three");

PdfOptions options = PdfOptions.create();

PdfConverter.getInstance().convert(document, out, options);

out.close();

return out;

}

and the code that calls this is:

public ResponseEntity convertToPDFPost(@ApiParam(value = "DTOs passed from the FE" ,required=true ) @Valid @RequestBody ExportEnvelopeDTO exportDtos) {

if (exportDtos.getProdExportDTOs() != null) {

try {

FileOutputStream out = new FileOutputStream("/Users/kornhaus/Desktop/test.pdf");

out.write(exporter.export().toByteArray());

out.close();

} catch (IOException e) {

e.printStackTrace();

}

return new ResponseEntity(responseFile, responseHeaders, HttpStatus.OK);

}

return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);

}

}

On this line here: out.write(exporter.export().toByteArray());

the code throws an exception:

org.apache.poi.xwpf.conv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值