如何从多个结果集向apache poi添加值

时间:2013-11-08 05:52:26

标签: java excel apache-poi

我正在执行查询并尝试使用apache poi将数据写为excel。现在我有一个要求,我需要在两行差距后显示最后一列的摘要但是当我尝试将第二个查询得到的汇总值写入excel文件,显示空白表。我不知道我在哪里做错了..

这是我的代码..

HSSFWorkbook hwb = new HSSFWorkbook();
                HSSFSheet sheet = hwb.createSheet("CallBillingSystem");
                HSSFRow rowhead = sheet.createRow((short) 0);
                rowhead.createCell((short) 0).setCellValue("CallDate");
                rowhead.createCell((short) 1).setCellValue("CallTime");
                rowhead.createCell((short) 2).setCellValue("Source");
                rowhead.createCell((short) 3).setCellValue("DialedNo");
                rowhead.createCell((short) 4).setCellValue("Extension");
                rowhead.createCell((short) 5).setCellValue("Trunk");
                rowhead.createCell((short) 6).setCellValue("Duration");
                rowhead.createCell((short) 7).setCellValue("CallType");
                rowhead.createCell((short) 8).setCellValue("CallCost");
                rowhead.createCell((short) 9).setCellValue("Site");


                String strQuery = "";
                ResultSet rs = null;
                ResultSet totalcost = null;

                conexion conexiondb = new conexion();
                conexiondb.Conectar();

                // strQuery = "SELECT * FROM cdrcost";

                strQuery = "SELECT *,date(calldate) as date,time(calldate) as time,SEC_TO_TIME(duration) as dur FROM processeddata_table where date(calldate) between '" + query + " and (destination!='h' and destination!='fmpr-5055' and destination!='hangup') order by date(calldate)  ";
                System.out.println(strQuery);
                rs = conexiondb.Consulta(strQuery);
                int i = 1;

                while (rs.next()) {
                    HSSFRow row = sheet.createRow((short) i);
                    row.createCell((short) 0).setCellValue(rs.getString("date"));
                    row.createCell((short) 1).setCellValue(rs.getString("time"));
                    row.createCell((short) 2).setCellValue(rs.getString("source"));
                    row.createCell((short) 3).setCellValue(rs.getString("destination"));
                    row.createCell((short) 4).setCellValue(rs.getString("extension"));
                    row.createCell((short) 5).setCellValue(rs.getString("trunk"));
                    row.createCell((short) 6).setCellValue(rs.getString("dur"));
                    row.createCell((short) 7).setCellValue(rs.getString("toc"));
                    row.createCell((short) 8).setCellValue(rs.getString("callcost"));
                    row.createCell((short) 9).setCellValue(rs.getString("Site"));

                    i++;
                }

                String sumcallcostquery = "SELECT sum(callcost) as totalcost FROM processeddata_table where date(calldate) between '" + query + " and (destination!='h' and destination!='fmpr-5055' and destination!='hangup')";

                totalcost = conexiondb.Consulta(sumcallcostquery);
                while (totalcost.next()) {
                    HSSFRow row = sheet.createRow((short) i);
                    row.createCell((short) 8).setCellValue(rs.getString("totalcost"));
                }


                hwb.write(fos);
                fos.write("HelloWorld".getBytes());

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

            }
        } finally {
            fos.close();
        }

请伙计们帮助我。 提前致谢

0 个答案:

没有答案
相关问题