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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值