1. 首先創建一個DTO類來存儲要導出的數據
public class People {
private String name;//名字
private String age;//年齡
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public People(String name, String age) {
super();
this.name = name;
this.age = age;
}
public People() {
super();
}
}
2.創建一個servlet,導出excel
代碼如下:
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
/**
* 簡單的導出excel_demo
*
*/
@WebServlet("/downExcel")
public class downExcel extends HttpServlet {
private static final long serialVersionUID = 1L;
public downExcel() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 備註:HSSFWorkbook對象無法導出大批量的數據智能導出65535條(xls excel2003), poi3.7版本後支持大批量數據導出xlsx(excel2007)
*
* 但需要導入dom4j-1.6.1.jar 包(解析XML文件的包)
* 使用方法基本一致,只是SXSSFWorkbook 對象在創建的過程中可以定義內存中的數據量,
* 在運行時會把一部分數據寫入到磁盤中,減少內存的壓力
* 結束後需要將臨時工作簿備份到磁盤上wb.dispose();
*/
//downExcel2003(request,response);
downExcel2007(request,response);
}
/**
* 舊版excel導出
* @param request
* @param response
* @throws IOException
*/
public void downExcel2003(HttpServletRequest request, HttpServletResponse response) {
// 1.導入Excel需要的jar包,Maven在pom.xml 中添加,本案例見lib下
/**
* <!-- excel文件生成 --> <dependency> <groupId>org.apache.poi</groupId>
* <artifactId>poi</artifactId> <version>3.9</version> </dependency>
*
* <dependency> <groupId>org.apache.poi</groupId>
* <artifactId>poi-ooxml</artifactId>
* <version>3.9</version> </dependency>
* <dependency> <groupId>org.apache.poi</groupId>
* <artifactId>poi-scratchpad</artifactId>
* <version>3.9</version> </dependency>
* <dependency> <groupId>org.apache.poi</groupId>
* <artifactId>poi-ooxml-schemas</artifactId>
* <version>3.7</version> </dependency>
* <dependency> <groupId>org.apache.xmlbeans</groupId>
* <artifactId>xmlbeans</artifactId>
* <version>2.3.0</version> </dependency>
*/
// 2.創建一筆要導出的數據
List<People> list = new ArrayList<People>();
People p1 = new People("張三", "13");
People p2 = new People("李四", "24");
list.add(p1);
list.add(p2);
// 3.創建Excel 生成的類,即導入jar包中的類
// 3-1.創建excel 必須的對象
HSSFWorkbook wb = new HSSFWorkbook();
// 3-2.創建樣式對象
HSSFCellStyle style = wb.createCellStyle();
// 3-3.創建文件內容頁面名稱
HSSFSheet sheet = wb.createSheet("人員信息頁");
// 3-4.設置居中樣式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 3-5.開始填充內容,創建第一行 即文件頭標題的內容
HSSFRow row = sheet.createRow((int) 0);
// 3-5-1.向第一行,即文件頭標題填充內容
String[] title = new String[] { "姓名", "年齡" };
for (int i = 0; i < title.length; i++) {
// 獲取第一行的第i個格子
HSSFCell cell = row.createCell((short) i);
cell.setCellValue(title[i]);
// 設置每個格子樣式
cell.setCellStyle(style);
}
// 3-5-2.填充第二行以後的內容
for (int i = 0; i < list.size(); i++) {
// 創建一行寫值
row = sheet.createRow((int) i + 1);
row.createCell((short) 0).setCellValue(list.get(i).getName());
row.createCell((short) 1).setCellValue(list.get(i).getAge());
row.createCell((short) 0).setCellStyle(style);
row.createCell((short) 1).setCellStyle(style);
}
// 3-6.數據填充完畢後,導出
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + "demo.xls");
// 開始關閉流
OutputStream ouputStream = null;
try {
ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 新版excel導出
* @param request
* @param response
*/
private void downExcel2007(HttpServletRequest request, HttpServletResponse response) {
//1.再導入dom4j-1.6.1.jar包
// 2.創建一筆要導出的數據
List<People> list = new ArrayList<People>();
People p1 = new People("張三", "13");
People p2 = new People("李四", "24");
list.add(p1);
list.add(p2);
// 3.創建Excel 生成的類,即導入jar包中的類
// 3-1.創建excel 必須的對象 -- 內存中保留100條數據,其餘寫入磁盤
SXSSFWorkbook wb = new SXSSFWorkbook(100);
// 3-2.創建樣式對象
CellStyle style = wb.createCellStyle();
// 3-3.創建文件內容頁面名稱
Sheet sheet = wb.createSheet("人員信息頁");
// 3-4.設置居中樣式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 3-5.開始填充內容,創建第一行 即文件頭標題的內容
Row row = sheet.createRow((int) 0);
// 3-5-1.向第一行,即文件頭標題填充內容
String[] title = new String[] { "姓名", "年齡" };
for (int i = 0; i < title.length; i++) {
// 獲取第一行的第i個格子
Cell cell = row.createCell((short) i);
cell.setCellValue(title[i]);
// 設置每個格子樣式
cell.setCellStyle(style);
}
// 3-5-2.填充第二行以後的內容
for (int i = 0; i < list.size(); i++) {
// 創建一行寫值
row = sheet.createRow((int) i + 1);
row.createCell((short) 0).setCellValue(list.get(i).getName());
row.createCell((short) 1).setCellValue(list.get(i).getAge());
row.createCell((short) 0).setCellStyle(style);
row.createCell((short) 1).setCellStyle(style);
}
// 3-6.數據填充完畢後,導出
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + "demo.xls");
// 開始關閉流
OutputStream ouputStream = null;
try {
ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
// 3-7.將臨時工作簿備份到磁盤上
wb.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}