//1.获取模板的路径:
String lujing = request.getSession().getServletContext().getRealPath("/")+ "export\\template\\test.xlsx";
//2.准备数据(一个List
List
InputStream in;
try {
in = new FileInputStream(new File(lujing1));
XSSFWorkbook work = null;
work = new XSSFWorkbook(in);
//4.把map里的值复写入Excel模板里
//获取第一个sheet
XSSFSheet sheetAt = work.getSheetAt(0);
//从第三行开始赋值
XSSFRow row = sheetAt.createRow(i+2);
row.createCell(0).setCellValue("");
//……依次循环赋值
//5.导出前台的一些设置
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel"); //保证不乱码
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("MMddHHmmss");
String time="bb"+format.format(date)+".xlsx";//导出的Excel的名字
response.setHeader("Content-Disposition","attachment;" + " filename=" + new String(time.getBytes("utf-8"), "ISO-8859-1"));
ByteArrayOutputStream oss =new ByteArrayOutputStream();
OutputStream os = response.getOutputStream();
work.write(oss);
byte temp[] = oss.toByteArray();
ByteArrayInputStream in1 = new ByteArrayInputStream(temp);
int n = 0;
while ((n = in1.read(temp)) >0) {
os.write(temp, 0, n);
}
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
//附:引入的jar包的pom.xml依赖
手机扫一扫
移动阅读更方便
你可能感兴趣的文章