从数据库生成Excel工作表

时间:2014-05-14 07:08:10

标签: java excel jdbc apache-poi

我试图通过使用我的代码正在工作的一些连接来创建具有来自数据库中不同表的不同不同值的excel表但是在excel表中记录被打印两次我不知道我已经检查了一切请任何人在这里帮助我... 这是我的代码..

public class ExcelFileGen extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    HttpSession session = request.getSession(true);
    String eid = (String) session.getAttribute("eid");

    try {
        String filename = "D:\\DailyWork.xls";
        HSSFWorkbook hwb = new HSSFWorkbook();
        HSSFSheet sheet = hwb.createSheet("new sheet");

        HSSFRow rowhead = sheet.createRow((short) 0);

        rowhead.createCell(0).setCellValue("date");
        rowhead.createCell(1).setCellValue("description");
        rowhead.createCell(2).setCellValue("Support HRS");
        rowhead.createCell(3).setCellValue("Development HRS");
        rowhead.createCell(4).setCellValue("training HRS");
        rowhead.createCell(5).setCellValue("Research HRS");
        rowhead.createCell(6).setCellValue("Meeting HRS");
        rowhead.createCell(7).setCellValue("leave hrs");
        rowhead.createCell(8).setCellValue("Project Name");
        rowhead.createCell(9).setCellValue("activity");
        rowhead.createCell(10).setCellValue("eid");
        rowhead.createCell(11).setCellValue("intime");
        rowhead.createCell(12).setCellValue("outtime");

        Connection con = ConnectionManager.getConnection();
        Statement st = con.createStatement();
                 ResultSet rs = st.executeQuery("select u.date, u.description, u.field1, u.field2, u.field3, u.field4, u.field5, u.field6, u.field7, u.activity, u.eid, d.intime, d.outtime, d.eid from updatework AS u, fulltime as d where d.eid = u.eid AND u.eid='"
                        + eid + "'");
        int i = 1;
        while (rs.next())

        {
            HSSFRow row = sheet.createRow((short) i);
            row.createCell(0).setCellValue(rs.getString("date"));
            row.createCell(1).setCellValue(rs.getString("description"));
            sheet.autoSizeColumn(1);
            row.createCell(2).setCellValue(rs.getString("field1"));
            sheet.autoSizeColumn(2);
            row.createCell(3).setCellValue(rs.getString("field2"));
            sheet.autoSizeColumn(3);
            row.createCell(4).setCellValue(rs.getString("field3"));
            sheet.autoSizeColumn(4);
            row.createCell(5).setCellValue(rs.getString("field4"));
            sheet.autoSizeColumn(5);
            row.createCell(6).setCellValue(rs.getString("field5"));
            sheet.autoSizeColumn(6);
            row.createCell(7).setCellValue(rs.getString("field6"));
            sheet.autoSizeColumn(7);
            row.createCell(8).setCellValue(rs.getString("field7"));
            sheet.autoSizeColumn(8);
            row.createCell(9).setCellValue(rs.getString("activity"));
            sheet.autoSizeColumn(9);
            row.createCell(10).setCellValue(rs.getString("eid"));
            sheet.autoSizeColumn(10);
            row.createCell(11).setCellValue(rs.getString("intime"));
            sheet.autoSizeColumn(11);
            row.createCell(12).setCellValue(rs.getString("outtime"));
            sheet.autoSizeColumn(12);

            i++;
        }
        FileOutputStream fileOut = new FileOutputStream(filename);
        hwb.write(fileOut);
        fileOut.close();
        System.out.println("Your excel file has been generated!");

    } catch (Exception ex) {
        System.out.println(ex);

    }
}

}

1 个答案:

答案 0 :(得分:0)

这是我的解决方案一切都是正确的,但查询不正确现在查询是正确的,它的工作......

 ResultSet rs = st.executeQuery("select u.date, u.description, u.field1, u.field2, u.field3, u.field4, u.field5, u.field6, u.field7, u.activity, u.eid, d.intime, d.outtime, d.eid from updatework AS u, fulltime as d where (d.date=u.date) and (d.eid = u.eid) AND u.eid='"
                    + eid + "'");