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;
}
}
效果如图:
应用场景:
一般多用于导入,导入时先设计好下载模板,用户按照模板写好内容再导入就能匹配上了