2016-11-30 90 views
1

我期待數據以Excel格式導出。我在將數據寫入Excel文件時遇到問題,並且在下載它時未使用Excel格式。如何將數據從struts2導出到Excel

在下面的第一種方法中,我從數據庫中獲取數據,並將其作爲列表發送給期望文件名的第二種方法。在第二種方法中,我試圖將數據庫數據寫入Excel文件。

我已經完成了導出爲PDF的編碼。它得到正確下載,但在URL欄downloadTeacherListINExcel.action。我已經給出了這樣的文件路徑:String fileName = "f:\\teachersList.pdf"。當它在瀏覽器中下載時,文件名會像f-teachersList一樣。爲Excel

@Override 
    public String exportInExcel(List<TeacherDTO> listOfTeachers) { 
     String fileName = "f:\\test\\teachersList.xls"; 
     try { 

      HSSFWorkbook workbook = new HSSFWorkbook(); 
      HSSFSheet sheet = workbook.createSheet("Products List"); 

      // create heading 
      Row rowHeading = sheet.createRow(0); 

      rowHeading.createCell(0).setCellValue("Id"); 
      rowHeading.createCell(1).setCellValue("Name"); 

      for (int i = 0; i < 6; i++) { 

       CellStyle stylerowHeading = workbook.createCellStyle(); 

       Font font = workbook.createFont(); 
       font.setBoldweight(Font.BOLDWEIGHT_BOLD); 
       font.setFontName(HSSFFont.FONT_ARIAL); 
       font.setFontHeightInPoints((short) 11); 

       stylerowHeading.setFont(font); 
       stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER); 

       rowHeading.getCell(i).setCellStyle(stylerowHeading); 
      } 

      int r = 1; 
      for (TeacherDTO teacher : listOfTeachers) { 
       Row row = sheet.createRow(r); 

       // Id column 
       Cell cellID = row.createCell(0); 
       cellID.setCellValue(teacher.getTeacherFirstName());// (run after 
                    // this line 
                    // once) 

       // name column 
       Cell cellName = row.createCell(1); 
       cellName.setCellValue(teacher.getTeacherEmailID());// (run after 
                    // this line 
                    // once) 

       r++; 
      } 

      // Auto fit 
      for (int i = 0; i < 6; i++) { 
       sheet.autoSizeColumn(i); 
      } 

      FileOutputStream fout = new FileOutputStream(new File(fileName)); 
      workbook.write(fout); 
      fout.close(); 

      System.out.println("Excel Written Success"); 

     } catch (Exception e) { 
     System.out.println("%%%%%%%%%%%%%%%%%%"+e.getMessage()); 
     } 
     return fileName; 
    } 

的Struts配置

<action name="downloadTeacherListINExcel" class="com.pradeep.sms.controller.report.StaffReportAction" method="exportInExcel"> 
      <result name="success" type="stream"> 

       <param name="contentType">application/vnd.ms-excel</param> 
       <param name="inputName">fileInputStream</param> 
       <param name="contentDisposition">attachment;filename=${fileName}</param> 
       <param name="bufferSize">1024</param> 
     </result> 
    </action> 

錯誤

Hibernate: select 
[email protected] 
%%%%%%%%%%%%%%%%%%null 
f:\test\teachersList.xls 
java.io.FileNotFoundException: f:\test\teachersList.xls (The system cannot find the file specified) 
0書面

public String exportInExcel() { 

     // getting data from data base 
     listOfTeachers = reportService.getlistOfTeachers(); 
     for (TeacherDTO teacherDTO : listOfTeachers) { 
      System.out.println(teacherDTO.getTeacherEmailID()); 
     } 

     // sending a list data export in excel 
     String excelFileName = reportService.exportInExcel(listOfTeachers); 
     System.out.println(excelFileName); 
     try { 
      fileInputStream = new FileInputStream(excelFileName); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("Execl Download Method"); 
     return SUCCESS; 
    } 

代碼

+0

你爲什麼要寫入文件,而不僅僅是直接到ST從struts令人沮喪? – Gagravarr

+0

告訴我一次如何做到這一點 –

回答

1

在動作類

public String exportInExcel() { 

       //getting List of teachers 
       listOfTeachers = reportServiceExcel.getlistOfTeachers(); 

       // sending list data to write in excel sheet 
       HSSFWorkbook workbook = reportServiceExcel.exportInExcel(listOfTeachers); 

       // code to download 
       try { 
        ByteArrayOutputStream boas = new ByteArrayOutputStream(); 
        workbook.write(boas); 
        setInputStream(new ByteArrayInputStream(boas.toByteArray())); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       return SUCCESS; 
      } 

的Excel類

public HSSFWorkbook exportInExcel(List<TeacherDTO> listOfTeachers) { 

      HSSFWorkbook workbook = null; 
      try { 
       workbook = new HSSFWorkbook(); 
       HSSFSheet sheet = workbook.createSheet("Products List"); 

       // create heading 
       Row rowHeading = sheet.createRow(0); 

       rowHeading.createCell(0).setCellValue("Name"); 
       rowHeading.createCell(1).setCellValue("Mobile Number"); 
       rowHeading.createCell(2).setCellValue("Email ID"); 
       rowHeading.createCell(3).setCellValue("Designation"); 

       for (int i = 0; i < 4; i++) { 
        CellStyle stylerowHeading = workbook.createCellStyle(); 
        Font font = workbook.createFont(); 
        font.setBoldweight(Font.BOLDWEIGHT_BOLD); 
        font.setFontName(HSSFFont.FONT_ARIAL); 
        font.setFontHeightInPoints((short) 11); 
        stylerowHeading.setFont(font); 
        stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER); 
        rowHeading.getCell(i).setCellStyle(stylerowHeading); 
       } 

       int r = 1; 
       for (TeacherDTO t : listOfTeachers) { 

        String teacherName = t.getTeacherFirstName() + "" + t.getTeacherMiddleName() + "" 
          + t.getTeacherLastName(); 
        Row row = sheet.createRow(r); 

        // Name column 
        Cell cellName = row.createCell(0); 
        cellName.setCellValue(teacherName);// (run after this line once) 

        // Mobile Number column 
        Cell cellMobileNumber = row.createCell(1); 
        cellMobileNumber.setCellValue(t.getTeacherMobileNumber()); 

        // Email column 
        Cell cellEmail = row.createCell(2); 
        cellEmail.setCellValue(t.getTeacherEmailID()); 

        // Designation column 
        Cell cellDesignation = row.createCell(3); 
        cellDesignation.setCellValue(t.getTeacherDesignation()); 

        r++; 
       } 

       // Auto fit columns in excel sheet 
       for (int i = 0; i < 4; i++) { 
        sheet.autoSizeColumn(i); 
       } 


       System.out.println("Excel Written Success"); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return workbook; 
     } 

Struts配置

<action name="downloadTeacherListExcel" class="com.pradeep.sms.controller.report.StaffReportAction" method="exportInExcel"> 
    <result name="success" type="stream">   
     <param name="contentType">application/vnd.ms-excel</param> 
     <param name="inputName">inputStream</param> 
     <param name="contentDisposition">attachment;filename="teachersList.xls"</param> 
     <param name="bufferSize">4096</param> 
    </result> 
</action>