struts2 + poi 数据导出excel

java源码:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BrowserAccessAction extends ActionSupport {
    private ReportService reportService;
    private String year;
    private String month;
    private String filename;
    private InputStream excelStream;
    
    public String export(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String ym = year + month;
        
        HSSFWorkbook workbook = null;
        int rowIndex = 0;
        try {
            workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();
            
            //标题行
            sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 1)); //合并单元格
            HSSFRow titleRow = sheet.createRow(rowIndex);
            HSSFCell titleCell = titleRow.createCell(0);
            titleCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            titleCell.setCellValue(new HSSFRichTextString(year+"年"+month+"月网站浏览器访问量统计"));
            
            //列标题
            HSSFRow labelRow = sheet.createRow(++rowIndex);
            HSSFCell cell0 = labelRow.createCell(0);
            cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell0.setCellValue(new HSSFRichTextString("浏览器"));
            sheet.setColumnWidth(0, 100*50); //列宽
            
            HSSFCell cell1 = labelRow.createCell(1);
            cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell1.setCellValue(new HSSFRichTextString("浏览量(%)"));
            sheet.setColumnWidth(1, 100*50);
            
            //数据行
            List<BrowserAccessBean> list = this.reportService.selectBrowserAccess(ym);
            for(BrowserAccessBean bean : list){
                HSSFRow dataRow = sheet.createRow(++rowIndex);
                
                HSSFCell c0 = dataRow.createCell(0);
                c0.setCellType(HSSFCell.CELL_TYPE_STRING);
                c0.setCellValue(new HSSFRichTextString(bean.getName()));
                
                HSSFCell c1 = dataRow.createCell(1);
                c1.setCellType(HSSFCell.CELL_TYPE_STRING);
                c1.setCellValue(new HSSFRichTextString(String.valueOf(bean.getPerc())));
            }
            
            filename = ReportUtil.createFileName() + ".xls";
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            workbook.write(out);
            out.flush();
            
            byte[] aa = out.toByteArray();
            excelStream = new ByteArrayInputStream(aa, 0, aa.length);
            out.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(workbook!=null){
                    workbook = null;
                }
            } catch (Exception e) {   
                e.printStackTrace();   
            }  
        }
        
        return "excel";
    }
    
    public InputStream getExcelStream(){
        return this.excelStream;
    }
    
    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

	......
}

 

2、struts配置

<action name="export" class="browserAccessAction" method="export">  
	<result name="excel" type="stream">  
		<param name="contentType">application/vnd.ms-excel</param>  
		<param name="contentDisposition">filename="${filename}"</param>  
		<param name="inputName">excelStream</param>  
	</result>  
</action>

 

你可能感兴趣的:(struts2)