我无法将数据导入到使用Apache poi创建的excel文件中。 Jar使用的是3.7
我有这段代码。
String classtitle=null;
String query="select attsta.username,attsta.status,att.class_title,ind.fname,ind.lname,ind.gender from attendance att,attendancestatus attsta,individual ind where att.attendance_id='"+id+"' and att.attendance_id=attsta.attendance_id and ind.username=attsta.username and att.class_id='"+classid+"'";
try
{
DBinfo obj=new DBinfo();
Connection con=obj.getConnect();
PreparedStatement ps=con.prepareStatement(query);
ResultSet rs=ps.executeQuery();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("lawix10");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue(new HSSFRichTextString("Username"));
rowhead.createCell(1).setCellValue(new HSSFRichTextString("Class Title"));
rowhead.createCell(2).setCellValue(new HSSFRichTextString("Name"));
rowhead.createCell(3).setCellValue(new HSSFRichTextString("Gender"));
int i = 1;
while (rs.next())
{
HSSFRow row = sheet.createRow((short) i);
row.createCell(0).setCellValue(rs.getString(1));
row.createCell(1).setCellValue(rs.getString(2));
classtitle=rs.getString(3);
row.createCell(2).setCellValue(classtitle);
System.out.print(classtitle);
row.createCell(3).setCellValue(rs.getString(4));
i++;
}
FileOutputStream fileOut = new FileOutputStream(classtitle+".xls");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
response.setHeader("Content-Disposition","attachment;filename="+classtitle+".xls");
response.setContentType("application/vnd.ms-excel");
HSSFSheet sheet1 = workbook.getSheetAt(0);
System.out.println(sheet.getPhysicalNumberOfRows()+" "+sheet.getFirstRowNum()+" "+sheet.getLastRowNum());
我得到代码最后一行的结果:HSSFSheet sheet1 = workbook.getSheetAt(0);
System.out.println(sheet.getPhysicalNumberOfRows()+" "+sheet.getFirstRowNum()+" "+sheet.getLastRowNum());
为9 0 8,这是所需的结果。但是浏览器下载的excel表是0字节而没有数据。