如何在将数据放入excel wrokBook后打开Excel文件并要求用户自动保存。(apache poi java)

时间:2018-03-20 08:15:20

标签: java excel apache-poi

我使用apache poi将数据写入excel文件,如下所示

String filename="f:/"+mdy+"MonthlyReport.xls"; 
            HSSFWorkbook hwb=new HSSFWorkbook();  
            HSSFSheet sheet =  hwb.createSheet("MonthlyReport");                                
            HSSFRow rowhead=   sheet.createRow((short)0);      
            rowhead.createCell((short) 0).setCellValue("MONTH_YEAR ");  
            rowhead.createCell((short) 1).setCellValue("NO_OF_TICKETS ");
            rowhead.createCell((short) 2).setCellValue("TOTAL_AMOUNT");
            rowhead.createCell((short) 3).setCellValue("NO_OF_AGENTS");
            rowhead.createCell((short) 4).setCellValue("AVERAGE_PRICE");
            rowhead.createCell((short) 5).setCellValue("PER_DAY_TICKETS");
            rowhead.createCell((short) 6).setCellValue("PER_AGENT_TICKETS");
            PreparedStatement ps=cn.prepareStatement("select * from MONTHLYREPORTS");
            ResultSet rs=ps.executeQuery();
            int i=1;
            while(rs.next()){
            HSSFRow row=   sheet.createRow((short)i);
            row.createCell((short) 0).setCellValue(rs.getString(1));
            row.createCell((short) 1).setCellValue(rs.getInt(2));
            row.createCell((short) 2).setCellValue(rs.getInt(3));
            row.createCell((short) 3).setCellValue(rs.getInt(4));
            row.createCell((short) 4).setCellValue(rs.getInt(5));
            row.createCell((short) 5).setCellValue(rs.getInt(6));
            row.createCell((short) 6).setCellValue(rs.getInt(7));
            i++;
            }    


            FileOutputStream fileOut =  new FileOutputStream( filename);
            hwb.write(fileOut);
            fileOut.close();

我不想将文件另存为

FileOutputStream fileOut =  new FileOutputStream( filename);
                hwb.write(fileOut);

我想自动打开文件并要求用户保存在自己的路径中。 如何实现它,请帮助我。

0 个答案:

没有答案