Newer
Older
EMS-WEB-3.0 / src / main / java / com / casic / accessControl / xls / ReadColumnFromExcel.java
wxn on 9 Aug 2016 8 KB first commit
//package com.casic.accessControl.xls;
//
//import com.casic.accessControl.shp.dto.MetaInfoDTO;
//import org.apache.poi.hssf.usermodel.HSSFCell;
//import org.apache.poi.hssf.usermodel.HSSFRow;
//import org.apache.poi.hssf.usermodel.HSSFSheet;
//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.apache.poi.xssf.usermodel.XSSFCell;
//import org.apache.poi.xssf.usermodel.XSSFRow;
//import org.apache.poi.xssf.usermodel.XSSFSheet;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * Created by Administrator on 2015/9/1.
// */
//public class ReadColumnFromExcel {
//
//    public static List<List<MetaInfoDTO>> readExcel(String path, Map<String, MetaInfoDTO> map) throws IOException {
//        if (path == null || Common.EMPTY.equals(path)) {
//            return null;
//        } else {
//            String postfix = Util.getPostfix(path);
//            if (!Common.EMPTY.equals(postfix)) {
//                if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
//                    return readXls(path,map);
//                } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
//                    return readXlsx(path,map);
//                }
//            } else {
//                System.out.println(path + Common.NOT_EXCEL_FILE);
//            }
//        }
//        return new ArrayList<List<MetaInfoDTO>>();
//    }
//
//    public static Map<String,Object> CheckColumnExcel(String path) throws  IOException{
//        Map<String,Object> map = new HashMap<String, Object>();
//        if (path == null || Common.EMPTY.equals(path)) {
//            map.put("success",false);
//            map.put("message","文件路径不能为空!");
//        } else {
//            String postfix = Util.getPostfix(path);
//            if (!Common.EMPTY.equals(postfix)) {
//                if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
//                    map = CheckXls(path);
//                } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
//                    map = CheckXlsx(path);
//                }
//            } else {
//                map.put("success",false);
//                map.put("message",path + Common.NOT_EXCEL_FILE);
//            }
//        }
//        return map;
//    }
//
//    public static Map<String,Object> CheckXlsx(String path) {
//        Map<String,Object> map = new HashMap<String, Object>();
//        return map;
//    }
//
//    public static Map<String, Object> CheckXls(String path) {
//        Map<String,Object> map = new HashMap<String, Object>();
//        return map;
//    }
//
//
//    /**
//     * Read the Excel 2010
//     *
//     * @param path the path of the excel file
//     * @param map
//     * @return
//     * @throws IOException
//     */
//    public static List<List<MetaInfoDTO>> readXlsx(String path, Map<String, MetaInfoDTO> map) throws IOException {
//        System.out.println(Common.PROCESSING + path);
//        InputStream is = new FileInputStream(path);
//        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
//
//
//        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
//        if (xssfSheet == null ) {
//            return new ArrayList<List<MetaInfoDTO>>();
//        }
//
//        //获取标题栏信息
//        XSSFRow xssfRow = xssfSheet.getRow(0);
//        List<String> headers = new ArrayList<String>();
//        if (xssfRow != null) {
//            for (int colNum = xssfRow.getFirstCellNum(); colNum < xssfRow.getLastCellNum(); colNum++) {
//                XSSFCell cell = xssfRow.getCell(colNum);
//                if (cell != null) {
//                    headers.add(cell.getStringCellValue());
//                }else {
//                    break;
//                }
//            }
//        }
//
//        List<List<MetaInfoDTO>> tbl = new ArrayList<List<MetaInfoDTO>>();
//        for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
//            xssfRow = xssfSheet.getRow(rowNum);
//            if (xssfRow != null) {
//                List<MetaInfoDTO> list = new ArrayList<MetaInfoDTO>();
//                for (int colNum = xssfRow.getFirstCellNum(); colNum < xssfRow.getLastCellNum(); colNum++) {
//                    XSSFCell cell = xssfRow.getCell(colNum);
//                    if (cell != null) {
//                        MetaInfoDTO dto = getValue(cell,map.get(headers.get(colNum)));
//                        if(null!=dto){
//                            list.add(dto);
//                        }
//                    }else {
//                        break;
//                    }
//                }
//
//                tbl.add(list);
//            }
//        }
//        return tbl;
//    }
//
//    /**
//     * Read the Excel 2003-2007
//     *
//     * @param path the path of the Excel
//     * @param map
//     * @return
//     * @throws IOException
//     */
//    public static List<List<MetaInfoDTO>> readXls(String path, Map<String, MetaInfoDTO> map) throws IOException {
//        System.out.println(Common.PROCESSING + path);
//        InputStream is = new FileInputStream(path);
//        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
//
//        // Read the Sheet
//        HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
//        if (hssfSheet == null ) {
//            return new ArrayList<List<MetaInfoDTO>>();
//        }
//
//        //获取标题栏信息
//        HSSFRow hssfRow = hssfSheet.getRow(0);
//        List<String> headers = new ArrayList<String>();
//        if (hssfRow != null) {
//            for (int colNum = hssfRow.getFirstCellNum(); colNum < hssfRow.getLastCellNum(); colNum++) {
//                HSSFCell cell = hssfRow.getCell(colNum);
//                if (cell != null) {
//                    headers.add(cell.getStringCellValue());
//                }else {
//                    break;
//                }
//            }
//        }
//
//        List<List<MetaInfoDTO>> tbl = new ArrayList<List<MetaInfoDTO>>();
//        for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
//            hssfRow = hssfSheet.getRow(rowNum);
//            if (hssfRow != null) {
//                List<MetaInfoDTO> list = new ArrayList<MetaInfoDTO>();
//                for (int colNum = hssfRow.getFirstCellNum(); colNum < hssfRow.getLastCellNum(); colNum++) {
//                    HSSFCell cell = hssfRow.getCell(colNum);
//                    if (cell != null) {
//                        MetaInfoDTO dto = getValue(cell, map.get(headers.get(colNum)));
//                        if(null!=dto){
//                            list.add(dto);
//                        }
//                    }else {
//                        break;
//                    }
//                }
//
//                tbl.add(list);
//            }
//        }
//
//        return tbl;
//    }
//
//    @SuppressWarnings("static-access")
//    public static MetaInfoDTO getValue(XSSFCell xssfCell,MetaInfoDTO dto) {
//        if(dto!=null){
//            MetaInfoDTO t = new MetaInfoDTO();
//            t.setLayerName(dto.getMetaName());
//            t.setName(dto.getMetaName());
//            t.setWidth(dto.getWidth());
//            t.setType(dto.getType());
//            t.setCount(dto.getCount());
//            t.setIsNull(dto.getIsNull());
//            t.setText(xssfCell.getStringCellValue());
//            return t;
//        }
//        return null;
//    }
//
//    @SuppressWarnings("static-access")
//    public static MetaInfoDTO getValue(HSSFCell  hssfCell,MetaInfoDTO dto) {
//        if(dto!=null){
//            MetaInfoDTO t = new MetaInfoDTO();
//            t.setName(dto.getMetaName());
//            t.setLayerName(dto.getMetaName());
//            t.setWidth(dto.getWidth());
//            t.setType(dto.getType());
//            t.setIsNull(dto.getIsNull());
//            t.setCount(dto.getCount());
//            t.setText(hssfCell.getStringCellValue());
//            return t;
//        }
//        return null;
//    }
//}