Java下载创建好的Excel模板
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模板的更多相关文章
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- java实现赋值excel模板,并在新文件中写入数据,并且下载
/** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...
- java 下载Excel模板
前端: JSP: <div id="insertBtn" class="MyCssBtn leftBtn" onclick="download( ...
- java实现excel模板导出
一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...
- vue Excel导入,下载Excel模板,导出Excel
vue Excel导入,下载Excel模板,导出Excel vue Excel导入,下载Excel模板 <template> <div style="display: ...
- NPOI之使用EXCEL模板创建报表
因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...
- PPT、Word、Excel模板免费下载
本篇文章可能只有寥寥数字,但他的作用可能很大,可能帮助到很多朋友.本人喜欢搜集一些资源,也爱免费分享,因为好东西我藏不住,总感觉分享出来更快乐. 网址:https://www.bangongziyua ...
- JAVA生成EXCEL模板
JAVA生成excel模板,支持1.必填字段前加 红色 * 2.定义可选值下拉列表 valList3.定义名称并通过名称设置可选值 refName(名称在sheet2,sheet2自动隐藏)4.支持设 ...
- 下载excel模板,导入数据时需要用到
页面代码: <form id="form1" enctype="multipart/form-data"> <div style=" ...
随机推荐
- Java面试题之对static的理解
1.静态变量 类型说明符是static.2.静态变量属于静态存储方式,其存储空间为内存中的静态数据区(在 静态存储区内分配存储单元),该区域中的数据在整个程序的运行期间一直占用这些存储空间(在程序整个 ...
- Python replace()方法
描述 Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: st ...
- java正则使用方法
import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegexMatches{ public ...
- Integer的疑惑
1.Integer m =200; Integer n =200; System.out.println(m==n); 输出falseInteger x =6; Integer y=6; System ...
- bzoj3598 [Scoi2014]方伯伯的商场之旅
数位dp,我们肯定枚举集合的位置,但是如果每次都重新dp的话会很麻烦,所以我们可以先钦定在最低位集合,dp出代价,然后再一步步找到正确的集合点,每次更改的代价也dp算就好了. #include < ...
- noip 2015 斗地主 大爆搜!!!
反正肯定是大模拟 但是每一个可以出的牌都搜一定不是最优的 考虑最特殊的出牌方案:顺子(单,对,三) 每一种方案再加上暴力贪心打出剩下的牌的步数 #include<cstdio> #incl ...
- python 备份文件脚本
使用python备份服务器的文件 #coding=utf- import os import os.path def copyFiles(sourceDir, targetDir): for file ...
- Java开源生鲜电商平台-安全设计与架构(源码可下载)
Java开源生鲜电商平台-安全设计与架构(源码可下载) 说明:Java开源生鲜电商平台是一个B2B的生鲜电商平台,由于是APP的一种模式,所以安全方面一般会思考以下几个维度: 1.数据安全. 2.传输 ...
- Django admin自定制功能
一:基础设置 1.应用注册 1)方式一 若要把app应用显示在后台管理中,需要在admin.py中注册.打开admin.py文件,如下代码: from django.contrib import ad ...
- 使用jdbc拼接条件查询语句时如何防止sql注入
本人微信公众号,欢迎扫码关注! 使用jdbc拼接条件查询语句时如何防止sql注入 最近公司的项目在上线时需要进行安全扫描,但是有几个项目中含有部分老代码,操作数据库时使用的是jdbc,并且竟然好多都是 ...