结合自己做的项目,下面是我做的项目的导出Excel的配置:
需要下载导入poi包,自己动手谷歌一下
就以导出员工信息为例
//在com.demo.utils下建立了ExportEmployeeExcel.java
//有需要的同学可以把Employee部分信息修改即可
package com.demo.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
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.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.HSSFColor;
import com.demo.entity.Employee;
public class ExportEmployeeExcel<T>{
@SuppressWarnings("deprecation")
public void exportExcel( String[] headers,
Collection<Employee> dataset, OutputStream out) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet();
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.WHITE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.RED.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.WHITE.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
Iterator<Employee> it = dataset.iterator();
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
List<List<String>> listRow=new ArrayList<List<String>>();
while (it.hasNext()) {
Employee employee=(Employee)it.next();
List<String> listHol=new ArrayList<String>();
//输出到Excel的行
//有需要的将这部分改成自己需要输出的即可
listHol.add(employee.getEmpId()+"");
listHol.add(employee.getEmpName()+"");
String Sex = "0";
if ("0".equals(employee.getSex())) {
Sex="男";
}else if ("1".equals(employee.getSex())) {
Sex="女";
}
listHol.add(Sex+"");
listHol.add(employee.getBirthDay());
listHol.add(employee.getEmpDesc()+"");
listHol.add(employee.getEmpRemark()+"");
listHol.add(employee.getDept().getDeptName()+"");
listHol.add(employee.getEducation().getEduName()+"");
listHol.add(employee.getRolers().getRolerName()+"");
listRow.add(listHol);
}
//以上是需要修改的部分
int index = 1;
for (List<String> list : listRow) {
row = sheet.createRow(index);
short i=0;
for (String string : list) {
//HSSFCell cell0 = row.createCell(i);
HSSFCell cell0=row.createCell(i);
cell0.setCellStyle(style2);
HSSFRichTextString richString = new HSSFRichTextString(string);
richString.applyFont(font3);
cell0.setCellValue(richString);
i++;
}
index++;
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}下面是调用servlet实现部分:
//com.demo.servlet下的主要实现代码
if ("exportEmp".equals(method)) {
resp.addHeader("Content-Disposition", "attachment;filename=Employee.xls");
List<Employee> list=empService.findAllInfo();//全查
ExportEmployeeExcel<Employee> ex=new ExportEmployeeExcel<Employee>();
String[] headers={"员工编号","员工姓名","性别","出生日期","描述","备注","所在部门","学历","角色"};
try {
OutputStream out=resp.getOutputStream();
ex.exportExcel(headers,list, out);
out.close();
} catch (IOException e){
e.printStackTrace();
}
}dao层和service层代码就忽略了
如果还不明白的联系我
本文出自 “可爱的xiaopp” 博客,请务必保留此出处http://smartxiaopp.blog.51cto.com/9349053/1548953
原文:http://smartxiaopp.blog.51cto.com/9349053/1548953