后端代码:
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;
}
}
public class Person {
private String name;
private String sex;
private String age;
public Person(String name, String sex, String age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return Objects.equals(getName(), person.getName()) &&
Objects.equals(getSex(), person.getSex()) &&
Objects.equals(getAge(), person.getAge());
}
@Override
public int hashCode() {
return Objects.hash(getName(), getSex(), getAge());
}
}
前端代码
//导出excel 数据接口
function importExcels(){
alert("确定要导出数据吗?");
window.location.href = '${ctx}/qcpartition/exportExcel?ids='+startQcIdString+","+ finishQcIdString
}