以springMVC为例:
1.controller:
/**
* 导出
* @param request
* @param response
*/
@RequestMapping("/exportDiscountRatioExcel")
public void exportDiscountRatioExcel(HttpServletRequest request,HttpServletResponse response)
{
Map paramsMap = getPageParams(request);
try {
Workbook wb = manager.exportExcel(request, paramsMap);
String excelName = java.net.URLEncoder.encode("价格监测-车型折扣对比", "UTF-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+excelName+".xls" );
ServletOutputStream out = response.getOutputStream();
wb.write(out);
//先把EXCEL写到临时目录,用来获取文件大小,最后删除
File f = new File(request.getSession().getServletContext().getRealPath("/") + "/demoExcel/demo.xls");
if(!f.exists())
{
f.createNewFile();
}
BufferedOutputStream s = new BufferedOutputStream(new FileOutputStream(f));
wb.write(s);
//关闭流
s.flush();
s.close();
out.flush();
out.close();
//记录导出日志,并删除临时文件
paramsMap.put("exportSize", AppFrameworkUtil.getNum(f.length()/1024, 0));
logManager.updateModuleLog(paramsMap);
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
2.serviceImpl:
/**
* 导出
*/
@Override
public Workbook exportExcel(HttpServletRequest request,Map paramsMap)
{
Workbook wb = null;
String json = (String) request.getSession().getAttribute(Constant.getExportExcelDataKey);
String languageType = (String) paramsMap.get("languageType");//导出语言
if(!AppFrameworkUtil.isEmpty(json))
{
JSONObject obj = (JSONObject) JSONObject.parse(json);
JSONArray gridObj = (JSONArray) obj.getJSONArray("grid");
try {
//读取模块路径
String path = request.getSession().getServletContext().getRealPath("/");
wb = new HSSFWorkbook(new FileInputStream(new File(path+"excelExample/discountRatio.xls")));
//导图
exportLineChartExcel(wb,wb.getSheet("DATA"),gridObj,languageType);
//导表格
exportOriginalData(wb,wb.getSheet("原数据"),gridObj,languageType,paramsMap);
} catch (Exception e) {
e.printStackTrace();
}
}
return wb;
}
/**
* 导出原始数据
* @param wb
* @param s
* @param gridObj
* @param paramsMap
*/
public void exportOriginalData(Workbook wb,Sheet s,JSONArray gridObj,String languageType,Map paramsMap)
{
String[] titles = null;//标题数组
if("EN".equals(languageType))
{
titles = new String[]{"Date","Code","Segment","Manufacture","Model","Engine Capacity","Transmission","Trim","Bodytype","Launch Date","Model Year","MSRP(RMB)"
,"TP(RMB)","Discount(RMB)","Discount Rate","VS Month","VS Change"};
}
else
{
titles = new String[]{"日期","车型编码","级别","厂商","车型","排量","排档方式","型号标识","车身形式","上市日期","年式","厂商指导价"
,"最低参考价加权值","折扣(元)","折扣率","VS上月","VS变化"};
}
int rowIndex = 0;//行号锁引
int cellIndex = 0;//列锁引
Row row = ExportExcelUtil.createRow(s, rowIndex, 400);
CellStyle titleStyle = ExportExcelUtil.getExcelTitleBackgroundStyle(wb);//表格标题样式
CellStyle textStyle = ExportExcelUtil.getExcelFillTextStyle(wb);//内容文本样式
CellStyle thousandthStyle = ExportExcelUtil.getExcelFormatThousandthStyle(wb);//格式化千分位样式
CellStyle percentageStyle = ExportExcelUtil.getExcelFillPercentageStyle(wb);//格式化百分号样式
//保存线图刻度最大值与最小值,供数据部使用
Cell cell = row.createCell(40);
ExportExcelUtil.setCellValueAndStyle(cell, AppFrameworkUtil.getNum(paramsMap.get("ymax").toString(), 0)+".0~", textStyle);
cell = row.createCell(41);
ExportExcelUtil.setCellValueAndStyle(cell, AppFrameworkUtil.getNum(paramsMap.get("ymin").toString(), 0)+".0~", textStyle);
cell = row.createCell(42);
ExportExcelUtil.setCellValueAndStyle(cell, languageType, textStyle);
//写标题
for(String title : titles)
{
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, title, titleStyle);
s.setColumnWidth(cell.getColumnIndex(), 3000);
}
//写数据
for(int i = 0; i < gridObj.size(); i++)
{
JSONObject obj = gridObj.getJSONObject(i);
row = ExportExcelUtil.createRow(s, ++rowIndex, 400);
cellIndex = 0;
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, obj.getString("yearMonth")+"~", textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, obj.getString("versionCode")+"~", textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, getValueByLanguageType(obj,languageType,"gradeName"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, getValueByLanguageType(obj,languageType,"manfName"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, getValueByLanguageType(obj,languageType,"subModelName"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, obj.getString("discharge"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, getValueByLanguageType(obj,languageType,"gearMode"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, getValueByLanguageType(obj,languageType,"typeId"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, getValueByLanguageType(obj,languageType,"bodyType"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, obj.getString("versionLaunchDate"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, obj.getString("modelYear"), textStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, obj.getString("msrp"), thousandthStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, AppFrameworkUtil.getNum(obj.getString("tp"),7), thousandthStyle);
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, AppFrameworkUtil.getNum(obj.getString("discount"),7), thousandthStyle);
cell = row.createCell(cellIndex++);
if("".equals(obj.getString("discountRate")) || "-".equals(obj.getString("discountRate"))){
ExportExcelUtil.setCellValueToPercentage(cell, "-", percentageStyle);
} else{
ExportExcelUtil.setCellValueToPercentage(cell, obj.getString("discountRate"), percentageStyle);
}
cell = row.createCell(cellIndex++);
ExportExcelUtil.setCellValueAndStyle(cell, AppFrameworkUtil.getNum(obj.getString("vsDiscount"),7), thousandthStyle);
cell = row.createCell(cellIndex++);
if("".equals(obj.getString("vsDiscountRate")) || "-".equals(obj.getString("vsDiscountRate"))){
ExportExcelUtil.setCellValueToPercentage(cell, "-", percentageStyle);
} else{
ExportExcelUtil.setCellValueToPercentage(cell, obj.getString("vsDiscountRate"), percentageStyle);
}
}
s.setDisplayGridlines(false);
}