导出2003版本的excel文件
ExportExcelServlet.java 源代码:
package com.web;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.entity.Student;
public class ExportExcelServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Student stu = new Student();
stu.setName("张三");
stu.setSex("男");
stu.setAge(21);
Student stu1 = new Student();
stu1.setName("李四");
stu1.setSex("女");
stu1.setAge(22);
Student stu2 = new Student();
stu2.setName("王五");
stu2.setSex("男");
stu2.setAge(20);
List<Student> stuList = new ArrayList<>();
stuList.add(stu);
stuList.add(stu1);
stuList.add(stu2);
// excel标题
String[] title = { "名称", "性别", "年龄" };
// excel文件名
String fileName = "学生信息表" + System.currentTimeMillis() + ".xls";
// sheet名
String sheetName = "学生信息表";
HSSFWorkbook book = OutputExcelDemo.expExcel(sheetName, title, stuList);
// 响应到客户端
try {
//this.setResponseHeader(resp, fileName);
//OutputStream os = resp.getOutputStream();
FileOutputStream os = new FileOutputStream("E:/"+fileName);
book.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
OutputExcelDemo.java 源代码:
package com.web;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.entity.Student;
public class OutputExcelDemo {
/**
* 创建excel并填入数据
* @author LiQuanhui
* @date 2017年11月24日 下午5:25:13
* @param head 数据头
* @param body 主体数据
* @return HSSFWorkbook
*/
@SuppressWarnings("deprecation")
public static HSSFWorkbook expExcel(String sheetName,String[] sheetTitle,List<Student> list) {
//创建一个excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个sheet工作表
HSSFSheet sheet = workbook.createSheet(sheetName);//传参
//创建第0行表头,再在这行里在创建单元格,并赋值
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = null;
//String[] title = new String[]{"名字","性别","年龄"};//传参
for (short i = 0; i < sheetTitle.length; i++) {
cell = row.createCell(i);
cell.setCellValue(sheetTitle[i]);//设置值
}
//将主体数据填入Excel中
for (int i = 0, isize = list.size(); i < isize; i++) {
row = sheet.createRow(i + 1);
Student stuInfo = list.get(i);
cell = row.createCell((short)0);
cell.setCellValue(stuInfo.getName());
cell = row.createCell((short)1);
cell.setCellValue(stuInfo.getSex());
cell = row.createCell((short)2);
cell.setCellValue(stuInfo.getAge());
}
return workbook;
}
}
导出excel文件时注意点:
1、导入jar包: poi-3.8.jar、poi-ooxml-3.8.jar
2、poi-3.8.jar必须放在WEB-INF/lib下,否则报错:
Java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
注:此文件是导出2003版本的excel文件,如果要导出2007需要有几个地方变动:
XSSFWorkbook
XSSFSheet
XSSFRow
HSSFCell
HSSFCellStyle