05_Excel操作_02_模拟Web环境的User列表导出

【思路解释】

在正式上到WebProject之前,准备模拟一下WebProject后台的导出流程。

主要都写在ExcelService层,在Excel的Service层,首先要获得UserList,即数据库查询的User用户列表,这个UserList从UserDao的getUserList()方法获得,这个getUserList()方法实际没有查询数据库,只是自己创造了一些数据。

ExcelService获得了UserList之后,然后将数据按照一定的格式创建Excel、在Excel中写入数据。

 

【工程截图】

【User.java】

package com.Higgin.Excel.domain;

public class User {
    private int id; 
    private String name;
    private String sex;
    private String phone;
    private String email;
  ...省去get和set方法...... }

【UserDao.java】//模拟从数据库中获得数据

package com.Higgin.Excel.dao;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.Higgin.Excel.domain.User;

public class UserDao {
    /**
     * 模拟从数据库获取的User列表
     * @return
     */
    public List<User> getUserList(){
        List<User> userList=new ArrayList<User>();
        for(int i=1;i<=99;i++){
            User user=new User();
            user.setId(i);
            user.setName("Higgin"+i);
            user.setSex(i%3==0?"男":"女");
            user.setPhone("000000000"+(i*i));
            user.setEmail("myemail"+i+"@qq.com");
            userList.add(user);
        }
        return userList;
    }
    
}

【ExcelService.java】

package com.Higgin.Excel.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
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 org.junit.Test;
import com.Higgin.Excel.dao.UserDao;
import com.Higgin.Excel.domain.User;

public class ExcelService {
    private UserDao userDao=new UserDao();
    
    //模拟从数据库中查询获得对应的User列表
    private List<User> userList=userDao.getUserList();
    
    @Test
    public void outputExcel() throws IOException{
        //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);
        
        //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&&userList.size()>0){
            for(int j=0;j<userList.size();j++){
                HSSFRow row=sheet.createRow(j+2);  //因为前两行分别是头标题和列标题,故要从第3行开始
                row.createCell(0).setCellValue(String.valueOf(userList.get(j).getId()));
                row.createCell(1).setCellValue(userList.get(j).getName());
                row.createCell(2).setCellValue(userList.get(j).getSex());
                row.createCell(3).setCellValue(userList.get(j).getPhone());
                row.createCell(4).setCellValue(userList.get(j).getEmail());
            }
        }
        
        //5.输出
        String fileName="D:\\用户列表.xls";
        FileOutputStream fileOutputStream=new FileOutputStream(fileName);
        workbook.write(fileOutputStream);
        workbook.close();
        fileOutputStream.close();
    }
    
    /**
     * 创建单元格 样式
     * @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;
    }
}

【运行结果】

 

posted @ 2016-08-18 17:06  HigginCui  阅读(366)  评论(0编辑  收藏  举报