Strust2 + POI export exel table and solve the Chinese garbled file name / no show

  1. Download and import the project [ poi.3.17.jar ]

  2. strust.xml

    <action name="returnLate_*" class="com.stureturnlate.moudels.biz.action.return_late.ReturnLateAction" method="{1}">
                <result name="list">/page/moudels/biz/return_late/returnLateList.jsp</result>
                <result name="openAdd">/page/moudels/biz/return_late/returnLateAdd.jsp</result>
                <!--<result name="openEdit">/page/biz/student/studentUpdate.jsp</result>-->
                <result name="openDetails">/page/moudels/biz/return_late/returnLateDetails.jsp</result>
    
                <!-- 导出Excel -->
                <result name="download" type="stream">
                    <param name="contentType">application/vnd.ms-excel</param><!-- 注意这里的ContentType -->
                    <param name="contentDisposition">attachment;filename="${fileName}.xls"</param><!-- 下载文件的名字 -->
                    <param name="bufferSize"1048576</ param> <-! Download file size1024>1M = -> 
                    <param name = " inputName " > ExcelFile </ param> <-! Here and Action in need of the same variable name -> 
                </ the Result> 
            </ Action>

     

     

  3. ReturnLateAction.class

        / ** 
         * export 
         * @return 
         * / 
        public String downloads () throws Exception { 
    
            List <ReturnLateEntity> = returnLateEntityList new new the ArrayList <> (); 
            fileName = fileName + DateTool.dateFormat ( new new a Date ());
             fileName = new new String (fileName .getBytes ( "GB2312"), "ISO8859-1" ) ; // prevent Chinese garbled or not displayed
     // TODO the first step: a statement of the document object excel 
            HSSFWorkbook returnLateExcel;
             the try {
                 // result of the query, insert padding Excel data
                String [] = IDS returnLateIds.split ( "," ); 
                returnLateEntityList = returnLateService.selectAllReturnLate (IDS); 
    
                // TODO Step Two: Get excel document object 
                returnLateExcel = CreateExcel.createReturnLateExcel (returnLateEntityList); 
    
                ByteArrayOutputStream the Output = new new ByteArrayOutputStream ( ); // ByteArrayOutputStream 
    
                // the TODO step: write byte array output stream 
                returnLateExcel.write (output);
                 byte [] BA = output.toByteArray (); 
    
                // the TODO fourth step: input assignment stream object 
                ExcelFile = new new A ByteArrayInputStream (BA);
    
                output.flush (); 
                output.close (); // Close 
            } the catch (SQLException E) { 
                System.out.println ( "write back late log file or create content Fail Fail!" ); 
                e.printStackTrace (); 
            } the catch (IOException E) { 
                e.printStackTrace (); 
            } 
    
            return "downloads" ; 
        }

     

  4. File Export class CreateExcel.class

    package com.stureturnlate.common;
    
    import com.stureturnlate.moudels.vo.*;
    import org.apache.poi.hssf.usermodel.*;
    import org.apache.poi.ss.usermodel.HorizontalAlignment;
    
    import java.sql.SQLException;
    import java.util.List;
    
    /**
     * @author soldier
     * @title: CreateExcel
     * @projectName stu_return_late
     * @description: 文件导出Excel
     * @date 19-6-10下午9:56
     */
    public class CreateExcel {
    
        public static HSSFWorkbook createReturnLateExcel(List<ReturnLateEntity> returnLateEntityList) throws SQLException {
             // create an Excel file 
            HSSFWorkbook Workbook = new new HSSFWorkbook (); // Excel document object
             // create a worksheet 
            HSSFSheet sheet = workbook.createSheet ( "Students coming home record" );
             // add a header row 
            HSSFRow = sheet.createRow HSSFRow (0 );
             // Format cells centered 
            HSSFCellStyle CellStyle = workbook.createCellStyle (); 
            cellStyle.setAlignment (HorizontalAlignment.CENTER); 
    
            int index = 0 ;
             // add header content 
            HSSFCell headCell = hssfRow .createCell (index ++ );
            headCell.setCellValue ( "coming home number" ); 
            headCell.setCellStyle (CellStyle); 
    
            headCell = hssfRow.createCell (index ++ ); 
            headCell.setCellValue ( "Students coming home" ); 
            headCell.setCellStyle (CellStyle); 
    
            headCell = hssfRow.createCell (index ++ ); 
            headCell.setCellValue ( "belongs to School" ); 
            headCell.setCellStyle (CellStyle); 
    
            headCell = hssfRow.createCell (index ++ ); 
            headCell.setCellValue ( "the student's dormitory" ); 
            headCell.setCellStyle (CellStyle); 
    
            headCellHssfRow.createCell = (index ++ ); 
            headCell.setCellValue ( "coming home time" ); 
            headCell.setCellStyle (CellStyle); 
    
            headCell = hssfRow.createCell (index ++ ); 
            headCell.setCellValue ( "Cause coming home" ); 
            headCell.setCellStyle ( CellStyle); 
    
            headCell = hssfRow.createCell (index ++ ); 
            headCell.setCellValue ( "recorder work number" ); 
            headCell.setCellStyle (CellStyle); 
    
            headCell = hssfRow.createCell (index); 
            headCell.setCellValue ( "record name" ) ; 
            headCell.setCellStyle (CellStyle);
    
            // 添加数据内容
            for (int i = 0; i < returnLateEntityList.size(); i++) {
                hssfRow = sheet.createRow((int) i + 1);
                ReturnLateEntity returnLateEntity = returnLateEntityList.get(i);
    
                index = 0;
                // 创建单元格,并设置值
                HSSFCell cell = hssfRow.createCell(index++);
                cell.setCellValue(returnLateEntity.getReturnLateId());
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index++);
                StudentEntity studentEntity = new StudentEntity();//获取学生名称
                cell.setCellValue(studentEntity.getStudentName());
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index++);
                CollegeEntity collegeEntity = new CollegeEntity();//获取学院
                cell.setCellValue(collegeEntity.getCollegeName());
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index++);
                HostelEntity hostelEntity = new HostelEntity();//获取宿舍名称
                cell.setCellValue(hostelEntity.getHostelName());
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index++);
                cell.setCellValue(DateTool.dateTimeFormat(returnLateEntity.getReturnLateTime()));
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index++);
                cell.setCellValue(returnLateEntity.getReturnLateCause());
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index++);
                DormMasterEntity dormMasterEntity = new DormMasterEntity();
                cell.setCellValue(dormMasterEntity.getDormMasterId());
                cell.setCellStyle(cellStyle);
    
                cell = hssfRow.createCell(index);
                cell.setCellValue(dormMasterEntity.getDormMasterName());
                cell.setCellStyle(cellStyle);
            }
    
            try {
                return workbook;
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

     

Guess you like

Origin www.cnblogs.com/HuangJie-sol/p/11001923.html