public void exportExel(String fileName, String pwd,
List<Map<String, Object>> listContent, HttpServletRequest request,
HttpServletResponse response) throws IOException {
SysUserVO userInfo = ResourceUtil.getSessionUserName(request);
String userId = userInfo.getUserId();
// 添加下载日志
service.insertExportLog(userId, fileName);
// poi使用模板生成excel
String realPath = request.getSession().getServletContext()
.getRealPath("");
String inPath = realPath
+ "/WEB-INF/classes/com/tdxx/excel/template.xlsx";
// 获取模板
FileInputStream fis = new FileInputStream(inPath);
// 如果是xlsx,2007,用XSSF,如果是xls,2003,用HSSF
XSSFWorkbook workBook = new XSSFWorkbook(fis);
// 获取第一个sheet页。
// XSSFSheet sheet = workBook.getSheetAt(0);
if (listContent != null && listContent.size() > 0) {
// 标题部分
List<String> titleList = new ArrayList<String>(listContent.get(0)
.keySet());
String[] excelKeyArray = new String[titleList.size()];
for (int j = 0; j < titleList.size(); j++) {
excelKeyArray[j] = titleList.get(j);
}
// 数据全部放到一个sheet里面
int len = listContent.size();
XSSFSheet sheet = workBook.getSheetAt(0); // 创建一个工作表
// 设置权限
if (pwd != null) {
sheet.enableLocking();
sheet.setSheetPassword(pwd, null);
CTSheetProtection sheetProtection = sheet.getCTWorksheet()
.getSheetProtection();
sheetProtection.setSelectLockedCells(true);
sheetProtection.setSelectUnlockedCells(true);
sheetProtection.setFormatCells(true);
sheetProtection.setFormatColumns(true);
sheetProtection.setFormatRows(true);
sheetProtection.setInsertColumns(true);
sheetProtection.setInsertRows(true);
sheetProtection.setInsertHyperlinks(true);
sheetProtection.setDeleteColumns(true);
sheetProtection.setDeleteRows(true);
sheetProtection.setSort(true);
sheetProtection.setAutoFilter(true);
sheetProtection.setPivotTables(true);
sheetProtection.setObjects(true);
sheetProtection.setScenarios(true);
} else {
sheet.getCTWorksheet().unsetSheetProtection();
}
// 标题
XSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < excelKeyArray.length; i++) {
titleRow.createCell(i).setCellValue(excelKeyArray[i]);
}
// 内容
for (int i = 0; i < len; i++) {
XSSFRow contentRow = sheet.createRow(i + 1);
for (int j = 0; j < excelKeyArray.length; j++) {
String value = listContent.get(i).get(excelKeyArray[j]) == null ? ""
: listContent.get(i).get(excelKeyArray[j])
.toString().replaceAll("<[^>]*>", "");
contentRow.createCell(j).setCellValue(value);
}
}
}
// 设置内容
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "8859_1") + ".xlsx");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
OutputStream outputStream = new BufferedOutputStream(
}
List<Map<String, Object>> listContent, HttpServletRequest request,
HttpServletResponse response) throws IOException {
SysUserVO userInfo = ResourceUtil.getSessionUserName(request);
String userId = userInfo.getUserId();
// 添加下载日志
service.insertExportLog(userId, fileName);
// poi使用模板生成excel
String realPath = request.getSession().getServletContext()
.getRealPath("");
String inPath = realPath
+ "/WEB-INF/classes/com/tdxx/excel/template.xlsx";
// 获取模板
FileInputStream fis = new FileInputStream(inPath);
// 如果是xlsx,2007,用XSSF,如果是xls,2003,用HSSF
XSSFWorkbook workBook = new XSSFWorkbook(fis);
// 获取第一个sheet页。
// XSSFSheet sheet = workBook.getSheetAt(0);
if (listContent != null && listContent.size() > 0) {
// 标题部分
List<String> titleList = new ArrayList<String>(listContent.get(0)
.keySet());
String[] excelKeyArray = new String[titleList.size()];
for (int j = 0; j < titleList.size(); j++) {
excelKeyArray[j] = titleList.get(j);
}
// 数据全部放到一个sheet里面
int len = listContent.size();
XSSFSheet sheet = workBook.getSheetAt(0); // 创建一个工作表
// 设置权限
if (pwd != null) {
sheet.enableLocking();
sheet.setSheetPassword(pwd, null);
CTSheetProtection sheetProtection = sheet.getCTWorksheet()
.getSheetProtection();
sheetProtection.setSelectLockedCells(true);
sheetProtection.setSelectUnlockedCells(true);
sheetProtection.setFormatCells(true);
sheetProtection.setFormatColumns(true);
sheetProtection.setFormatRows(true);
sheetProtection.setInsertColumns(true);
sheetProtection.setInsertRows(true);
sheetProtection.setInsertHyperlinks(true);
sheetProtection.setDeleteColumns(true);
sheetProtection.setDeleteRows(true);
sheetProtection.setSort(true);
sheetProtection.setAutoFilter(true);
sheetProtection.setPivotTables(true);
sheetProtection.setObjects(true);
sheetProtection.setScenarios(true);
} else {
sheet.getCTWorksheet().unsetSheetProtection();
}
// 标题
XSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < excelKeyArray.length; i++) {
titleRow.createCell(i).setCellValue(excelKeyArray[i]);
}
// 内容
for (int i = 0; i < len; i++) {
XSSFRow contentRow = sheet.createRow(i + 1);
for (int j = 0; j < excelKeyArray.length; j++) {
String value = listContent.get(i).get(excelKeyArray[j]) == null ? ""
: listContent.get(i).get(excelKeyArray[j])
.toString().replaceAll("<[^>]*>", "");
contentRow.createCell(j).setCellValue(value);
}
}
}
// 设置内容
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "8859_1") + ".xlsx");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
OutputStream outputStream = new BufferedOutputStream(
response.getOutputStream());
workBook.write(outputStream);
outputStream.flush();
outputStream.close();
response.flushBuffer();}