导出excel模板
1. Controller
package org.lhj.pro.template;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/excel")
public class ExcelController {
@Autowired
private ExcelService excelService;
@RequestMapping("/student")
public void downLoadUserTemplate(HttpServletRequest request, HttpServletResponse response, String type) {
String fileName = null;
String path = null;
if (null == type || "" == type) {
fileName = "教师模板.xls";
path = "template/teacherTemplate.xls";
}
if (type == "student") {
fileName = "学生模板.xls";
path = "template/studentTemplate.xls";
}
excelService.downloadTemplate(request, response, fileName, path);
}
}
2. Service
package org.lhj.pro.template;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface ExcelService {
void downloadTemplate(HttpServletRequest request, HttpServletResponse response, String fileName, String path);
}
3. ServiceImpl
package org.lhj.pro.template;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.stereotype.Service;
@Service
public class ExcelServiceImpl implements ExcelService {
private Logger logger = LoggerFactory.getLogger(ExcelServiceImpl.class);
public void downloadTemplate(HttpServletRequest request, HttpServletResponse response, String fileName,
String path) {
InputStream inputStream = null;
ServletOutputStream servletOutputStream = null;
try {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:" + path);
// 判断使用的浏览器
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) {
response.addHeader("Content-Disposition", "attachment; filename=\""
+ java.net.URLEncoder.encode(fileName, "UTF-8").replace("+", " ") + "\"");
} else if (null != agent && -1 != agent.indexOf("Firefox")) {
response.addHeader("Content-Disposition",
"attachment; filename=\"" + new String(fileName.getBytes(), "iso8859-1") + "\"");
} else {
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(java.net.URLEncoder.encode(fileName, "utf-8").getBytes(), "iso8859-1"));
}
response.setContentType("application/vnd.ms-excel");
inputStream = resource.getInputStream();
servletOutputStream = response.getOutputStream();
IOUtils.copy(inputStream, servletOutputStream);
response.flushBuffer();
} catch (Exception e) {
logger.error("下载Excel模版失败!", e);
} finally {
try {
if (servletOutputStream != null) {
servletOutputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
logger.error("关闭IO流失败!", e);
}
}
}
}