简体   繁体   English

从数据库生成Excel工作表

[英]generating excel sheet from database

I am trying to create excel sheet with different different values from different table in database by using some joins my code is working but record is getting printed twice in the excel sheet i do not know i have checked everything please anyone help me here... here is my code.. 我正在尝试通过使用某些联接从数据库中的不同表中创建具有不同值的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);

    }
}

} }

here is my solution everything was correct only but query was not proper now query is correct and its working ... 这是我的解决方案,只是一切都是正确的,但查询不正确,现在查询是正确的,它的工作...

 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 + "'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM