目前使用過兩種導出excel的方式,一種是如題所示的使用POI的方式進行數據的導出,這種方式一般只有在處理比較多的數據或者說需要導出的excel表格中有圖片之類的需要特殊處理的文件的時候使用;還有一種方式是直接在HTML文件中加上.xls的后綴,直接導出excel,一般比較適用於數據較少且沒有特殊文件的情況下;再其次就是各種插件,不過適用插件進行導出的時候導出的圖片一般無法展示;
使用POI導出excel:
package com.controller;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.pojo.Person;
@Controller
@RequestMapping("/exportController")
public class ExportController {
@RequestMapping("/export")
public void export(HttpServletResponse response){
//適用List集合造一些數據作為要導出的數據
List<Person> list = new ArrayList<Person>();
list.add(new Person("亞索", "男", "800"));
list.add(new Person("亞索1", "男", "801"));
list.add(new Person("亞索2", "男", "802"));
//調用方法創建HSSFWorkbook工作簿對象
HSSFWorkbook wb = ExportController.export_text(list);
try {
//定義導出文件的名稱,看不懂的同學可以先行了解一下文件下載
String fileName = new String("personRelation.xls".getBytes("UTF-8"),"ISO-8859-1");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename="+fileName);
OutputStream os = response.getOutputStream();
//將工作薄寫入到輸出流中
wb.write(os);
os.close();
} catch (Exception e) {
e.getStackTrace();
}
}
//創建HSSFWorkbook工作薄對象
public static HSSFWorkbook export_text(List<Person> list){
try {
//創建工作薄對象
HSSFWorkbook wb = new HSSFWorkbook();
//創建標題行樣式
HSSFCellStyle headStyle = headStyle(wb);
//創建內容行樣式
HSSFCellStyle contentStyle = contentStyle(wb);
//創建表
HSSFSheet sheet_1 = wb.createSheet("人員信息");
//設置表的默認列寬
sheet_1.setDefaultColumnWidth(30);
//創建標題行
HSSFRow headRow = sheet_1.createRow(0);
HSSFCell head_cell_1 = headRow.createCell(0); //創建標題行第一列
head_cell_1.setCellValue("姓名"); //第一列內容
head_cell_1.setCellStyle(headStyle); //將標題行樣式添加
HSSFCell head_cell_2 = headRow.createCell(1);
head_cell_2.setCellValue("性別");
head_cell_2.setCellStyle(headStyle);
HSSFCell head_cell_3 = headRow.createCell(2);
head_cell_3.setCellValue("年齡");
head_cell_3.setCellStyle(headStyle);
//為內容行添加數據和樣式
for (int i = 1; i <= list.size(); i++) {
HSSFRow contentRow = sheet_1.createRow(i);
HSSFCell content_cell_1 = contentRow.createCell(0);
content_cell_1.setCellValue(list.get(i-1).getName());
content_cell_1.setCellStyle(contentStyle);
HSSFCell content_cell_2 = contentRow.createCell(1);
content_cell_2.setCellValue(list.get(i-1).getSex());
content_cell_2.setCellStyle(contentStyle);
HSSFCell content_cell_3 = contentRow.createCell(2);
content_cell_3.setCellValue(list.get(i-1).getAge());
content_cell_3.setCellStyle(contentStyle);
}
return wb;
} catch (Exception e) {
e.getStackTrace();
}
return null;
}
/**
* 創建標題行樣式
* @param wb
* @return
*/
public static HSSFCellStyle headStyle(HSSFWorkbook wb){
HSSFCellStyle headStyle = wb.createCellStyle(); //創建樣式對象
HSSFFont headFont = wb.createFont(); //創建字體
headFont.setFontName("微軟雅黑");
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headFont.setColor(HSSFFont.COLOR_RED);
headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headStyle.setFont(headFont);
return headStyle;
}
/**
* 創建內容行樣式
* @param wb
* @return
*/
public static HSSFCellStyle contentStyle(HSSFWorkbook wb){
HSSFCellStyle contentStyle = wb.createCellStyle();
HSSFFont contentFont = wb.createFont();
contentFont.setFontName("微軟雅黑");
contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
contentFont.setColor(HSSFFont.COLOR_NORMAL);
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
contentStyle.setFont(contentFont);
return contentStyle;
}
}
