如何将数据从struts2导出到Excel

如何将数据从struts2导出到Excel,struts2,apache-poi,Struts2,Apache Poi,我希望数据以Excel格式导出。我在将数据写入Excel文件时遇到问题,下载时发现它不是Excel格式 在下面的第一个方法中,我从数据库中获取数据,并将其作为列表发送给需要文件名的第二个方法。在第二种方法中,我尝试将数据库数据写入Excel文件 我也做了导出到PDF的编码。它被正确下载,但在URL栏downloadtheacherlistinexcel.action中。我给出的文件路径如下:String fileName=“f:\\teachersList.pdf”。在浏览器中下载时,文件名会像

我希望数据以Excel格式导出。我在将数据写入Excel文件时遇到问题,下载时发现它不是Excel格式

在下面的第一个方法中,我从数据库中获取数据,并将其作为列表发送给需要文件名的第二个方法。在第二种方法中,我尝试将数据库数据写入Excel文件

我也做了导出到PDF的编码。它被正确下载,但在URL栏
downloadtheacherlistinexcel.action
中。我给出的文件路径如下:
String fileName=“f:\\teachersList.pdf”
。在浏览器中下载时,文件名会像
f-teachersList
一样出现

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;
    }
为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;
    }

行动类

 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;
        }
public HSSF工作手册导出目录(教师列表){
HSSF工作簿=空;
试一试{
工作簿=新的HSSF工作簿();
HSSFSheet sheet=workbook.createSheet(“产品列表”);
//创建标题
行标题=sheet.createRow(0);
rowHeading.createCell(0.setCellValue(“名称”);
行标题.createCell(1).setCellValue(“手机号码”);
rowHeading.createCell(2.setCellValue(“电子邮件ID”);
行标题.createCell(3).setCellValue(“指定”);
对于(int i=0;i<4;i++){
CellStyleRowHeading=工作簿.createCellStyle();
Font=workbook.createFont();
font.setBoldweight(font.BOLDWEIGHT\u BOLD);
font.setFontName(hssfont.font_ARIAL);
字体设置字体高度输入点((短)11);
stylerowHeading.setFont(字体);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
int r=1;
for(教师到t:教师列表){
String teacherName=t.getTeacherFirstName()+“”+t.getTeacherMiddleName()+“”
+t.getTeacherLastName();
Row Row=sheet.createRow(r);
//名称列
Cell cellName=row.createCell(0);
cellName.setCellValue(teacherName);//(在此行后运行一次)
//手机号码栏
Cell cellMobileNumber=row.createCell(1);
setCellValue(t.getTeacherMobileNumber());
//电子邮件栏
Cell cellEmail=row.createCell(2);
setCellValue(t.getTeacherEmailID());
//名称栏
Cell cellsignation=行。createCell(3);
cellDesignation.setCellValue(t.getTeacherDesignation());
r++;
}
//excel工作表中的自动适配列
对于(int i=0;i<4;i++){
表1.autoSizeColumn(i);
}
System.out.println(“Excel编写成功”);
}捕获(例外e){
e、 printStackTrace();
}
返回工作簿;
}
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>

应用程序/vnd.ms-excel
输入流
附件filename=“teachersList.xls”
4096

为什么要写入文件,而不仅仅是直接从struts写入流?给我演示一下如何做到这一点
 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;
        }
<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>