我们使用POI技术来导出和导入我们之前的用户列表。POI在之前的《杰信商贸》工程中已经详细介绍过了,这里就不赘述。我们直接上手开发。
首先说下一我们的POI打印的流程,我们接下来就按照这个流程开发:
1、创建工作簿
1.1、创建合并单元格对象
1.2、头标题样式
1.3、列标题样式
2、创建工作表
2.1、加载合并单元格对象
3、创建行
3.1、创建头标题行;并且设置头标题
3.2、创建列标题行;并且设置列标题
4、操作单元格;将用户列表写入excel
5、输出
可以回顾一下我们之前的用户列表:
可以看到这里有“导入”和“导出”的按钮,可以对列表的数据进行excel文本的导入和导出工作。
我们打印的最终效果:
首先我们要先导入POI的jar包:
poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
我们在列表的界面的“导出”按钮部分添加点击事件的js代码,是我们能够点击跳转发到相应的action去执行打印excel表的操作:
我们在UserAction类中添加exportExcel方法来打印数据:
userService.exportExcel(userList,outputStream);这个方法我们还没有实现,所以我们在userService接口中定义这个方法:
/
然后在userServiceImpl类中去实现这个方法:
下面进行测试:
点击导出
然后导出对话框,点击下载:
得到文件,打开之后发现就是我们所要的结果:
至此,我们的用户列表导入打印成功!
这里要注意的是,我们因为打印这一项功能就占据了Service的大部分空间,使得Service方法过于臃肿,所以我们要将打印方法抽取出来,放在一个专门提供打印服务的类中,所以我们专门编写一个工具类:
然后修改Service:
再次测试没有问题,我们方法抽出成功。
首先说下一我们的POI打印的流程,我们接下来就按照这个流程开发:
1、创建工作簿
1.1、创建合并单元格对象
1.2、头标题样式
1.3、列标题样式
2、创建工作表
2.1、加载合并单元格对象
3、创建行
3.1、创建头标题行;并且设置头标题
3.2、创建列标题行;并且设置列标题
4、操作单元格;将用户列表写入excel
5、输出
可以回顾一下我们之前的用户列表:
可以看到这里有“导入”和“导出”的按钮,可以对列表的数据进行excel文本的导入和导出工作。
我们打印的最终效果:
首先我们要先导入POI的jar包:
poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
我们在列表的界面的“导出”按钮部分添加点击事件的js代码,是我们能够点击跳转发到相应的action去执行打印excel表的操作:
//导出列表
function doExportExcel(){
window.open("${basePath}tax/user_exportExcel.action");
}
我们在UserAction类中添加exportExcel方法来打印数据:
//导出列表
public void exportExcel(){
try {
//1.查找用户列表
userList=userService.findObjects();
//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();
userService.exportExcel(userList,outputStream);
if(outputStream !=null){
outputStream.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
userService.exportExcel(userList,outputStream);这个方法我们还没有实现,所以我们在userService接口中定义这个方法:
/
/导出列表
public void exportExcel(List<User> userList,ServletOutputStream outputStream);
然后在userServiceImpl类中去实现这个方法:
@Override
public void exportExcel(List<User> userList,
ServletOutputStream outputStream) {
try {
//1.创建工作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//1.1创建合并单元格对象
CellRangeAddress cellRangeAddress=new CellRangeAddress(0,0,0,4);//起始行号,结束行号,起始列号,结束列号
//1.2、头标题样式
HSSFCellStyle style1=creatCellStyle(workbook,(short)16);
//1.3、列标题样式
HSSFCellStyle style2=creatCellStyle(workbook,(short)13);
//2、创建工作表
HSSFSheet sheet=workbook.createSheet("用户列表");
//2.1、加载合并单元格对象
sheet.addMergedRegion(cellRangeAddress);
//设置默认列宽
sheet.setDefaultColumnWidth(20);
//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={"用户名","账号","所属部门","性别","电子邮箱"};
for (int i = 0; i < titles.length; i++) {
HSSFCell cell2=row2.createCell(i);
//加载单元格样式
cell2.setCellStyle(style2);
cell2.setCellValue(titles[i]);
}
//4、操作单元格,将用户写入Execl
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).getName());
HSSFCell cell12=row.createCell(1);
cell12.setCellValue(userList.get(j).getAccount());
HSSFCell cell13=row.createCell(2);
cell13.setCellValue(userList.get(j).getDept());
HSSFCell cell14=row.createCell(3);
cell14.setCellValue(userList.get(j).isGender()?"男":"女");
HSSFCell cell15=row.createCell(4);
cell15.setCellValue(userList.get(j).getEmail());
}
}
//5、输出
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建单元格样式
* @param workbook 工作簿
* @param fontSize 字体大小
* @return 单元格样式
* */
private HSSFCellStyle creatCellStyle(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;
}
下面进行测试:
点击导出
然后导出对话框,点击下载:
得到文件,打开之后发现就是我们所要的结果:
至此,我们的用户列表导入打印成功!
这里要注意的是,我们因为打印这一项功能就占据了Service的大部分空间,使得Service方法过于臃肿,所以我们要将打印方法抽取出来,放在一个专门提供打印服务的类中,所以我们专门编写一个工具类:
package cn.edu.hpu.tax.core.util;
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.hssf.util.CellRangeAddress;
import cn.edu.hpu.tax.user.entity.User;
public class ExcelUtil {
/**
* 导出用户的所有列表到Execl
* @param userList 用户列表
* @param outputStream 输出流
* */
public static void exportExcel(List<User> userList,
ServletOutputStream outputStream) {
try {
//1.创建工作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//1.1创建合并单元格对象
CellRangeAddress cellRangeAddress=new CellRangeAddress(0,0,0,4);//起始行号,结束行号,起始列号,结束列号
//1.2、头标题样式
HSSFCellStyle style1=creatCellStyle(workbook,(short)16);
//1.3、列标题样式
HSSFCellStyle style2=creatCellStyle(workbook,(short)13);
//2、创建工作表
HSSFSheet sheet=workbook.createSheet("用户列表");
//2.1、加载合并单元格对象
sheet.addMergedRegion(cellRangeAddress);
//设置默认列宽
sheet.setDefaultColumnWidth(20);
//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={"用户名","账号","所属部门","性别","电子邮箱"};
for (int i = 0; i < titles.length; i++) {
HSSFCell cell2=row2.createCell(i);
//加载单元格样式
cell2.setCellStyle(style2);
cell2.setCellValue(titles[i]);
}
//4、操作单元格,将用户写入Execl
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).getName());
HSSFCell cell12=row.createCell(1);
cell12.setCellValue(userList.get(j).getAccount());
HSSFCell cell13=row.createCell(2);
cell13.setCellValue(userList.get(j).getDept());
HSSFCell cell14=row.createCell(3);
cell14.setCellValue(userList.get(j).isGender()?"男":"女");
HSSFCell cell15=row.createCell(4);
cell15.setCellValue(userList.get(j).getEmail());
}
}
//5、输出
workbook.write(outputStream);
workbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建单元格样式
* @param workbook 工作簿
* @param fontSize 字体大小
* @return 单元格样式
* */
private static HSSFCellStyle creatCellStyle(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;
}
}
然后修改Service:
@Override
public void exportExcel(List<User> userList,
ServletOutputStream outputStream) {
ExcelUtil.exportExcel(userList, outputStream);
}
再次测试没有问题,我们方法抽出成功。
用户列表导出完成,下一次我们来探讨导入功能。
转载请注明出处:http://blog.csdn.net/acmman/article/details/49382979