poi生成的excel中,如果数据库中某项值显示是空白的 ,在excel中这一格就会显示“null”,
要怎么修改才能改成和数据库中一样显示空白呢?
关键代码如下:
public void WriteExcel(FileOutputStream fOut,ResultSet rs) throws SQLException, IOException{
XSSFWorkbook workbook = new XSSFWorkbook();
// FileOutputStream fOut = new FileOutputStream(".\\" + args[0]
// + "\\" + filename + ".xlsx");
XSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "sheet");
XSSFRow row = sheet.createRow((short) 0);
XSSFCell cell;
ResultSetMetaData md = rs.getMetaData();
int nColumn = md.getColumnCount();
for (int i = 1; i <= nColumn; i++) {
cell = row.createCell((short) (i - 1));
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new XSSFRichTextString(md
.getColumnLabel(i)));
}
int iRow = 1;
while (rs.next()) {
row = sheet.createRow((short) iRow);
;
for (int j = 1; j <= nColumn; j++) {
cell = row.createCell((short) (j - 1));
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new XSSFRichTextString(rs
.getObject(j) + ""));
}
iRow++;
}
workbook.write(fOut);
fOut.flush();
fOut.close();
}
cell.setCellValue(new XSSFRichTextString(rs.getObject(j) + ""));
如果你的rs.getObject(j)是null 那么 null + "" 还是等于null呀
你可以这么来
Object value = rs.getObjec(j);
value = value == null ? "" : value;
cell.setCellValue(value);