ssh 实现导出excel

                                                                                                   SSH实现表格导出

首先在 com.neu.bean 创建一个实体 ExcelUtil

import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.ss.util.CellRangeAddress;

import com.neu.entity.CustoMer;


public class ExcelUtil {

    /**
     * 导出用户的所有列表到excel
     * @param userList 用户列表
     * @param outputStream 输出流
     */
    public static void exportUserExcel(List<CustoMer> userList, ServletOutputStream outputStream) {
        try {
            //1、创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            //1.1、创建合并单元格对象
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 4);//起始行号,结束行号,起始列号,结束列号
            
            //1.2、头标题样式
            HSSFCellStyle style1 = createCellStyle(workbook, (short)16);
            
            //1.3、列标题样式
            HSSFCellStyle style2 = createCellStyle(workbook, (short)13);
            
            //2、创建工作表
            HSSFSheet sheet = workbook.createSheet("用户列表");
            //2.1、加载合并单元格对象
            sheet.addMergedRegion(cellRangeAddress);
            //设置默认列宽
            sheet.setDefaultColumnWidth(25);
            
            //3、创建行
            //3.1、创建头标题行;并且设置头标题
            HSSFRow row1 = sheet.createRow(0);
            HSSFCell cell1 = row1.createCell(0);
            //加载单元格样式
            cell1.setCellStyle(style1);
            cell1.setCellValue("用户列表");
            
            //3.2、创建列标题行;并且设置列标题
            HSSFRow row2 = sheet.createRow(1);
            String[] titles = {"ID","姓名", "昵称", "邮箱", "出生日期","住址","工作单位","邮政编码","客户类型"};
            for(int i = 0; i < titles.length; i++){
                HSSFCell cell2 = row2.createCell(i);
                //加载单元格样式
                cell2.setCellStyle(style2);
                cell2.setCellValue(titles[i]);
            }
            
            //4、操作单元格;将用户列表写入excel
            if(userList != null){
                for(int j = 0; j < userList.size(); j++){
                    HSSFRow row = sheet.createRow(j+2);
                    HSSFCell cell11 = row.createCell(0);
                    cell11.setCellValue(userList.get(j).getCustomerId());
                    HSSFCell cell12 = row.createCell(1);
                    cell12.setCellValue(userList.get(j).getCustomerName());
                    HSSFCell cell13 = row.createCell(2);
                    cell13.setCellValue(userList.get(j).getCustomerNick());
                    HSSFCell cell14 = row.createCell(3);
                    cell14.setCellValue(userList.get(j).getEmail());
                    HSSFCell cell15 = row.createCell(4);
                    cell15.setCellValue(userList.get(j).getCustomerData());
                    HSSFCell cell16 = row.createCell(5);
                    cell16.setCellValue(userList.get(j).getCustomerAddress());
                    HSSFCell cell17 = row.createCell(6);
                    cell17.setCellValue(userList.get(j).getWorkUnit());
                    HSSFCell cell18 = row.createCell(7);
                    cell18.setCellValue(userList.get(j).getPostcode());
                    HSSFCell cell19 = row.createCell(8);
                    cell19.setCellValue(userList.get(j).getCustomer());
                }
        
            }
            //5、输出
            workbook.write(outputStream);
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建单元格样式
     * @param workbook 工作簿
     * @param fontSize 字体大小
     * @return 单元格样式
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) {
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
        //创建字体
        HSSFFont font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体
        font.setFontHeightInPoints(fontSize);
        //加载字体
        style.setFont(font);
        return style;
    }

}

                               dao层

public void add(List<CustoMer> student);

public List findByHql(CustoMer customer) ;

                      daoImpl层

 /*
      * 查询全部
      */ public List findByHql(CustoMer customer) {
        String hql = "from CustoMer where 1=1";
        List list = super.getHibernateTemplate().find(hql);
        return list;
        }

@Override
    public void add(List<CustoMer> student) {
        if(student.size() > 0){
            int sNum = student.size();
            for(int i=0;i<sNum;i++){
                hibernateTemplate.saveOrUpdate(student.get(i));
            }
        }

                       server 也是biz

/**
     *  导出excel
     */
    public void exportExcel(List<CustoMer> userList, ServletOutputStream outputStream);

                                         

                                                      serverimpl

@Override
    public void exportExcel(List<CustoMer> userList, ServletOutputStream outputStream) {
            ExcelUtil.exportUserExcel(userList, outputStream);
        }

                               action

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware {
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected Map session;
    @Override
    public void setSession(Map<String, Object> arg0) {
        // TODO Auto-generated method stub
        this.session=arg0;
    }
 }

               actionimpl

//导出用户列表


public class CustoMerAction extends BaseAction {

private CustoMerBiz customerbiz;

private List<CustoMer> userList;       

public void exportExcel(){

            try {
                //1、查找用户列表
                userList = this.customerbiz.dofindByExample(cus);
                //2、导出
                HttpServletResponse response = ServletActionContext.getResponse();
                response.setContentType("application/x-execl");
                response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户列表.xls".getBytes(), "ISO-8859-1"));
                ServletOutputStream outputStream = response.getOutputStream();
                customerbiz.exportExcel(userList, outputStream);
                if(outputStream != null){
                    outputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

 最后前台页面

 <script type="text/javascript">
       //全选、全反选
        function doSelectAll(){
            // jquery 1.6 前
            //$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked"));
            //prop jquery 1.6+建议使用
            $("input[name=selectedRow]").prop("checked", $("#selAll").is(":checked"));        
        }
 
    function doExportExcel(){
          window.open("com.neu.adction.impl/cus_exportExcel.action");
      }
    </script>


                    <div>
           <input type="button" value="导出" class="s_button" οnclick="doExportExcel()"/>&nbsp;
                        </div>      
 完成 如果有问题可以加qq:   1158219108



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值