我正在使用apache poi创建ms word文档。我想创建一个这样的表。
为此,我需要合并表单元格。我写了这段代码
XWPFParagraph paragraph = document.createParagraph();
XWPFRun baslik1 = paragraph.createRun();
baslik1.setFontSize(12);
baslik1.setBold(true);
baslik1.setText("SECTION 1: MAINTENANCE SUMMARY");
baslik1.addBreak();
InputStream summary_pic=access_connection.class.getResourceAsStream("summary.png");
try {
String reason = null;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
baglanti2= DriverManager.getConnection("jdbc:ucanaccess://" + db_ans,db_ans_username,db_ans_password);
sorgu2=baglanti2.createStatement();
ResultSet summary=sorgu2.executeQuery("SELECT * FROM TableWSINI WHERE strSO='"+salesOrder+"'");
while(summary.next())
{
reason=summary.getString("strRemReason");
}
XWPFParagraph para = null;
CTHMerge hMerge=CTHMerge.Factory.newInstance();
hMerge.setVal(STMerge.CONTINUE);
XWPFTable table=document.createTable(18,5);
table.getRow(0).getCell(0).setText("Removal Reason");
table.getRow(0).getCell(1).setText(reason);
table.getRow(0).getCell(2).getCTTc().getTcPr().setHMerge(hMerge);
table.getRow(0).getCell(3).getCTTc().getTcPr().setHMerge(hMerge);
table.getRow(0).getCell(4).getCTTc().getTcPr().setHMerge(hMerge);
XWPFTableRow row2=table.getRow(0); //row2
para=row2.getCell(0).addParagraph();
XWPFRun run=para.createRun();
run.addPicture(summary_pic, XWPFDocument.PICTURE_TYPE_PNG, "Images", Units.toEMU(500), Units.toEMU(250));
FileOutputStream input = new FileOutputStream(new File(word_saveas_file+salesOrder+"_"+type+"_Report.docx"));
document.write(input);
input.close();
但是我遇到了这个错误。
java.lang.NullPointerException
at access_read_and_write.access_connection.summary(access_connection.java:1370)
at access_read_and_write.access_connection.Start(access_connection.java:228)
at Servlet.shop_visit_report.doGet(shop_visit_report.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52
我的第1370行是 table.getRow(0).getCell(2).getCTTc()。getTcPr()。setHMerge(hMerge);