引入依赖
<!--poi依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
后端代码
简单例子(下载前端要用全局刷新)
package com.bjpowernode.crm;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class test02 {
public static void main(String[] args) {
HSSFWorkbook hw=new HSSFWorkbook();
HSSFSheet hssfSheet=hw.createSheet("student");
HSSFRow row = hssfSheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell=row.createCell(1);
cell.setCellValue("学号");
cell=row.createCell(2);
cell.setCellValue("班级");
row = hssfSheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("郭");
cell=row.createCell(1);
cell.setCellValue("201617");
cell=row.createCell(2);
cell.setCellValue("2004");
try {
FileOutputStream out=new FileOutputStream("D:\\动力节点\\student.xls");
hw.write(out);
out.close();
hw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入从数据库中查询的内容并返回到前端
List<Activity> activityList = activityService.queryAllActivitys();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("市场活动列表");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("ID");
cell = row.createCell(1);
cell.setCellValue("所有者");
cell = row.createCell(2);
cell.setCellValue("名称");
cell = row.createCell(3);
cell.setCellValue("开始日期");
cell = row.createCell(4);
cell.setCellValue("结束日期");
cell = row.createCell(5);
cell.setCellValue("成本");
cell = row.createCell(6);
cell.setCellValue("描述");
cell = row.createCell(7);
cell.setCellValue("创建时间");
cell = row.createCell(8);
cell.setCellValue("创建者");
cell = row.createCell(9);
cell.setCellValue("修改时间");
cell = row.createCell(10);
cell.setCellValue("修改者");
Activity activity = null;
for (int i = 0; i < activityList.size(); i++) {
activity = activityList.get(i);
row = sheet.createRow(i + 1);
cell = row.createCell(0);
cell.setCellValue(activity.getId());
cell = row.createCell(1);
cell.setCellValue(activity.getOwner());
cell = row.createCell(2);
cell.setCellValue(activity.getName());
cell = row.createCell(3);
cell.setCellValue(activity.getStartDate());
cell = row.createCell(4);
cell.setCellValue(activity.getEndDate());
cell = row.createCell(5);
cell.setCellValue(activity.getCost());
cell = row.createCell(6);
cell.setCellValue(activity.getDescription());
cell = row.createCell(7);
cell.setCellValue(activity.getCreateTime());
cell = row.createCell(8);
cell.setCellValue(activity.getCreateBy());
cell = row.createCell(9);
cell.setCellValue(activity.getEditTime());
cell = row.createCell(10);
cell.setCellValue(activity.getEditBy());
}
response.setContentType("application/octet-stream;charset=UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=activityList.xls");
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
wb.close();