下面是Java代码:
Dao层:我这里面参数有4个分别是:begindate(开始时间),enddate(结束时间),organizeid(ID),key,方法名:getalsbybelong
Map<String,String> getalsbybelong(@Param("begindate")String begindate,@Param("enddate")String enddate,@Param("organizeid")String organizeid,@Param("key")String key);
Service层:
@Autowired
private Will2Dao dao;
public void excelImportss(HttpServletRequest request, HttpServletResponse response, String begindate,
String enddate, String organizeid,String key) throws IOException {
Map<String, String> data = this.dao.getalsbybelong( begindate, enddate,organizeid,key);
String fileurl = request.getSession().getServletContext().getRealPath("/") + "jsp/util/szxzwhkzjdb.xls";
System.out.println(data.toString());
excelImportDatasss(fileurl, data, response, begindate, enddate,organizeid,key);
}
public static void excelImportDatasss(String filurl, Map<String, String> list, HttpServletResponse response,
String begindate,String enddate,String organizeid,String key) throws IOException {
File excel2 = new File(filurl);
FileInputStream is = new FileInputStream(excel2);
HSSFWorkbook excel = new HSSFWorkbook(is);
Sheet sheet = excel.getSheetAt(0);
Object obj = null;
Row rowa = sheet.getRow(1);
if (rowa == null) {
rowa = sheet.createRow(1);
}
Cell tempcells = rowa.getCell(5);
if (tempcells == null) {
tempcells = rowa.createCell(5);
}
tempcells.setCellValue(begindate+"-"+enddate);
rowa = sheet.getRow(1);
if (rowa == null) {
rowa = sheet.createRow(1);
}
tempcells = rowa.getCell(1);
if (tempcells == null) {
tempcells = rowa.createCell(1);
}
tempcells.setCellValue(key);
if (list != null && list.size() > 0) {
Row row = sheet.getRow(3);
if (row == null) {
row = sheet.createRow(2);
}
Cell tempcell = row.getCell(3);
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("ZWHZZ");
System.out.println(list.toString());
tempcell.setCellValue(obj.toString());
row = sheet.getRow(3);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(3);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("ZWHZZNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(4);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(4);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("GLXSHY");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(4);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(4);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("GLXSHYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(5);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(5);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("QTHY");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(5);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(5);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("QTHYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(6);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(6);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("JOINSTUDYSTATENUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(6);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(6);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("STUDYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(7);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(7);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("INVESTIGATION");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(7);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(7);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("RDYNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(8);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(8);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("INSPECTION");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(8);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(8);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("RSCNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(9);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(9);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("INVESTIGATE");
tempcell.setCellValue(obj.toString());
/*
if (row == null) {
row = sheet.createRow(9);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
row = sheet.getRow(9);
tempcell = row.getCell(5);
obj = list.get("RKCNUM");
tempcell.setCellValue(obj.toString());*/
row = sheet.getRow(10);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(10);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("DENUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(10);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(10);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("DERNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(11);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(11);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("TANUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(11);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(11);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("TARNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(12);
tempcell = row.getCell(2);
if (row == null) {
row = sheet.createRow(12);
}
if (tempcell == null) {
tempcell = row.createCell(2);
}
obj = list.get("QTNUM");
tempcell.setCellValue(obj.toString());
row = sheet.getRow(12);
tempcell = row.getCell(5);
if (row == null) {
row = sheet.createRow(12);
}
if (tempcell == null) {
tempcell = row.createCell(5);
}
obj = list.get("QTRNUM");
tempcell.setCellValue(obj.toString());
}
sheet.setForceFormulaRecalculation(true);
try {
OutputStream os = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename="
+ new String(URLEncoder.encode("省政协专委会组织委员开展活动情况季度统计表", "utf-8").getBytes("UTF-8"), "ISO-8859-1")
+ ".xls");
response.setContentType("application/msexcel");
excel.write(os);
os.close();
excel.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Action层:
@Autowired
private TongTj tongJi;
@RequestMapping(value ="importadvisers.json")
@ResponseBody
public ResultObj importadvisers(HttpServletRequest request,HttpServletResponse response,String organizeid,String begindate,String enddate,String key ){
try {
ResultObj resultobj = new ResultObj(SUCCESS);
tongJi.excelImportss(request,response,begindate,enddate,organizeid,key);
return resultobj;
} catch (Exception e) {
e.printStackTrace();
ResultObj resultobj = new ResultObj(FAIL);
return resultobj;
}
}
页面JS:
function excelImport(){
var beginDate = $("#years1").val()+$("#monthes1").val();
//alert(beginDate);
var endDate = $("#years2").val()+$("#monthes2").val();
//alert(endDate);
if(parseInt($("#monthes1").val())<10){
beginDate = $("#years1").val()+"0"+$("#monthes1").val();
}
if (parseInt($("#monthes2").val())<10) {
endDate = $("#years2").val()+"0"+$("#monthes2").val();
}
var organizeid=$("#belongquere").val();
var key= $("#belongquere").find("option:selected").text()
//alert(key);
var data = {};
data['begindate']=beginDate;
//alert(beginDate);
data['enddate']=endDate;
//alert(endDate);
/* data['flog'] = flogs;
data['type'] = '1'; */
data['organizeid'] = organizeid;
data['key']=key;
//alert(organizeid);
download('adviserqueryaction/importadvisers.json',data);
}