Excel模板下载(带下拉框)
当我们需要实时制作⼀个带有下拉框的Excel上传模板时,⽐如我们要上传员⼯信息,⽽上传那些信息⼜是有格式要求,此时就需要制作⼀个上传的Excel模板供⽤户下载,其中,岗位和部门⼜是实时更新的,故下拉框的内容⼜不固定,需要实时从数据库更新,我们的思路就是,每次下载时,都将数据更新到⼀个新的⽂档⾥⾯,然后再下载这个⽂档,⽤的io包括,先是制作表格存到本地服务器,然后就是根据路径将⽂档读取出来,⽤到了FileInputStream()流,具体看如下代码:
/**
* 模板下载
*
* @param request
*/
@RequestMapping(value = "/downloadTemplate")
public void download(HttpServletResponse response) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = Request();
// ⽂件名
String filename = "员⼯导⼊模板.xlsx";
try {
// 写到服务器上
String path = Session().getServletContext().getRealPath("") + "/" + filename;
File name = new File(path);
// 创建写⼯作簿对象
WritableWorkbook workbook = ateWorkbook(name);
/
/ ⼯作表
WritableSheet sheet = ateSheet("员⼯导⼊模板", 0);
// 设置字体;
WritableFont font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat cellFormat = new WritableCellFormat(font);
// 设置背景颜⾊;
cellFormat.setBackground(Colour.WHITE);
// 设置边框;
cellFormat.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);
// 设置⽂字居中对齐⽅式;
cellFormat.setAlignment(Alignment.CENTRE);
// 设置垂直居中;
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
// 分别给1,2,3,4列设置不同的宽度;
sheet.setColumnView(0, 15);
sheet.setColumnView(1, 15);
sheet.setColumnView(2, 15);
sheet.setColumnView(3, 15);
sheet.setColumnView(4, 15);
// 给sheet电⼦版中所有的列设置默认的列的宽度;
/
/ 给sheet电⼦版中所有的⾏设置默认的⾼度,⾼度的单位是1/20个像素点,但设置这个貌似就不能⾃动换⾏了
// 设置⾃动换⾏;
cellFormat.setWrap(true);
// 给第⼆⾏设置背景、字体颜⾊、对齐⽅式等等;
WritableFont font1 = new WritableFont(WritableFont.ARIAL, 14, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.RED);
WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
WritableFont font2 = new WritableFont(WritableFont.ARIAL, 14, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
// 设置⽂字居中对齐⽅式;
cellFormat2.setAlignment(Alignment.CENTRE);
// 设置垂直居中;
// 设置垂直居中;
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat2.setBackground(Colour.WHITE);
cellFormat1.setBackground(Colour.WHITE);
cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat2.setWrap(true);
// 记录⾏数
int n = 1;
// 获取当前的合作机构ID
Long partnerId = (Long) Session().getAttribute("partnerId");
// 获取得到所有部门信息
List<Department> deptList = departmentService.selectAll(partnerId);
// 获取得到所有岗位信息
List<Post> postList = postService.selectAll(partnerId);
List<String> deptName = new ArrayList<String>();
List<String> postName = new ArrayList<String>();
WritableCellFeatures wcf1 = new WritableCellFeatures();// 待选择集合对象,这是jxl的对象
WritableCellFeatures wcf2 = new WritableCellFeatures();// 待选择集合对象,这是jxl的对象
Label lt1 = new Label(0, 0, "⼯号", cellFormat2);
Label lt2 = new Label(1, 0, "姓名", cellFormat2);
Label lt3 = new Label(2, 0, "性别", cellFormat2);
Label lt4 = new Label(3, 0, "部门", cellFormat2);
免费模板制作下载Label lt5 = new Label(4, 0, "岗位", cellFormat2);
Label lt6 = new Label(5, 0, "1、添加员⼯部门、岗位的时候,按照员⼯数量的多少,将下拉框样式往下复制,然后点击选择下拉框中的信息,最后导⼊的时候将该句话    cellFormat1);
Label lblColumn1 = new Label(0, 1, "xxx", cellFormat2);
Label lblColumn2 = new Label(1, 1, "xxx", cellFormat2);
Label lblColumn3 = new Label(2, 1, "xxx", cellFormat2);
Label lblColumn4 = new Label(3, 1, "请选择部门", cellFormat2);// ⽣成⼀个待选择的标签
Label lblColumn5 = new Label(4, 1, "请选择岗位", cellFormat2);// ⽣成⼀个待选择的标签
// 设置单元格
for (int i = 0; i < postList.size(); i++) {
postName.(i).getPostName());
}
// 设置单元格
for (int i = 0; i < deptList.size(); i++) {
deptName.(i).getDepartmentName());
}
// 将部门集合导⼊到表格中去
wcf1.setDataValidationList(deptName);// 设置jxl对象要选择的集合
lblColumn4.setCellFeatures(wcf1);// 设置到单元格⾥⾯去
// 将岗位集合导⼊到表格中去
wcf2.setDataValidationList(postName);// 设置jxl对象要选择的集合
lblColumn5.setCellFeatures(wcf2);// 设置到单元格⾥⾯去
//以下是将我们上⾯设置的单元格加⼊到我们的表中去
sheet.addCell(lt1);
sheet.addCell(lt2);
sheet.addCell(lt3);
sheet.addCell(lt4);
sheet.addCell(lt5);
sheet.addCell(lt6);
sheet.addCell(lblColumn1);
sheet.addCell(lblColumn2);
sheet.addCell(lblColumn3);
sheet.addCell(lblColumn4);
sheet.addCell(lblColumn5);
// 开始执⾏写⼊操作,将该模板写⼊到具体的路径下⾯
workbook.write();
// 关闭流
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
OutputStream out = null;
OutputStream out = null;
try {
response.addHeader("content-disposition",
"attachment;filename=" + de(filename, "utf-8"));
// 2.下载
out = OutputStream();
String path3 = Session().getServletContext().getRealPath("") + "/" + filename;
// inputStream:读⽂件,前提是这个⽂件必须存在,要不就会报错
InputStream is = new FileInputStream(path3);
byte[] b = new byte[4096];
int size = is.read(b);
while (size > 0) {
out.write(b, 0, size);
size = is.read(b);
}
out.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。