一、效果如下
二、js代码
function export_word(){ //导出word
var url = "czzsca/exportWord.do";
this.export(url);
}
function export(url){
var currentyear = $("#mainYear").val() * 1;
var key_columns = ['xh',"d_name","d_unit","d_number","d_estimate","d_lastyear","jnjh","d_remarks","d_depart","d_executeunit"];
var value_columns = ['序号','项目名称','单位','数量','总概算','至'+(currentyear-1)+'年完成',currentyear+'年计划','附注','责任部门','计划执行单位'];
window.location.href= url+"?key_columns="+key_columns+"&value_columns="+value_columns+"&title="+currentyear+"年 更新改造计划 正式计划草案";
}
三、controller代码
(controller代码比较长,主要是格式化每一行的数据,主要是往工具类里传List>参数,泛型的List是每一行,String是每一列)
//导出word
@RequestMapping("/exportWord.do")
@ResponseBody
public ReturnMsg exportWord(String[] key_columns,String[] value_columns,String title,HttpServletResponse response){
ReturnMsg rm = new ReturnMsg();
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/msexcle");
response.setHeader("content-disposition", "attachment;filename="+new String("正式计划草案".getBytes("gb2312"),"ISO8859-1")+".doc");
List> content = new ArrayList>();
List> head = new ArrayList>();
String jnjhzh = new BigDecimal(this.map.get("jnjhzh")==null||"".equals(this.map.get("jnjhzh")+"")?"0":this.map.get("jnjhzh")+"").setScale(1, RoundingMode.UP)+"";
String trimpro = new BigDecimal(this.map.get("tzjh")==null||"".equals(this.map.get("tzjh")+"")?"0":this.map.get("tzjh")+"").setScale(1, RoundingMode.UP)+"";
String temp = new BigDecimal(trimpro).subtract(new BigDecimal(jnjhzh))+"";
if("0".equals(temp)) {
temp = "";
}
head = ExportUtil.getCzzscaList(jnjhzh, trimpro, temp);
Map dounit = new HashMap();
dounit.put("dwlx", 0);
dounit.put("user_type", 1);
List d_executeunit = unitDao.selectByPrimaryKey(dounit);
Map zrbm = new HashMap();
zrbm.put("dwlx", 1);
zrbm.put("user_type", 1);
List d_zrbm = unitDao.selectByPrimaryKey(zrbm);
List zmtype = nd_caController.findProType("2-3-4");
for (Map map: this.list) {
List row = new ArrayList();
for (int i = 0; i < key_columns.length; i++) {
System.out.print(map.get(key_columns[i])+",");
Object Otzjh = map.get("tzjh")!=null?map.get("tzjh"):0;
Object Ojnjh = map.get("jnjh")!=null?map.get("jnjh"):0;
BigDecimal tzjh = null;
BigDecimal jnjh = null;
if ("tzjh".equals(key_columns[i])||"jnjh".equals(key_columns[i])||"d_lnorde".equals(key_columns[i])) {
if(Otzjh instanceof BigDecimal) {
tzjh = (BigDecimal)Otzjh;
}else {
tzjh = new BigDecimal(Otzjh+"");
}
if(Ojnjh instanceof BigDecimal) {
jnjh = (BigDecimal)Ojnjh;
}else {
jnjh = new BigDecimal(Ojnjh+"");
}
}
if ("d_lnorde".equals(key_columns[i])) {
if(map.get("flag")!=null&&"2".equals((map.get("flag")+""))) {
row.add("");
}else {
row.add(tzjh.subtract(jnjh).setScale(1, RoundingMode.UP)+""); //不为整数则四舍五入
}
}else if ("xh".equals(key_columns[i])&&map.get("xh")!=nu