Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Problem with exporting csv file using template

    Java Code:
    public ActionForward createReport(ActionMapping mapping
                            , ActionForm form
                            , HttpServletRequest request
                            , HttpServletResponse response) {
                    ((MenuForm)request.getSession().getAttribute("MenuForm")).setAction(Constants.ACTION_REPORT);
                    ReportForm reportForm = (ReportForm)form;
                    String year = reportForm.getYear();
                    String month = reportForm.getMonth();
                    try{
                            ServletContext context = this.getServlet().getServletContext();
                            String path = context.getRealPath("/WEB-INF/classes/template.csv");
                            FileInputStream fileInputStream = new FileInputStream(path);
                            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);                      
                            reportBusiness.report(year, month, workbook);
                            response.setContentType("Text/csv");
                            response.setCharacterEncoding("UTF-8");
                            response.setHeader("Content-Disposition", "attachment;filename=report.csv");
                            ServletOutputStream outputStream = response.getOutputStream();
                            workbook.write(outputStream);
                            outputStream.close();
                    }
                    catch (Exception exception){
                            System.out.println(exception.getMessage());
                    }
                   
                    return null;    
            }
    the code above i write to export report.csv base on template.csv format, but when i run the web, it always export report.xls, although i changed the file name already, so can anyone take a look of it and help me toa resolve this problem

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I think it is because the HSSFWorkbook wants to write out an .xls file.

    You would need to instead have the .xls contents converted to .csv and have that written out, instead of the workbook directly writing to the output stream. see also: How can I convert HSSFWorkbook into a CSV file..? - Stack Overflow

Similar Threads

  1. Exporting to a executable JAR file
    By Drun in forum Eclipse
    Replies: 4
    Last Post: 03-27-2010, 04:16 PM
  2. Exporting WAR File Issue
    By bsimic in forum Eclipse
    Replies: 0
    Last Post: 12-29-2009, 04:23 PM
  3. Exporting jar problem
    By cocorota in forum Eclipse
    Replies: 0
    Last Post: 11-20-2009, 06:08 PM
  4. Apache Velocity template file and output locations
    By mjwoodford in forum New To Java
    Replies: 1
    Last Post: 10-05-2009, 04:59 PM
  5. Problem when exporting to CSV
    By deepusrp in forum New To Java
    Replies: 10
    Last Post: 06-26-2009, 10:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •