springmvc用java代码写Excel文件

引入依赖

 <!--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) {
        //创建一个Excel文件
        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();
        }


    }
}

写入从数据库中查询的内容并返回到前端

 //调用service方法,查询所有的市场活动
        List<Activity> activityList = activityService.queryAllActivitys();
        //根据查询结果,生成响应信息
        //创建一个HSSFWorkbook对象,对应一个excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //使用wb创建一个HSSFSheet对象,对应wb文件中的一页
        HSSFSheet sheet = wb.createSheet("市场活动列表");
        //使用sheet创建一个HSSFRow对象,对应sheet中的一行
        HSSFRow row = sheet.createRow(0);//行的编号,从0开始,0表示第一行,1表示第二行,....
        //使用row创建一个HSSFCell对象,对应row中的一列
        HSSFCell cell = row.createCell(0);//列的编号,从0开始,0表示第一列,1表示第二列,...
        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("修改者");

        //遍历activityList,生成数据行
        Activity activity = null;
        for (int i = 0; i < activityList.size(); i++) {
            //每遍历出一个activity对象
            activity = activityList.get(i);
            //生成一个HSSFRow对象,对应一行
            row = sheet.createRow(i + 1);
            //每一个row对象都创建11个HSSFCell对象,对应11列;每一列的数据从activity的属性中获取
            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());
        }
        //把生成excel文件返回浏览器
        //1.设置响应类型
        response.setContentType("application/octet-stream;charset=UTF-8");
        //设置响应头信息
        //默认情况下,浏览器接收到响应信息之后,会直接在显示窗口中打开,如果浏览器打不开响应信息,也会自动调用操系统的应用软件,只有实在打不开时,也会激活文件下载窗口.
        //可以设置响应响应头信息Content-Disposition,使浏览器接收到响应信息之后,直接激活文件下载窗口,即使能打开也不打开.
        response.addHeader("Content-Disposition", "attachment;filename=activityList.xls");
        //2.获取输出流
        OutputStream out = response.getOutputStream();
        wb.write(out);
		out.flush();//流是服务器创建的,由服务器关闭
        wb.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值