下载模板

jsp:

<input type="button" value="下载导入模板" class="layui-btn" onclick="downMode()">

js:

/**
 * 
 * @returns
 */
function downMode(){
	confirm("确定要下载此模板吗?",function (){
		window.open("scholarShip/downMode","_self")
	});
}

java

Controller:
	/**
	 * 下载奖学金导入模板
	 */
	@SysLog("下载奖学金导入模板")
	@RequestMapping(value = "/downMode", method = RequestMethod.GET)
	public void downMode(HttpServletResponse response) {
		scholarShipService.downMode(response);
	}
Service:
/**
	 * 下载模板
	 * @param response
	 */
	public void downMode(HttpServletResponse response) {
		String date = DateUtil.formatDate("yyyy年MM月dd日 HH点mm分ss秒");
		String fileName="入学奖学金导入模板"+date;
		try {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ScholarShipUtil.downloadMode().write(os);
			down(response,os,fileName);	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	public void down(HttpServletResponse response,ByteArrayOutputStream os,String fileName) throws IOException {
		 byte[] content = os.toByteArray();
	     InputStream is = new ByteArrayInputStream(content);
		 // 设置response参数,可以打开下载页面
       response.reset();
       response.setContentType("application/vnd.ms-excel;charset=utf-8");
       response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
       ServletOutputStream out = response.getOutputStream();
       BufferedInputStream bis = null;
       BufferedOutputStream bos = null;
       try {
           bis = new BufferedInputStream(is);
           bos = new BufferedOutputStream(out);
           byte[] buff = new byte[2048];
           int bytesRead;
           while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
               bos.write(buff, 0, bytesRead);
           }
       } catch (final IOException e) {
           throw e;
       } finally {
           if (bis != null)
               bis.close();
           if (bos != null)
               bos.close();
       }
	}
Util:
package com.bigdata.campus.enroll.util;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.bigdata.campus.util.Constants;
import com.google.common.collect.Maps;

/**
 * 奖学金 下载导入模板
 * @author 
 *
 */
public class ScholarShipUtil {
	
	 private static Map<String,String> scholarShipMap=Maps.newLinkedHashMap();
	/**
	 * 下载奖学金导入模板
	 */
	 public static Workbook downloadMode() {
		 // 创建excel工作簿
		 Workbook wb = new HSSFWorkbook();
		 //教学周数加上作业列加上序号学号姓名
		 CellStyle cs3_String = createCellStyle_String(wb);//String格式
		 Sheet sheet = wb.createSheet("sheet1");
			 sheet.setVerticallyCenter(true);//设置打印页面为垂直居中使用POI输出Excel时打印页面的设置
			 sheet.createFreezePane(1,1,1,1);
			 Map<String, String> maps =getScholarShipMap();
			 //创建第一行表头
			 Row row = sheet.createRow((short) 0);
			 row.setHeight((short) (3 * 200));
		 	 row.setHeight((short) (3 * 200));
		 	 int i = 0;
		 	 /*循环map获取需要下载的列*/
		 	 for(Entry<String, String> map:maps.entrySet()) {
		 		 sheet.setColumnWidth((short) i, (short) (40 * 160));
				 Cell cell = row.createCell(i);
				 cell.setCellValue(map.getValue());
				 cell.setCellStyle(cs3_String);
				 i++;
		 	 }
		 return wb;
	 }
	 /**
		 *入学奖学金map
		 */
	 public static Map<String,String> getScholarShipMap(){
		if(scholarShipMap.isEmpty()) {
			scholarShipMap.put("stuName","姓名");
			scholarShipMap.put("stuNumber","学号");
			scholarShipMap.put("examNumber","考生号");
			scholarShipMap.put("idCard","身份证号");
			scholarShipMap.put("totalMoney","奖学金总额");
			scholarShipMap.put("distributeNumber","分发次数");
		}
		return scholarShipMap;
	 }
	/**
	 * 普通文本格式
	 * @param wb
	 * @return
	 */
	private static CellStyle createCellStyle_String(Workbook wb) {
		// 创建单元格格式
		 CellStyle cs3 = wb.createCellStyle();
		 // 创建字体
		 Font f2 = wb.createFont();
		 // 创建第二种字体样式(用于值)
		 f2.setFontHeightInPoints((short) 12);
		 f2.setColor(IndexedColors.BLACK.getIndex());
		 f2.setFontName("宋体");
		 // 设置第二种单元格的样式(用于值)
		 cs3.setFont(f2);
		 cs3.setBorderLeft(CellStyle.BORDER_THIN);
		 cs3.setBorderRight(CellStyle.BORDER_THIN);
		 cs3.setBorderTop(CellStyle.BORDER_THIN);
		 cs3.setBorderBottom(CellStyle.BORDER_THIN);
		 cs3.setAlignment(CellStyle.ALIGN_CENTER);
		 cs3.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中
		 DataFormat df = wb.createDataFormat();  //此处设置数据格式  
		 cs3.setDataFormat(df.getFormat("@")); //数据格式只显示整数,如果是小数点后保留两位,可以写contentStyle.setDataFormat(df.getFormat("#,#0.00"));  
		return cs3;
	}
}

效果如图:
在这里插入图片描述
应用场景:
一般多用于导入,导入时先设计好下载模板,用户按照模板写好内容再导入就能匹配上了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值