有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用Apache POI获取Excel工作表中的数据

我无法将数据放入使用Apache poi创建的excel文件中。 使用的罐子是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字节的,没有数据


共 (0) 个答案