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=" ...
随机推荐
- Jenkins持续集成项目搭建与实践——基于Python Selenium自动化测试(自由风格)
Jenkins简介 Jenkins是Java编写的非常流行的持续集成(CI)服务,起源于Hudson项目.所以Jenkins和Hudson功能相似. Jenkins支持各种版本的控制工具,如CVS.S ...
- ubuntu 16.04启用root用户方法
1.使用:sudo passwd root设置root的密码,如下图所示: 2.使用su root来测试是否可以进入root用户,如果出现#说明已经设置root用户的密码成功,如下图所示:
- Firefox Profile
win7环境下打开Firefox,跳出提示信息 "无法加载你的firefox配置文件 它可能已经丢失或无法访问" 解决方法: 1.按下WIN+R 调出运行,然后输入: firefo ...
- 【BZOJ 2744】【HEOI2012】朋友圈
题目链接: TP 题解: 对于A国,我们发现,最大团一定不大于2.对于B国,发现同奇偶性点之间都有边,不同奇偶性之间可能有边,也就是说对于B国是一个二分图最大团,也就是求B国补图的二分图最大独立集.然 ...
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- Windows上安装配置SSH教程(1)——知识点汇总
1.是什么SSH? 维基百科:https://zh.wikipedia.org/wiki/Secure_Shell 其他博客:http://www.ruanyifeng.com/blog/2011/1 ...
- Python + Appium 获取当前屏幕的截图方法的封装
使用方法:get_screenshot_as_file(filename),来自于selenium\webdriver\remote\webdiver.py def take_screenShot(s ...
- Lucene 源码分析之倒排索引(三)
上文找到了 collect(-) 方法,其形参就是匹配的文档 Id,根据代码上下文,其中 doc 是由 iterator.nextDoc() 获得的,那 DefaultBulkScorer.itera ...
- 解决vs2019中暂时无法为.net core WinForms使用 Designer 的临时方法
目录 解决vs2019中暂时无法为.net core WinForms使用 Designer 的临时方法 安装 vs 2019 professional/enterprise版本 在vs的设置里,勾选 ...
- [Hyperledger] Fabric系统中 peer模块的 gossip服务详解
最近一直在看fabric系统中的核心模块之一——peer模块.在看peer的配置文件core.yaml的信息时,对其中的gossip配置选项很感兴趣.看了一上午,还是不能明白这个选项到底什么意思呢?表 ...