java导出导入

jar包引入

poi 3.16

poi-ooxml 3.16

poi-ooxml-schemas 3.16

commons-fileupload 1.3.1

commons-io 2.4

导出:

1前端用get请求,如window.location=""

/**导出*/

@RequestMapping(value ="/daoChu")

@ResponseBody

public void daoChu(HttpServletResponse response,@RequestParam(required =false) String surveyedName, @RequestParam(required =false) String surveyedIdcard, @RequestParam(defaultValue ="0") Integer investigationType, @RequestParam(required =false) String investigationName, @RequestParam(required =false, defaultValue ="1") Integer pageNumber, @RequestParam(required =false, defaultValue ="5") Integer pageSize) {

HSSFWorkbook hwb =new HSSFWorkbook();

    SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddhhmmss");

    String pattern = sdf.format(new Date());

    HSSFSheet sheet = hwb.createSheet("考察" + pattern);

    sheet.setDefaultColumnWidth((short)15);

    HSSFCellStyle style = hwb.createCellStyle();

    HSSFRow row = sheet.createRow(0);

    String str ="被考察人姓名,被考察人身份证,被考察人单位,被考察人地区,考察类型,考察人姓名,考察人职位,考察内容,创建时间";

    String[] heads = str.split(",");

    if (null != heads && heads.length >0) {

for(int i=0;i

row.createCell(i).setCellValue(heads[i]);

        }

}

//添加数据

    Grid grid =investigationService.pageListInvestigationBean(surveyedName, surveyedIdcard, investigationType, investigationName, pageNumber, pageSize);

    List list = grid.getRows();

    for(int i=0;i

row = sheet.createRow(i+1);

        row.createCell(0).setCellValue(list.get(i).getSurveyedName());

        row.createCell(1).setCellValue(list.get(i).getSurveyedIdcard());

        row.createCell(2).setCellValue(list.get(i).getSurveyedCompany());

        row.createCell(3).setCellValue(list.get(i).getSurveyedArea());

        row.createCell(4).setCellValue(list.get(i).getInvestigationType());

        row.createCell(5).setCellValue(list.get(i).getInvestigationName());

        row.createCell(6).setCellValue(list.get(i).getInvestigationPosition());

        row.createCell(7).setCellValue(list.get(i).getInvestigationContent());

        row.createCell(8).setCellValue(list.get(i).getTime());

    }

OutputStream output;

    try {

output = response.getOutputStream();

        response.reset();

        response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode("考察", "UTF-8") +".xls");

        response.setContentType("application/msexcel");

        hwb.write(output);

        output.close();

    }catch (IOException e) {

e.printStackTrace();

    }

}

导入:前端form加入method="post" enctype="multipart/form-data"

/**导入*/

@RequestMapping(value ="/daoRu")

@ResponseBody

public void daoRu(HttpServletRequest request) {

CommonsMultipartResolver cmr =new CommonsMultipartResolver(request.getSession().getServletContext());

    MultipartFile file =null;

    if (cmr.isMultipart(request)){

MultipartHttpServletRequest msr = (MultipartHttpServletRequest) request;

        Iterator iter = msr.getFileNames();

        if (iter.hasNext()){

file = msr.getFile(iter.next());

        }

}

if (file ==null)return;

    List> data =new ArrayList>();

    FileUtil.ExcelFileToList(file, data);

    if (null != data && data.size() >0) {

for (int i=1;i

List obj = data.get(i);

            if (null != obj && obj.size() >0) {

for (Object o : obj) {

if (null == o) {

return;

                    }

}

InvestigationBean investigationBean =new InvestigationBean();

                investigationBean.setSurveyedName(obj.get(0).toString());

                investigationBean.setSurveyedIdcard(obj.get(1).toString());

                investigationBean.setSurveyedCompany(obj.get(2).toString());

                investigationBean.setSurveyedArea(obj.get(3).toString());

                investigationBean.setInvestigationType(Long.parseLong(obj.get(4).toString()));

                investigationBean.setInvestigationName(obj.get(5).toString());

                investigationBean.setInvestigationPosition(obj.get(6).toString());

                investigationBean.setInvestigationContent(obj.get(7).toString());

                investigationBean.setTime(obj.get(8).toString());

                investigationService.saveInvestigation(investigationBean);

            }

}

}

}

你可能感兴趣的:(java导出导入)