一、前端js
var str = $('#dd').datebox('getValue')
var data = new Date(str);
var time = data.myTime(2);
$.post("/rap/export/excel?time="+time+"&type=3",function(data){
if(data=="fail"){
alert("导出失败,请联系管理员!")
}else if(data==""){
alert("这月没有填写月总结")
}else{
window.location.href="/rap/downImport/xiazai/"+data;
}
})
二、controller类
ExportExcel excel;
@RequestMapping("/excel")
@ResponseBody
public String excel(HttpServletRequest request,HttpServletResponse response) {
String time = PubFunc.toString(request.getParameter("time"));
String type = PubFunc.toString(request.getParameter("type"));
String val = "fail";
excel = new ExportExcel();
String f_1267 = "";
String f_1268 = "";
String[] split = time.split("/");
f_1267 = split[0];
f_1268 = split[1];
String fileName = f_1267+"年"+f_1268+"月总结.xls";
String path = System.getProperty("web.root")+ "upload"+File.separator+fileName;
//String path = "D:\\"+fileName;
String table = "编制人,年份,月份,任务名称,工作目标,工作成果,工作目的,开始日期,结束日期,负责人,配合人,是否完成,完成情况总结";
String sheet1 = "月总结";
String sql ="SELECT DISTINCT f_1266,f_1267,f_1268,f_1269,f_1270,f_1271,f_1272,f_1273,f_1274,f_1275,f_1276,f_1277,f_1278 "
+"FROM c97_yuejhyzj WHERE 1=1 AND f_1267 = '"+f_1267+"' AND f_1268 = '"+f_1268+"'";
List<Record> find = Db.find(sql);
if (find.size() == 0) {
val = "";
return val;
}
List<Map<String, Object>> ListMap = new ArrayList<Map<String, Object>>();
for (Record record : find) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("编制人", record.get("f_1266"));
map.put("年份", record.get("f_1267"));
map.put("月份", record.get("f_1268"));
map.put("任务名称", record.get("f_1269"));
map.put("工作目标", record.get("f_1270"));
map.put("工作成果", record.get("f_1271"));
map.put("工作目的", record.get("f_1272"));
map.put("开始日期", record.get("f_1273"));
map.put("结束日期", record.get("f_1274"));
map.put("负责人", record.get("f_1275"));
map.put("配合人", record.get("f_1276"));
map.put("是否完成", record.get("f_1277"));
map.put("完成情况总结", record.get("f_1278"));
ListMap.add(map);
}
try {
excel.outExcel(path , table , sheet1 , ListMap );
val = fileName;
} catch (Exception e) {
val = "fail";
}
return val;
}
三、ExportExcel类
package pub.excel;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import pub.PubFunc;
public class ExportExcel {
public void outExcel(String path,String headerTitle,String sheet1,List<Map<String,Object>> dataList){
// 第一步,创建一个workbook,对应一个Excel文件
Workbook wb = null;
if(path.endsWith(".xls")){
wb = new HSSFWorkbook();
}else if(path.endsWith(".xlsx")){
wb = new XSSFWorkbook();
}else{
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
Sheet sheet = wb.createSheet(sheet1);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
Row row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
CellStyle style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
// 生成一个字体
Font font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
CellStyle style1 = wb.createCellStyle();
style1.setAlignment(CellStyle.ALIGN_CENTER); // 创建一个居中格式
style1.setFillForegroundColor(HSSFColor.BLACK.index);
style1.setFillPattern(CellStyle.SOLID_FOREGROUND);
style1.setBorderBottom(CellStyle.BORDER_THIN);
style1.setBorderLeft(CellStyle.BORDER_THIN);
style1.setBorderRight(CellStyle.BORDER_THIN);
style1.setBorderTop(CellStyle.BORDER_THIN);
// 生成一个字体
Font font1 = wb.createFont();
font1.setFontHeightInPoints((short) 12);
//font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style1.setFont(font1);
Cell cell = null;
int count=0;
String[] header_title = headerTitle.split(",");
for (int i=0;i<header_title.length;i++){
cell = row.createCell((short) count);
cell.setCellValue(header_title[i]);
cell.setCellStyle(style);
count++;
}
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < dataList.size(); i++){
row = sheet.createRow((int) i+1);
Map<String,Object> dataMap = dataList.get(i);
//System.out.println("dataMap="+dataMap);
for(int j=0;j<header_title.length;j++){
cell = row.createCell((short) j);
cell.setCellValue(filterDangerString(PubFunc.toString(dataMap.get(header_title[j].toLowerCase()))));
}
}
for (int i=0;i<header_title.length;i++){
sheet.setColumnWidth(i,header_title[i].getBytes().length*2*256);
}
// 第六步,将文件存到指定位置
try{
FileOutputStream fout = new FileOutputStream(path);
wb.write(fout);
fout.close();
}
catch (Exception e){ }
}
public String filterDangerString(String value){
if(value==null){
return null;
}
value=value.replaceAll("<","<");
value=value.replaceAll(">",">");
value=value.replaceAll("'","\'");
return value;
}
}