1.废话不多说,直接上源码

//从数据库取数据创建表格	
private HSSFWorkbook exportStudentInfo(List<ExamStudentVo> studentList, List<ExamStudentVo> subjectName,
Integer testId) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheetlist = wb.createSheet();
sheetlist.protectSheet(Constant.EXCEL_LOCK_PASSWORD);
HSSFRow row1 = sheetlist.createRow(0);
row1.createCell(0).setCellValue(Constant.EXCEL_ID);// 身份标识
row1.createCell(1).setCellValue(testId);
HSSFRow row2 = sheetlist.createRow(1);
sheetlist.setColumnWidth(3, 15 * 256);// 设置宽度
sheetlist.setColumnWidth(4, 12 * 256);
row1.setZeroHeight(true);// 隐藏行
sheetlist.setColumnHidden(1, true);// 隐藏列
org.apache.poi.hssf.usermodel.HSSFCellStyle unLockCellStyle = wb.createCellStyle();
unLockCellStyle.setLocked(false);
row2.createCell(0).setCellValue("序号");
row2.createCell(1).setCellValue("学生ID");
row2.createCell(2).setCellValue("姓名");
row2.createCell(3).setCellValue("年级");
row2.createCell(4).setCellValue("班级");
int t = 5;
for (int i = 0; i < subjectName.size(); ++i) {
row1.createCell(t).setCellValue(subjectName.get(i).getSubjectId());
row2.createCell(t).setCellValue(subjectName.get(i).getSubjectName());
t++;
}
int temRow = 2; for (int i = 0; i < studentList.size(); ++i) {
HSSFRow row3 = sheetlist.createRow(temRow);
if (StringUtil.isNullOrEmpty(studentList.get(i))) {
continue;
}
row3.createCell(0).setCellValue(i + 1);
row3.createCell(1).setCellValue(studentList.get(i).getStudentId());
row3.createCell(2).setCellValue(studentList.get(i).getStudentName());
row3.createCell(3)
.setCellValue(studentList.get(i).getStageName() + "部" + studentList.get(i).getEntranceYear() + "级");
row3.createCell(4).setCellValue(studentList.get(i).getClassName());
temRow++;
int tem = 5;
for (int j = 0; j < subjectName.size(); ++j) {
row3.createCell(tem).setCellValue(HSSFCell.CELL_TYPE_STRING);
row3.createCell(tem).setCellStyle(unLockCellStyle);
tem++;
}
}
return wb;
}

2.下载

              HSSFWorkbook wb = importScoreService.exportTestInfo(eq.getTestId());
String testName = importScoreService.getTestName(eq.getTestId());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
wb.write(byteArrayOutputStream);
testName = StringUtils.deleteWhitespace(testName);
String dateTime = DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
//设置文件标题
String outFile = "学生成绩/" + dateTime +"/" + testName + ".xls";
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//对文件名编码
outFile = response.encodeURL(new String(outFile.getBytes("gb2312"), "iso8859-1"));
//使用Servlet实现文件下载的时候,避免浏览器自动打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + outFile);
//将流写进response输出流中
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();

3.下载完毕

Java下载创建好的Excel模板的更多相关文章

  1. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  2. java实现赋值excel模板,并在新文件中写入数据,并且下载

    /** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...

  3. java 下载Excel模板

    前端: JSP: <div id="insertBtn" class="MyCssBtn leftBtn" onclick="download( ...

  4. java实现excel模板导出

    一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...

  5. vue Excel导入,下载Excel模板,导出Excel

    vue  Excel导入,下载Excel模板,导出Excel vue  Excel导入,下载Excel模板 <template> <div style="display: ...

  6. NPOI之使用EXCEL模板创建报表

    因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...

  7. PPT、Word、Excel模板免费下载

    本篇文章可能只有寥寥数字,但他的作用可能很大,可能帮助到很多朋友.本人喜欢搜集一些资源,也爱免费分享,因为好东西我藏不住,总感觉分享出来更快乐. 网址:https://www.bangongziyua ...

  8. JAVA生成EXCEL模板

    JAVA生成excel模板,支持1.必填字段前加 红色 * 2.定义可选值下拉列表 valList3.定义名称并通过名称设置可选值 refName(名称在sheet2,sheet2自动隐藏)4.支持设 ...

  9. 下载excel模板,导入数据时需要用到

    页面代码: <form id="form1" enctype="multipart/form-data"> <div style=" ...

随机推荐

  1. 19.最省钱的app发短信方法

    在创业团队中,一个重要的原则是能省就省,该花就花,把银子用在刀刃上. 现在的app,为了获取用户的社交关系,需要用户的手机号注册.用手机号注册就涉及到一个发送短信验证码的问题,那怎么才能在短信服务上投 ...

  2. SQL Server 2000安装教程图解

    SQL Server 2000安装教程图解... ============= 下面网盘链接中的SQL2000数据库在Win7和Win10系统上安装都是可以正常使用的,只不过是Win10上安装的话,需要 ...

  3. Python爬虫进阶六之多进程的用法

    前言 在上一节中介绍了thread多线程库.python中的多线程其实并不是真正的多线程,并不能做到充分利用多核CPU资源. 如果想要充分利用,在python中大部分情况需要使用多进程,那么这个包就叫 ...

  4. Firefox Profile (2)

    一些关于selenium copy Firefox profile to a temp directory的讨论 https://stackoverflow.com/questions/6787095 ...

  5. segment.go

    package sego // 文本中的一个分词 type Segment struct {     // 分词在文本中的起始字节位置     start int     // 分词在文本中的结束字节 ...

  6. BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP

    BZOJ_1705_[Usaco2007 Nov]Telephone Wire 架设电话线_DP Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是 ...

  7. 用Python学分析 - t分布

    1. t分布形状类似于标准正态分布2.  t分布是对称分布,较正态分布离散度强,密度曲线较标准正态分布密度曲线更扁平3.  对于大型样本,t-值与z-值之间的差别很小 作用- t分布纠正了未知的真实标 ...

  8. ZZZPHP1.61 代码审计-从SQL注入到Getshell

    近期有很多小伙伴在后台留言想看关于代码审计的文章,其实有关审计的文章网上资源是比较多的,但是从代码审计开始到结束的这类文章却少之甚少. 今天要讲解的ZZZPHP1.61这套审计漏洞比较多,SQL注入漏 ...

  9. 学习笔记02(随便看看mybatis源码)

    两个很有名的持久层hibernate和mybatis应该很熟悉不过了,两者最大相同点是底层都是对jdbc的封装,最大的不同点是前者是自动生成sql语句,后者是需要我们在映射文件中写出sql. 其实从以 ...

  10. Go:学习笔记兼吐槽(3)

    Go:学习笔记兼吐槽(1) Go:学习笔记兼吐槽(2) Go:学习笔记兼吐槽(3) 数组 Golang 中,数组是值类型. 数组的声明 var arr [10]int 数组的初始化 var arr1  ...