Excel匯出,特定模板,二維陣列匯出
效果:
@RequestMapping(value = "/exportExcelForSJSB", method = { RequestMethod.GET, RequestMethod.POST })
public void exportExcelForSJSB(
@RequestParam(value = "startMonth", required = false) String startMonth,
@RequestParam(value = "endMonth", required = false) String endMonth,
HttpServletRequest request, HttpServletResponse response, ModelMap model) {
EntityWrapper<Organ> entityWrapper = new EntityWrapper<Organ>();
entityWrapper.eq("ORGANPID", "130000000000");
entityWrapper.orderBy("ORGANORDER");
List<Organ> organList = organServiceImpl.selectList(entityWrapper);
List<String> cityCodeList = new ArrayList<String>();
for(Organ o:organList){
String cityCode = o.getOrganID();
cityCodeList.add(cityCode);
}
String[] typeNames = new String[]{"LY_SLQZJB", "LY_XSQDPXC" ,"LY_SDZFJGYS","LY_QTZNBMYS","LY_QTZNBMYS",//重複了一個目的是讓陣列對齊,與匯出的表格格式一直
"BM_DWHZFJG","BM_XZBM","BM_CLWBZ","BM_QTBM","BM_QTBM",//重複了一個目的是讓陣列對齊
"JB_SCJ","JB_XKJ","JB_CLWBZCY","JB_QT","JB_QT"};//重複了一個目的是讓陣列對齊
int[][] total = new int[cityCodeList.size()+1][15];//建立14行15列陣列
for(int i=0;i<cityCodeList.size();i++){
for(int j=0;j<typeNames.length;j++){
int sum = sjsbMpxsServiceImpl.summaryReportByCityAndType(startMonth, endMonth, cityCodeList.get(i), typeNames[j]);
total[i][j] = sum;
}
}
//遍歷二維陣列
for(int m=0;m<total.length;m++){//控制行數
for(int n=0;n<total[m].length;n++){//一行中有多少個元素(即多少列)
total[m][4] = total[m][0]+total[m][1]+total[m][2]+total[m][3];//來源-小計
total[m][9] = total[m][8]+total[m][5]+total[m][6]+total[m][7];//部門-小計
total[m][14] = total[m][12]+total[m][13]+total[m][10]+total[m][11];//級別-小計
}
}
//計算合計,先遍歷列,再遍歷行
for(int j=0;j<15;j++){
for(int i=0;i<cityCodeList.size();i++){
total[cityCodeList.size()][j] += total[i][j];
}
}
export(request, response, total, "摸排接收涉黑涉惡腐敗和“保護傘”線索情況彙總表", "countReport", 0, 4, 1, 15);
}
private static void export(HttpServletRequest request,HttpServletResponse response,
int[][] result, String fileName,String templateName, int numSheet, int startRow, int xStart, int xEnd){
response.reset();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String dateStr = sdf.format(new Date());
fileName += "-" + dateStr;
try {
fileName = new String(fileName.getBytes(),"ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
// 指定下載的檔名
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// String basePrintPath = (String) request.getSession().getAttribute("basePrintPath");
String basePrintPath = request.getRealPath("") + "/print/";
String template = basePrintPath + templateName + ".xls";
@SuppressWarnings("unchecked")
HSSFWorkbook exportExcel = generateExcelForSJSB(result, template, numSheet, startRow, xStart, xEnd);
try {
exportExcel.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
private static HSSFWorkbook generateExcelForSJSB(int[][] dataArray, String template,// 模板路徑
int numSheet,// 第幾個工作簿
int startRow,// 開始行號,即記錄變數的行號。起始為0
int xStart, int xEnd) {
InputStream is = null;
HSSFWorkbook excel = null;
try {
is = new FileInputStream(template);
excel = new HSSFWorkbook(is);
HSSFSheet xssfSheet = excel.getSheetAt(numSheet);
HSSFRow row = xssfSheet.getRow(startRow);
for(int m=0;m<dataArray.length;m++){
HSSFRow tempRow = xssfSheet.getRow(startRow + m);
for(int n=0;n<dataArray[m].length;n++){
if (row.getCell(n+1) != null) {
HSSFCell cell = row.getCell(n+1);
HSSFCell c = tempRow.createCell((short) (n+1));
HSSFCellStyle cellStyle= cell.getCellStyle();
cellStyle.setWrapText(true);
c.setCellStyle(cellStyle);
c.setCellValue(dataArray[m][n]);
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return excel;
}