|
|
@ -1,10 +1,10 @@ |
|
|
|
package com.yxkLibraryIntegratedManage.utils; |
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.*; |
|
|
|
import org.apache.poi.ss.usermodel.*; |
|
|
|
import org.apache.poi.ss.util.CellRangeAddress; |
|
|
|
import org.apache.poi.xssf.usermodel.*; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.net.URLEncoder; |
|
|
@ -17,12 +17,67 @@ public class ExcelUtil { |
|
|
|
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
|
|
|
|
|
|
/** |
|
|
|
* 导出Excel |
|
|
|
* @param sheetName sheet名称 |
|
|
|
* @param title 标题 |
|
|
|
* @param values 内容 |
|
|
|
* @param wb HSSFWorkbook对象 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static XSSFWorkbook getXSSFWorkbook(String sheetName,String []title,String [][]values, XSSFWorkbook wb){ |
|
|
|
|
|
|
|
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件 |
|
|
|
if(wb == null){ |
|
|
|
wb = new XSSFWorkbook(); |
|
|
|
} |
|
|
|
|
|
|
|
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet |
|
|
|
XSSFSheet sheet = wb.createSheet(sheetName); |
|
|
|
|
|
|
|
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制 |
|
|
|
XSSFRow row = sheet.createRow(0); |
|
|
|
|
|
|
|
// 第四步,创建单元格,并设置值表头 设置表头居中 |
|
|
|
XSSFCellStyle style = wb.createCellStyle(); |
|
|
|
style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式 |
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
|
|
|
|
//声明列对象 |
|
|
|
XSSFCell cell = null; |
|
|
|
|
|
|
|
//创建标题 |
|
|
|
for(int i=0;i<title.length;i++){ |
|
|
|
cell = row.createCell(i); |
|
|
|
cell.setCellValue(title[i]); |
|
|
|
cell.setCellStyle(style); |
|
|
|
} |
|
|
|
|
|
|
|
//创建内容 |
|
|
|
for(int i=0;i<values.length;i++){ |
|
|
|
row = sheet.createRow(i + 1); |
|
|
|
for(int j=0;j<values[i].length;j++){ |
|
|
|
XSSFCell newCell = row.createCell(j); |
|
|
|
//将内容按顺序赋给对应的列对象 |
|
|
|
newCell.setCellValue(values[i][j]); |
|
|
|
newCell.setCellStyle(style); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static XSSFWorkbook createUnifyExcel(String fileName,String[] mark,String[] title, List<Map<String,Object>> list){ |
|
|
|
//自适应单元格 |
|
|
|
for (int i = 0; i < sheet.getLastRowNum(); i++) { |
|
|
|
sheet.autoSizeColumn(i); |
|
|
|
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10); |
|
|
|
} |
|
|
|
|
|
|
|
return wb; |
|
|
|
} |
|
|
|
|
|
|
|
public static XSSFWorkbook getXSSFWorkbook(String sheetName,String []title,String [][]values,String []title1,String [][]values1, XSSFWorkbook workbook){ |
|
|
|
// 创建一个工作薄 |
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook(); |
|
|
|
workbook = new XSSFWorkbook(); |
|
|
|
// 创建一个工作表 |
|
|
|
XSSFSheet sheet = workbook.createSheet(fileName); |
|
|
|
XSSFSheet sheet = workbook.createSheet(sheetName); |
|
|
|
|
|
|
|
// 设置表头字体格式 |
|
|
|
XSSFFont headersFont = workbook.createFont(); |
|
|
@ -52,56 +107,71 @@ public class ExcelUtil { |
|
|
|
textStyle.setBorderLeft(BorderStyle.THIN); |
|
|
|
textStyle.setBorderTop(BorderStyle.THIN); |
|
|
|
textStyle.setFont(dataSetFont); |
|
|
|
textStyle.setDataFormat(49); |
|
|
|
// 数据内容对齐方式:居中 |
|
|
|
textStyle.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
textStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
|
|
|
|
XSSFCellStyle bodyStyle = workbook.createCellStyle(); |
|
|
|
bodyStyle.setFont(dataSetFont); |
|
|
|
bodyStyle.setDataFormat(49); |
|
|
|
// 数据内容对齐方式:居中 |
|
|
|
bodyStyle.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
|
|
|
|
//行 |
|
|
|
int index = 1; |
|
|
|
XSSFRow fondsTitleRow = sheet.createRow(0); |
|
|
|
for(int i = 0;i<title.length;i++){ |
|
|
|
XSSFCell cell = fondsTitleRow.createCell(i); |
|
|
|
cell.setCellStyle(textStyle); |
|
|
|
Integer rowIndex = 0; |
|
|
|
|
|
|
|
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制 |
|
|
|
XSSFRow thisTitle = sheet.createRow(rowIndex); |
|
|
|
|
|
|
|
//创建标题 |
|
|
|
for(int i=0;i<title.length;i++){ |
|
|
|
XSSFCell cell = thisTitle.createCell(i); |
|
|
|
cell.setCellValue(title[i]); |
|
|
|
sheet.setDefaultColumnStyle(i,bodyStyle); |
|
|
|
cell.setCellStyle(headersStyle); |
|
|
|
} |
|
|
|
|
|
|
|
for(Map<String,Object> map : list){ |
|
|
|
XSSFRow fondsValueRow = sheet.createRow(index); |
|
|
|
XSSFCell fondsValueCell0 = fondsValueRow.createCell(0);fondsValueCell0.setCellValue(index);fondsValueCell0.setCellStyle(textStyle); |
|
|
|
int columnIndex = 1; |
|
|
|
for(String t : mark){ |
|
|
|
XSSFCell fondsValueCell = fondsValueRow.createCell(columnIndex);fondsValueCell.setCellType(CellType.STRING);fondsValueCell.setCellValue(map.get(t)==null?"":map.get(t)+"");fondsValueCell.setCellStyle(textStyle); |
|
|
|
columnIndex++; |
|
|
|
rowIndex++; |
|
|
|
//创建内容 |
|
|
|
for(int i=0;i<values.length;i++){ |
|
|
|
XSSFRow valueRow = sheet.createRow(rowIndex); |
|
|
|
for(int j=0;j<values[i].length;j++){ |
|
|
|
XSSFCell newCell = valueRow.createCell(j); |
|
|
|
//将内容按顺序赋给对应的列对象 |
|
|
|
newCell.setCellValue(values[i][j]); |
|
|
|
newCell.setCellStyle(textStyle); |
|
|
|
} |
|
|
|
index++; |
|
|
|
rowIndex++; |
|
|
|
|
|
|
|
XSSFRow sonTitleRow = sheet.createRow(rowIndex); |
|
|
|
for(int t=0;t<title1.length;t++){ |
|
|
|
XSSFCell sonTitleCell = sonTitleRow.createCell(t); |
|
|
|
sonTitleCell.setCellValue(title1[t]); |
|
|
|
sonTitleCell.setCellStyle(headersStyle); |
|
|
|
} |
|
|
|
rowIndex++; |
|
|
|
for(int v = 0;v<values1.length;v++){ |
|
|
|
XSSFRow sonValueRow = sheet.createRow(rowIndex); |
|
|
|
for(int v1=0;v1<values1[v].length;v1++){ |
|
|
|
XSSFCell sonValueCell = sonValueRow.createCell(v1); |
|
|
|
//将内容按顺序赋给对应的列对象 |
|
|
|
sonValueCell.setCellValue(values1[v][v1]); |
|
|
|
sonValueCell.setCellStyle(textStyle); |
|
|
|
} |
|
|
|
rowIndex++; |
|
|
|
} |
|
|
|
rowIndex++; |
|
|
|
} |
|
|
|
|
|
|
|
//自适应单元格 |
|
|
|
for (int i = 0; i < sheet.getLastRowNum(); i++) { |
|
|
|
sheet.autoSizeColumn(i); |
|
|
|
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10); |
|
|
|
} |
|
|
|
|
|
|
|
return workbook; |
|
|
|
} |
|
|
|
|
|
|
|
public static XSSFWorkbook createFourCheckExcel(String fileName,Map<String,Object> currentReportMap,List<Map<String,Object>> reportDetailsList){ |
|
|
|
public static XSSFWorkbook getTwoSheetXSSFWorkbook(String sheetName1,String sheetName2,String []title1,String []title2,String [][]values1,String [][]values2, XSSFWorkbook workbook){ |
|
|
|
// 创建一个工作薄 |
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook(); |
|
|
|
// 创建一个工作表 |
|
|
|
XSSFSheet sheet = workbook.createSheet(fileName); |
|
|
|
workbook = new XSSFWorkbook(); |
|
|
|
|
|
|
|
// 设置表头字体格式 |
|
|
|
XSSFFont headersFont = workbook.createFont(); |
|
|
|
headersFont.setColor(XSSFFont.DEFAULT_FONT_COLOR); |
|
|
|
headersFont.setFontHeightInPoints((short) 26); |
|
|
|
headersFont.setFontHeightInPoints((short) 14); |
|
|
|
headersFont.setBold(true); |
|
|
|
|
|
|
|
// 设置正文字体格式 |
|
|
@ -119,20 +189,6 @@ public class ExcelUtil { |
|
|
|
// 表头内容对齐方式:居中 |
|
|
|
headersStyle.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
|
|
|
|
// 创建标题文本样式 |
|
|
|
XSSFCellStyle titleTextStyle = workbook.createCellStyle(); |
|
|
|
titleTextStyle.setBorderBottom(BorderStyle.THIN); |
|
|
|
titleTextStyle.setBorderRight(BorderStyle.THIN); |
|
|
|
titleTextStyle.setBorderLeft(BorderStyle.THIN); |
|
|
|
titleTextStyle.setBorderTop(BorderStyle.THIN); |
|
|
|
titleTextStyle.setFont(dataSetFont); |
|
|
|
titleTextStyle.setDataFormat(49); |
|
|
|
// 数据内容对齐方式:居中 |
|
|
|
titleTextStyle.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
titleTextStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
titleTextStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); // 设置前景颜色为黄色 |
|
|
|
titleTextStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置前景填充样式 |
|
|
|
|
|
|
|
// 创建文本样式 |
|
|
|
XSSFCellStyle textStyle = workbook.createCellStyle(); |
|
|
|
textStyle.setBorderBottom(BorderStyle.THIN); |
|
|
@ -140,263 +196,85 @@ public class ExcelUtil { |
|
|
|
textStyle.setBorderLeft(BorderStyle.THIN); |
|
|
|
textStyle.setBorderTop(BorderStyle.THIN); |
|
|
|
textStyle.setFont(dataSetFont); |
|
|
|
textStyle.setDataFormat(49); |
|
|
|
// 数据内容对齐方式:居中 |
|
|
|
textStyle.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
textStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
|
|
|
|
XSSFCellStyle bodyStyle = workbook.createCellStyle(); |
|
|
|
bodyStyle.setFont(dataSetFont); |
|
|
|
bodyStyle.setDataFormat(49); |
|
|
|
// 数据内容对齐方式:居中 |
|
|
|
bodyStyle.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
|
|
|
|
//合并单元格 |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3)); |
|
|
|
sheet.addMergedRegion(new CellRangeAddress(5,5,1,3)); |
|
|
|
|
|
|
|
XSSFRow fondsTitleRow = sheet.createRow(0); |
|
|
|
XSSFCell row0cell = fondsTitleRow.createCell(0); |
|
|
|
row0cell.setCellValue("四性检测结果"); |
|
|
|
row0cell.setCellStyle(headersStyle); |
|
|
|
|
|
|
|
XSSFRow row1 = sheet.createRow(1); |
|
|
|
XSSFCell row1cell1 = row1.createCell(0); |
|
|
|
row1cell1.setCellValue("检测范围"); |
|
|
|
row1cell1.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row1cell2 = row1.createCell(1); |
|
|
|
row1cell2.setCellValue("真实性、完整性、可用性、安全性"); |
|
|
|
row1cell2.setCellStyle(textStyle); |
|
|
|
XSSFCell row1cell3 = row1.createCell(2); |
|
|
|
row1cell3.setCellValue("检测结论"); |
|
|
|
row1cell3.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row1cell4 = row1.createCell(3); |
|
|
|
row1cell4.setCellValue((Integer)currentReportMap.get("failNum") == 0 ? "通过" : "不通过"); |
|
|
|
row1cell4.setCellStyle(textStyle); |
|
|
|
|
|
|
|
XSSFRow row2 = sheet.createRow(2); |
|
|
|
XSSFCell row2cell1 = row2.createCell(0); |
|
|
|
row2cell1.setCellValue("检测耗时"); |
|
|
|
row2cell1.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row2cell2 = row2.createCell(1); |
|
|
|
row2cell2.setCellValue(currentReportMap.get("checkTime")+"ms"); |
|
|
|
row2cell2.setCellStyle(textStyle); |
|
|
|
XSSFCell row2cell3 = row2.createCell(2); |
|
|
|
row2cell3.setCellValue("检测总数"); |
|
|
|
row2cell3.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row2cell4 = row2.createCell(3); |
|
|
|
row2cell4.setCellValue(currentReportMap.get("checkNum")==null?"":currentReportMap.get("checkNum")+""); |
|
|
|
row2cell4.setCellStyle(textStyle); |
|
|
|
|
|
|
|
XSSFRow row3 = sheet.createRow(3); |
|
|
|
XSSFCell row3cell1 = row3.createCell(0); |
|
|
|
row3cell1.setCellValue("所属门类"); |
|
|
|
row3cell1.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row3cell2 = row3.createCell(1); |
|
|
|
row3cell2.setCellValue(currentReportMap.get("categoryName")+""); |
|
|
|
row3cell2.setCellStyle(textStyle); |
|
|
|
XSSFCell row3cell3 = row3.createCell(2); |
|
|
|
row3cell3.setCellValue("所在级别"); |
|
|
|
row3cell3.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row3cell4 = row3.createCell(3); |
|
|
|
row3cell4.setCellValue("2".equals(currentReportMap.get("categoryLevel")+"")?"案卷级":"文件级"); |
|
|
|
row3cell4.setCellStyle(textStyle); |
|
|
|
|
|
|
|
XSSFRow row4 = sheet.createRow(4); |
|
|
|
XSSFCell row4cell1 = row4.createCell(0); |
|
|
|
row4cell1.setCellValue("档案档号"); |
|
|
|
row4cell1.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row4cell2 = row4.createCell(1); |
|
|
|
row4cell2.setCellValue(currentReportMap.get("archivesNo")+""); |
|
|
|
row4cell2.setCellStyle(textStyle); |
|
|
|
XSSFCell row4cell3 = row4.createCell(2); |
|
|
|
row4cell3.setCellValue("档案题名"); |
|
|
|
row4cell3.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row4cell4 = row4.createCell(3); |
|
|
|
row4cell4.setCellValue(currentReportMap.get("archivesName")+""); |
|
|
|
row4cell4.setCellStyle(textStyle); |
|
|
|
|
|
|
|
XSSFRow row5 = sheet.createRow(5); |
|
|
|
XSSFCell row5cell1 = row5.createCell(0); |
|
|
|
row5cell1.setCellValue("检测详情"); |
|
|
|
row5cell1.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row5cell2 = row5.createCell(1); |
|
|
|
row5cell2.setCellValue("总共检测 "+currentReportMap.get("checkNum")+" 项;通过 "+currentReportMap.get("passNum")+" 项;未通过 "+currentReportMap.get("failNum")+" 项"); |
|
|
|
row5cell2.setCellStyle(textStyle); |
|
|
|
XSSFCell row5cell3 = row5.createCell(2); |
|
|
|
row5cell3.setCellValue(""); |
|
|
|
row5cell3.setCellStyle(textStyle); |
|
|
|
XSSFCell row5cell4 = row5.createCell(3); |
|
|
|
row5cell4.setCellValue(""); |
|
|
|
row5cell4.setCellStyle(textStyle); |
|
|
|
|
|
|
|
//行 |
|
|
|
int index = 6; |
|
|
|
|
|
|
|
for(int k=1;k<=4;k++){ |
|
|
|
index++; |
|
|
|
XSSFRow row7 = sheet.createRow(index); |
|
|
|
XSSFCell row7cell1 = row7.createCell(0); |
|
|
|
row7cell1.setCellValue("序号"); |
|
|
|
row7cell1.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell2 = row7.createCell(1); |
|
|
|
row7cell2.setCellValue("一级分类"); |
|
|
|
row7cell2.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell3 = row7.createCell(2); |
|
|
|
row7cell3.setCellValue("二级分类"); |
|
|
|
row7cell3.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell4 = row7.createCell(3); |
|
|
|
row7cell4.setCellValue("编号"); |
|
|
|
row7cell4.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell5 = row7.createCell(4); |
|
|
|
row7cell5.setCellValue("检测项目"); |
|
|
|
row7cell5.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell6 = row7.createCell(5); |
|
|
|
row7cell6.setCellValue("检测目的"); |
|
|
|
row7cell6.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell7 = row7.createCell(6); |
|
|
|
row7cell7.setCellValue("检测对象"); |
|
|
|
row7cell7.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell8 = row7.createCell(7); |
|
|
|
row7cell8.setCellValue("检测依据和方法"); |
|
|
|
row7cell8.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell9 = row7.createCell(8); |
|
|
|
row7cell9.setCellValue("检测结果"); |
|
|
|
row7cell9.setCellStyle(titleTextStyle); |
|
|
|
XSSFCell row7cell10 = row7.createCell(9); |
|
|
|
row7cell10.setCellValue("检测详情"); |
|
|
|
row7cell10.setCellStyle(titleTextStyle); |
|
|
|
index++; |
|
|
|
|
|
|
|
int this1 = 1; |
|
|
|
|
|
|
|
for(Map<String,Object> map:reportDetailsList){ |
|
|
|
String detectionCode = map.get("detectionCode")+""; |
|
|
|
if(detectionCode.indexOf("GD-"+k)>-1){ |
|
|
|
XSSFRow row = sheet.createRow(index); |
|
|
|
XSSFCell cell1 = row.createCell(0); |
|
|
|
cell1.setCellValue(this1); |
|
|
|
cell1.setCellStyle(textStyle); |
|
|
|
XSSFCell cell2 = row.createCell(1); |
|
|
|
cell2.setCellValue(map.get("primaryClassId")+""); |
|
|
|
cell2.setCellStyle(textStyle); |
|
|
|
XSSFCell cell3 = row.createCell(2); |
|
|
|
cell3.setCellValue(map.get("secondaryClassId")+""); |
|
|
|
cell3.setCellStyle(textStyle); |
|
|
|
XSSFCell cell4 = row.createCell(3); |
|
|
|
cell4.setCellValue(map.get("detectionCode")+""); |
|
|
|
cell4.setCellStyle(textStyle); |
|
|
|
XSSFCell cell5 = row.createCell(4); |
|
|
|
cell5.setCellValue(map.get("detectionProject")+""); |
|
|
|
cell5.setCellStyle(textStyle); |
|
|
|
XSSFCell cell6 = row.createCell(5); |
|
|
|
cell6.setCellValue(map.get("detectionObjective")+""); |
|
|
|
cell6.setCellStyle(textStyle); |
|
|
|
XSSFCell cell7 = row.createCell(6); |
|
|
|
cell7.setCellValue(map.get("detectionObject")+""); |
|
|
|
cell7.setCellStyle(textStyle); |
|
|
|
XSSFCell cell8 = row.createCell(7); |
|
|
|
cell8.setCellValue(map.get("detectionAccording")+""); |
|
|
|
cell8.setCellStyle(textStyle); |
|
|
|
XSSFCell cell9 = row.createCell(8); |
|
|
|
cell9.setCellValue(map.get("checkResult")+""); |
|
|
|
cell9.setCellStyle(textStyle); |
|
|
|
XSSFCell cell10 = row.createCell(9); |
|
|
|
cell10.setCellValue(map.get("checkDetails")==null?"":map.get("checkDetails")+""); |
|
|
|
cell10.setCellStyle(textStyle); |
|
|
|
|
|
|
|
index++; |
|
|
|
this1++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// for(Map<String,Object> map : list){ |
|
|
|
// XSSFRow fondsValueRow = sheet.createRow(index); |
|
|
|
// XSSFCell fondsValueCell0 = fondsValueRow.createCell(0);fondsValueCell0.setCellValue(index);fondsValueCell0.setCellStyle(textStyle); |
|
|
|
// int columnIndex = 1; |
|
|
|
// for(String t : mark){ |
|
|
|
// XSSFCell fondsValueCell = fondsValueRow.createCell(columnIndex);fondsValueCell.setCellType(CellType.STRING);fondsValueCell.setCellValue(map.get(t)==null?"":map.get(t)+"");fondsValueCell.setCellStyle(textStyle); |
|
|
|
// columnIndex++; |
|
|
|
// } |
|
|
|
// index++; |
|
|
|
// } |
|
|
|
// 创建第一个工作表 |
|
|
|
XSSFSheet sheet1 = workbook.createSheet(sheetName1); |
|
|
|
Integer rowIndex1 = 0; |
|
|
|
XSSFRow thisTitle1 = sheet1.createRow(rowIndex1); |
|
|
|
|
|
|
|
//自适应单元格 |
|
|
|
for (int i = 0; i < sheet.getLastRowNum(); i++) { |
|
|
|
sheet.autoSizeColumn(i); |
|
|
|
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10); |
|
|
|
//创建标题 |
|
|
|
for(int i=0;i<title1.length;i++){ |
|
|
|
XSSFCell cell = thisTitle1.createCell(i); |
|
|
|
cell.setCellValue(title1[i]); |
|
|
|
cell.setCellStyle(headersStyle); |
|
|
|
} |
|
|
|
return workbook; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 导出Excel |
|
|
|
* @param sheetName sheet名称 |
|
|
|
* @param title 标题 |
|
|
|
* @param values 内容 |
|
|
|
* @param wb HSSFWorkbook对象 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){ |
|
|
|
rowIndex1++; |
|
|
|
|
|
|
|
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件 |
|
|
|
if(wb == null){ |
|
|
|
wb = new HSSFWorkbook(); |
|
|
|
//创建内容 |
|
|
|
for(int i=0;i<values1.length;i++){ |
|
|
|
XSSFRow row = sheet1.createRow(i + 1); |
|
|
|
for(int j=0;j<values1[i].length;j++){ |
|
|
|
XSSFCell newCell = row.createCell(j); |
|
|
|
//将内容按顺序赋给对应的列对象 |
|
|
|
newCell.setCellValue(values1[i][j]); |
|
|
|
newCell.setCellStyle(textStyle); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet |
|
|
|
HSSFSheet sheet = wb.createSheet(sheetName); |
|
|
|
|
|
|
|
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制 |
|
|
|
HSSFRow row = sheet.createRow(0); |
|
|
|
|
|
|
|
// 第四步,创建单元格,并设置值表头 设置表头居中 |
|
|
|
HSSFCellStyle style = wb.createCellStyle(); |
|
|
|
style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式 |
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
//自适应单元格 |
|
|
|
for (int i = 0; i < sheet1.getLastRowNum(); i++) { |
|
|
|
sheet1.autoSizeColumn(i); |
|
|
|
sheet1.setColumnWidth(i, sheet1.getColumnWidth(i) * 17 / 10); |
|
|
|
} |
|
|
|
|
|
|
|
//声明列对象 |
|
|
|
HSSFCell cell = null; |
|
|
|
// 创建第二个工作表 |
|
|
|
XSSFSheet sheet2 = workbook.createSheet(sheetName2); |
|
|
|
Integer rowIndex2 = 0; |
|
|
|
XSSFRow thisTitle2 = sheet2.createRow(rowIndex2); |
|
|
|
|
|
|
|
//创建标题 |
|
|
|
for(int i=0;i<title.length;i++){ |
|
|
|
cell = row.createCell(i); |
|
|
|
cell.setCellValue(title[i]); |
|
|
|
cell.setCellStyle(style); |
|
|
|
for(int i=0;i<title1.length;i++){ |
|
|
|
XSSFCell cell = thisTitle2.createCell(i); |
|
|
|
cell.setCellValue(title2[i]); |
|
|
|
cell.setCellStyle(headersStyle); |
|
|
|
} |
|
|
|
|
|
|
|
rowIndex2++; |
|
|
|
|
|
|
|
//创建内容 |
|
|
|
for(int i=0;i<values.length;i++){ |
|
|
|
row = sheet.createRow(i + 1); |
|
|
|
for(int j=0;j<values[i].length;j++){ |
|
|
|
HSSFCell newCell = row.createCell(j); |
|
|
|
for(int i=0;i<values2.length;i++){ |
|
|
|
XSSFRow row = sheet2.createRow(i + 1); |
|
|
|
for(int j=0;j<values2[i].length;j++){ |
|
|
|
XSSFCell newCell = row.createCell(j); |
|
|
|
//将内容按顺序赋给对应的列对象 |
|
|
|
newCell.setCellValue(values[i][j]); |
|
|
|
newCell.setCellStyle(style); |
|
|
|
newCell.setCellValue(values2[i][j]); |
|
|
|
newCell.setCellStyle(textStyle); |
|
|
|
} |
|
|
|
} |
|
|
|
return wb; |
|
|
|
} |
|
|
|
|
|
|
|
//自适应单元格 |
|
|
|
for (int i = 0; i < sheet2.getLastRowNum(); i++) { |
|
|
|
sheet2.autoSizeColumn(i); |
|
|
|
sheet2.setColumnWidth(i, sheet2.getColumnWidth(i) * 17 / 10); |
|
|
|
} |
|
|
|
|
|
|
|
return workbook; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//发送响应流方法 |
|
|
|
public static void setResponseHeader(HttpServletResponse response, String fileName) { |
|
|
|
public static void setResponseHeader(HttpServletRequest request, HttpServletResponse response, String fileName) { |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
fileName = new String(fileName.getBytes(),"UTF-8"); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
// TODO Auto-generated catch block |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
response.setContentType("application/octet-stream;charset=UTF-8"); |
|
|
|
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName,"utf-8")+".xlsx"); |
|
|
|
response.addHeader("Pargam", "no-cache"); |
|
|
|
response.addHeader("Cache-Control", "no-cache"); |
|
|
|
} catch (Exception ex) { |
|
|
|
ex.printStackTrace(); |
|
|
|
response.setCharacterEncoding(request.getCharacterEncoding()); |
|
|
|
response.setContentType("application/octet-stream"); |
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName,request.getCharacterEncoding())+".xlsx"); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |