码迷,mamicode.com
首页 > 其他好文 > 详细

excel导入

时间:2017-01-17 14:19:13      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:java

angulis.js:

$upload.upload({

  url: url,

  params: {

  //providerid: $scope.institutionDetail.providerCode

},

  file: $scope.files

  }).success(function (response) {


                //todo

            })

.error(function (r) {

                //todo

})


或者用ajax也行,主要是用于提交一个文件。


controler:

    

@SecurityAccess(menuNodes = "", open = true)

@RequestMapping(value = "/hcpms/madagascar/uploadSqlParameterExcel.do", method = RequestMethod.POST)

@ResponseBody

public IntegrityResponseBean uploadParameterExcel(

@ModelAttribute InterityBlackAddRequestVO vo,

HttpServletRequest request) {

    //获取文件信息:

    List<MultipartFile> files = getParameterExcelFileList(request);

    //封装为自己的DTO

    List<HcpmsBiEtlJobParamsDTO> list = this.findParameterExcelHcpmsBiEtlSqlDTO(files, user);     



}    

    

    1、findParameterExcelHcpmsBiEtlSqlDTO 方法:

    

    

private List<HcpmsBiEtlJobParamsDTO> findParameterExcelHcpmsBiEtlSqlDTO(

List<MultipartFile> files, UserDetails user) {


List<HcpmsBiEtlJobParamsDTO> list = new ArrayList<HcpmsBiEtlJobParamsDTO>();


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


for (MultipartFile file : files) {

parseParameterExcelToDTO(file, list, user);

}

}


return list;

}


    


    2、getParameterExcelFileList方法:

private List<MultipartFile> getParameterExcelFileList(

HttpServletRequest request) {

MultipartResolver resolver = new CommonsMultipartResolver(request

.getSession().getServletContext());

MultipartHttpServletRequest multiRequest = resolver

.resolveMultipart(request);

List<MultipartFile> files = multiRequest.getFiles("file");


return files;

}

    3、parseParameterExcelToDTO方法:

private void parseParameterExcelToDTO(MultipartFile file,

List<HcpmsBiEtlJobParamsDTO> list, UserDetails user) {

String username = user.getUsername();

InputStream is = null;


try {

is = file.getInputStream();

String fileName = file.getOriginalFilename();

String fileType = fileName.substring(fileName.lastIndexOf(".") + 1,

fileName.length());

if ("xlsx".equals(fileType)) {

XSSFWorkbook wb = new XSSFWorkbook(is);

XSSFSheet childSheet = wb.getSheetAt(0);

// readExcel

readParameterExcelToBean(childSheet, username, list);

} else if ("xls".equals(fileType)) {

HSSFWorkbook wb = new HSSFWorkbook(is);

HSSFSheet childSheet = wb.getSheetAt(0);

// readExcel

readParameterExcelToBean(childSheet, username, list);

}

} catch (Exception e) {

HttpProcessUtil.processException(e);

} finally {

try {

if (is != null) {

is.close();

}

} catch (IOException e) {

}

}

}

    4、readParameterExcelToBean方法:

    

private void readParameterExcelToBean(HSSFSheet childSheet,

String username, List<HcpmsBiEtlJobParamsDTO> list) {

int rowNum = childSheet.getLastRowNum();

// j=1 跳过head部分

for (int j = 1; j <= rowNum; j++) {

HSSFRow row = childSheet.getRow(j);

HcpmsBiEtlJobParamsDTO currentBean = this.getParameterDTOFromRow(

row, username);

if (null != currentBean) {

list.add(currentBean);

}

}

}

    5、getParameterDTOFromRow方法:

private HcpmsBiEtlJobParamsDTO getParameterDTOFromRow(XSSFRow row,

String opertionUser) {


if (row == null) {

return null;

}


HcpmsBiEtlJobParamsDTO bean = new HcpmsBiEtlJobParamsDTO();

int cellNum = row.getLastCellNum(); // 每行有多少列(格)

for (int k = 0; k < cellNum; k++) {

if (row.getCell(k) == null) {

continue;

}


String value = row.getCell(k).toString();

if (!StringUtils.hasText(value)) {

continue;

}


// setBeanFir

setParameterBeanFir(bean, value, k);


}

return bean;

}

    6、setParameterBeanFir方法:

private void setParameterBeanFir(HcpmsBiEtlJobParamsDTO bean, String value,

int k) {


if (k == 0) { //

bean.setParamsType(value);

}

if (k == 1) { //

bean.setJobCode(value);

}

if (k == 2) { //

bean.setParamsName(value);

}

if (k == 3) { //

bean.setParamsValue(value);

}

if (k == 4) { //

bean.setParamsInfo(value);

}

if (k == 5) { //

bean.setIsValid(value);

}

}

    7、HcpmsBiEtlJobParamsDTO 实体类:

public class HcpmsBiEtlJobParamsDTO extends BaseDTO  {

private static final long serialVersionUID = 1L;

// VARCHAR2(2) idEtlParameter

private String idEtlParameter;

// VARCHAR2(2) 参数类型(0:公有,1:私有)

private String paramsType;

// VARCHAR2(20)  JOB编码

private String jobCode;

// VARCHAR2(20) 参数名称

private String paramsName;

// VARCHAR2(1024)  参数值(符合PL/SQL格式)

private String paramsValue;

// VARCHAR2(2) N 是否有效[0-有效,1-无效]

private String isValid;

// VARCHAR2(200) 参数说明

private String paramsInfo;

// VARCHAR2(100) N 创建人

private String createdBy;

// DATE N SYSDATE 创建时间

private Date dateCreated;

// VARCHAR2(100) N 修改人

private String updatedBy;

// DATE N SYSDATE 修改时间

private Date dateUpdated;

}

excel导入

标签:java

原文地址:http://tianjian.blog.51cto.com/3549910/1892584

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!