//新建一个工具类
package com.controller;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.web.servlet.view.document.AbstractExcelView;
@SuppressWarnings({ "deprecation", "unused" })
public class ExcelView extends AbstractExcelView {
@SuppressWarnings("deprecation")
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 设置列标题字体,样式
HSSFFont hssf_titleFont = workbook.createFont();
hssf_titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
hssf_titleFont.setFontHeightInPoints((short) 9);// 设置字体大小
hssf_titleFont.setColor(HSSFColor.WHITE.index);
HSSFCellStyle hssf_titleStyle = workbook.createCellStyle();
hssf_titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 设置边框
hssf_titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
hssf_titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
hssf_titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
hssf_titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
hssf_titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
hssf_titleStyle.setFont(hssf_titleFont);
//1黄色2红色3绿色4蓝色
hssf_titleStyle.setFillForegroundColor((short) 4);
hssf_titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
hssf_titleStyle.setWrapText(true);//设置自动换行
// 内容列样式
HSSFCellStyle hssf_contentStyle = workbook.createCellStyle();
hssf_contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
hssf_contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
hssf_contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
hssf_contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
hssf_contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
hssf_contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
hssf_contentStyle.setWrapText(true);//设置自动换行
@SuppressWarnings("unchecked")
List<Object> dataSet = (List<Object>) model.get("dataSet");
String[] keys = (String[])model.get("keys");
String[] titles = (String[])model.get("titles");
String fileName = (String)model.get("fileName");
boolean flag = (Boolean)model.get("flag");
HSSFSheet sheet = workbook.createSheet("退款订单");
if(!flag){
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0); // 新建一个单元格
cell.setCellValue("导出退款结算单异常");
cell.setCellStyle(hssf_contentStyle);//设置内容样式
}else{
sheet.setColumnWidth(0, 80*100);
sheet.setColumnWidth(1, 50*100);
sheet.setColumnWidth(2, 60*100);
sheet.setColumnWidth(3, 50*100);
// sheet.setColumnWidth(4, 40*100);
// sheet.setColumnWidth(5, 40*100);
// sheet.setColumnWidth(6, 60*100);
// sheet.setColumnWidth(7, 40*100);
// sheet.setColumnWidth(8, 100*100);
// sheet.setColumnWidth(9, 50*100);
// sheet.setColumnWidth(10, 40*100);
// sheet.setColumnWidth(11, 40*100);
// sheet.setColumnWidth(12, 70*100);
// sheet.setColumnWidth(13, 50*100);
for (int i = 0; i < titles.length; i++) {
@SuppressWarnings("deprecation")
HSSFCell cell = getCell(sheet,0,i);
cell.setCellStyle(hssf_titleStyle);//设置标题样式
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
setText(cell, titles[i]);
}
for (int i = 0; i < dataSet.size(); i++) {
HSSFRow row = sheet.createRow(i+1);
Object obj = dataSet.get(i);
// 处理列
for(int j=0; j < keys.length; j++) {
@SuppressWarnings("deprecation")
HSSFCell cell = row.createCell((short) j); // 新建一个单元格
cell.setCellValue(getFieldValueByMapKey(keys[j], obj));
cell.setCellStyle(hssf_contentStyle);//设置内容样式
}
}
}
fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1") + ".xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
@SuppressWarnings("rawtypes")
protected String getFieldValueByMapKey(String fieldName, Object obj) {
try {
Map objMap = (Map) obj;
Object value = objMap.get(fieldName);
if(value!=null && !"".equals(value)){
return value.toString();
}else{
return "";
}
} catch (Exception e) {
}
return "";
}
}
//调用
@RequestMapping("/exportExcel")
public ModelAndView exportExcel(ModelMap model) throws Exception{
boolean flag = true;
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
Map<String, Object> m = new HashMap<String, Object>();
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timers=format.format(date);
m.put("PAYORDERNO", "1");
m.put("PARTNERNAME", "张三");
m.put("PARTNERNUM", "22");
m.put("MERCHANTNAME", timers);
list2.add(m);
String[] keys = new String[]{"PAYORDERNO","PARTNERNAME","PARTNERNUM","MERCHANTNAME"};
String[] titles = new String[]{"订单号","付款人姓名","付款人手机号","商户名称"};
String fileName ="退款结算报表";
ExcelView excelView = new ExcelView();
model.put("dataSet", list2);
model.put("keys", keys);
model.put("titles", titles);
model.put("fileName", fileName);
model.put("flag", flag);
return new ModelAndView(excelView, model);
}