下面簡單給大家介紹poi導出配置
1.首先導入依賴
<dependency>
<groupid>org.apache.poi/<groupid>
<artifactid>poi/<artifactid>
<version>3.6/<version>
/<dependency>
2.創建工具類
package javaj.work.util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtil {
//參數說明: fileName:文件名 projects:對象集合 columnNames: 列名 keys: map中的key
public static void start_download(HttpServletResponse response, String fileName, List> projects,
String[] columnNames, String[] keys) throws IOException {
//將集合中對象的屬性 對應到 List
3.pojo
public class Student {
private int no;
private String name;
private int age;
//此處省略getter和setter
}
4.配置controller
@RequestMapping(value = "download")
public void index(HttpServletResponse response){
String fileName = "人員檔案列表";
List<student> students = new ArrayList<student>();
for (int i = 0;i<20 ;i++){
Student student = new Student();
student.setAge(i+10);
student.setName("張三"+i);
student.setNo(i);
students.add(student);
}
// 列名
String columnNames[] = { "ID", "姓名", "年齡" };
// map中的key
String keys[] = { "no", "name", "age" };
try {
ExcelUtil.start_download(response, fileName, students,columnNames, keys);
} catch (IOException e) {
e.printStackTrace();
}
}
/<student>/<student>
5.頁面