I have a JDBC result set which I am trying to export it as a table in Word document (docx). The JDBC result set has 30+ columns; when the Word document is generated, it's showing only 10 columns and the rest gets hidden. How do I show the remaining 20+ columns? Is there a way to make the Word document orientation landscape or set custom page size?
Here is my Java code:
@Override
public Void extractData(ResultSet rs) throws SQLException, DataAccessException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
System.out.println("Columns : " + columnsNumber);
XWPFDocument document = new XWPFDocument();
// Creating Table with 1 row and as many columns as in the result set
XWPFTable table = document.createTable(1, columnsNumber);
table.setWidth("100%");
//Get header Row
XWPFTableRow header = table.getRow(0);
long c = header.getTableCells().stream().count();
System.out.println("Count of cells : "+ c);
//Set header columns
for (int col = 0; col < columnsNumber; col++) {
System.out.println("header col : " + col);
header.getCell(col).setText(rsmd.getColumnLabel(col + 1));
}
//Set data rows
while (rs.next()) {
System.out.println("RS row : " + rs.getRow());
XWPFTableRow desRow = table.createRow();
for (int col = 0; col < columnsNumber; col++) {
final var value = rs.getObject(col + 1);
String v = value == null ? "" : value.toString();
desRow.getCell(col).setText(v);
System.out.println("RS row col : "+ col);
}
}
try {
document.write(os);
document.close();
} catch (IOException e) {
log.error("Error occurred: {0}", e);
}
return null;
}
Maven dependency being used
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
Snapshot of table generated