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=" ...
随机推荐
- 电梯调度算法---SCAN算法
请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. 扫描(SCAN)调度算法:总是从磁臂当前位置开始,沿磁臂的移动方向去选择离当前磁臂最近的那个柱面的访问者.如果沿磁臂的方向无请求访问时,就改变磁 ...
- 玩转PHP(一)---php中处理汉字字符串长度:strlen和mb_strlen
注:本文为小编原创,如若转载,请注明出处:http://blog.csdn.net/u012116457/article/details/42536039 今天正式开始学习PHP了,不过小编一不小心就 ...
- C# 操作Word文本框——插入表格/读取表格/删除表格
在文本框中,我们可以操作很多元素,如文本.图片.表格等,在本篇文章中将着重介绍如何插入表格到文本框,插入的表格我们可以对表格进行格式化操作来丰富表格内容.此外,对于文本框中的表格内容,我们也可以根据需 ...
- inner join 与 left join 之间的区别
关于inner join 与 left join 之间的区别,以前以为自己搞懂了,今天从前端取参数的时候发现不是预想中的结果,才知道问题出在inner join 上了. 需求是从数据库查数据,在前端以 ...
- CSS实现核辐射警告标志
今天做了下360的前端星计划测试题,碰到一个有趣的css题,实现如下图效果,记得上次也是在360面试的时候碰到一个有趣的css实现宝马logo,不得不说360的面试题还是很有创意的. 我一直努力想用一 ...
- bzoj 2510 弱题 矩阵乘
看题就像矩阵乘 但是1000的数据无从下手 打表发现每一行的数都是一样的,只不过是错位的,好像叫什么循环矩阵 于是都可以转化为一行的,O(n3)->O(n2)*logk #include< ...
- 【游戏开发】Excel表格批量转换成lua的转表工具
一.简介 在上篇博客<[游戏开发]Excel表格批量转换成CSV的小工具> 中,我们介绍了如何将策划提供的Excel表格转换为轻便的CSV文件供开发人员使用.实际在Unity开发中,很多游 ...
- Python多版本管理-pyenv
经常遇到这样的情况: 系统自带的Python是2.x,自己需要Python 3.x,此时需要在系统中安装多个Python,但又不能影响系统自带的Python,即需要实现Python的多版本共存,pye ...
- 【已解决】【Mac】 运行adb提示command not found,需要配置adb环境
问题:运行adb提示command not found 解决措施: 1.下载安装:android-sdk-macosx 下载路径:http://down.tech.sina.com.cn/page/ ...
- Mybatis配置详解
一.SqlSession的使用范围说明 1.SQLSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory, ...