组织机构关系的整理

版权声明:转载请注明出处 https://blog.csdn.net/qq_26769591/article/details/82469311

写作前言
简单来说就是从父子关系中,整理出层次关系,里面比较关键的一点是面向对象的思路,将每个部门看成一个节点,封装起来

1.问题描述

就是将左边的图转成右边的这张图,说白了就是用父子关系,整理出里面的层次结构

这里写图片描述

2.解决思路(第三点是关键 面向对象的思想)

1.读取excel表,得到父子关系 存到一个map

2.读取excel表,读取编号和姓名的关系存到一个map

3.然后对每一项搜索它的上级 一级上级的上级 直到最顶级 用list来保存必须要用有序的集合类型不能用set

4.将搜索的中间节点的编号记下来,存到一个数组中list到string,写到excel中

5.对齐顶级–采用一个逆置

3 设计到的知识点有

1.map

这里写图片描述

2.list

这里写图片描述

3.set

这里写图片描述

4.对象数组初始化

5.动态数组

4.代码

Dep_deal_new

import java.util.*;

public class Dep_deal_new {
    public void dealonelayer(String filename, int parent, int children, int colth_name) {
        try {
            //打开文件
            ExcelWrite ew=new ExcelWrite("Dep_deal_new.xls");
            ExcelRead er = new ExcelRead();

            er.filename = filename;

            String str_tmp[];
            str_tmp = er.readonecolum(parent);
            //读取父子关系map
            Map<String, String> map = new HashMap<String, String>();
            int rowth = 0;
            while (er.readoneline(rowth) != null && rowth < 400) {
                str_tmp = er.readoneline(rowth);
                map.put(str_tmp[children], str_tmp[parent]);
                rowth++;
            }

            //读取名称
            Department[] d = new Department[map.size()];
            Map<String, String> map_name = new HashMap<String, String>();
            rowth = 0;
            while (er.readoneline(rowth) != null && rowth < 400) {
                str_tmp = er.readoneline(rowth);
                map_name.put(str_tmp[children], str_tmp[colth_name]); //名称保存在第一列
                rowth++;
            }

            //写子 父 爷爷 祖宗关系
            String par = null;
            List<String> set_one = new   ArrayList<String>();
//            Set<String> set_one = new HashSet<String>();  //set是无序的 这里需要注意一下
            rowth=0;
            for (Map.Entry<String, String> entry : map.entrySet()) {
                set_one.clear();
                set_one.add(entry.getKey()+map_name.get(entry.getKey())); //写第一层
                par = map.get(entry.getKey());
                while (par != null) {
                    set_one.add(par+map_name.get(par)); //添加父亲
                    par = map.get(par);
                }

                int j = 0;
                String[] strwrite = new String[set_one.size()];
                for (String str : set_one) {
                    strwrite[j++] = str;

                }
                //数组 进行逆序 这样实现了左对齐
                String t ="";
                int n=strwrite.length;
                for(int i=0; i<n/2; ++i){
                     t=strwrite[i];
                    strwrite[i]=strwrite[n-i-1];
                    strwrite[n-i-1]=t;
                }
                ew.WriteSingleCell(rowth++,strwrite); //写一行

            }

            ew.WriteBack();
        } catch (Exception e)

        {
            e.printStackTrace();
        }
    }

    public  static void  main(String args[])
    {
        Dep_deal_new d=new Dep_deal_new();
        d.dealonelayer("DepartInfo.xls",4,0,2);

    }

}

ExcelRead

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.poifs.filesystem.POIFSFileSystem;

public class ExcelRead {
    InputStream stream;
    POIFSFileSystem fs;
    HSSFWorkbook wb;
    String filename = "UserInfo_2.xls";

    public ExcelRead() {
    }

    public void ExcelRead(String filename) {
        this.filename = filename;
    }

    public String[] readoneline(int rowNum) {
        int colnum = 10;
        String[] str = new String[colnum];

        try {
            this.stream = new FileInputStream(this.filename);
            this.fs = new POIFSFileSystem(this.stream);
            this.wb = new HSSFWorkbook(this.fs);
            HSSFSheet sheet = this.wb.getSheetAt(0);
            HSSFRow row = sheet.getRow(rowNum);
            if (row == null) {
                return null;
            }

            int[] col = new int[]{0, 1, 2, 3, 4};
            int i = false;
            int j = false;

            for(int i = 0; i < col.length; ++i) {
                HSSFCell cell = row.getCell(col[i]);
                str[i] = readCellSecondMethod(cell);
            }

            this.stream.close();
        } catch (Exception var10) {
            var10.printStackTrace();
        }

        return str;
    }

    public String[] readonecolum(int colnum) {
        int col_length = 690;
        String[] str = new String[col_length];

        try {
            this.stream = new FileInputStream(this.filename);
            this.fs = new POIFSFileSystem(this.stream);
            this.wb = new HSSFWorkbook(this.fs);
            HSSFSheet sheet = this.wb.getSheetAt(0);
            int col_length = sheet.getLastRowNum();
            String[] str_temp = new String[col_length];

            for(int i = 0; i < col_length; ++i) {
                HSSFRow row = sheet.getRow(i);
                HSSFCell cell = row.getCell(colnum);
                str_temp[i] = readCellSecondMethod(cell);
                str[i] = readCellSecondMethod(cell);
            }

            this.stream.close();
        } catch (Exception var9) {
            var9.printStackTrace();
        }

        return str;
    }

    public void read() {
        try {
            this.stream = new FileInputStream(this.filename);
            this.fs = new POIFSFileSystem(this.stream);
            this.wb = new HSSFWorkbook(this.fs);
            HSSFSheet sheet = this.wb.getSheetAt(0);
            if (sheet == null) {
                return;
            }

            for(int rowNum = 0; rowNum <= sheet.getLastRowNum(); ++rowNum) {
                HSSFRow row = sheet.getRow(rowNum);
                if (row != null) {
                    for(int cellNum = 0; cellNum <= row.getLastCellNum(); ++cellNum) {
                        HSSFCell cell = row.getCell(cellNum);
                        if (cell != null) {
                            String strVal = readCellSecondMethod(cell);
                            if (cellNum == 2) {
                                strVal = strVal.contains(".") ? strVal.substring(0, strVal.indexOf(".")) : strVal;
                            }

                            System.out.print(" " + strVal);
                        }
                    }

                    System.out.println();
                }
            }

            this.stream.close();
        } catch (Exception var7) {
            var7.printStackTrace();
        }

    }

    public static String readCellFirstMethod(HSSFCell cell) {
        if (cell.getCellType() == 4) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType() == 0) {
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
            } else {
                return String.valueOf(cell.getNumericCellValue());
            }
        } else {
            return cell.getStringCellValue();
        }
    }

    public static String readCellSecondMethod(HSSFCell cell) {
        if (cell == null) {
            return null;
        } else {
            switch(cell.getCellType()) {
            case 0:
                String strVal = String.valueOf(cell.getNumericCellValue());
                strVal = strVal.contains(".") ? strVal.substring(0, strVal.indexOf(".")) : strVal;
                return strVal;
            case 1:
                return cell.getStringCellValue();
            case 2:
                return cell.getCellFormula();
            case 3:
                return null;
            case 4:
                return cell.getBooleanCellValue() + "";
            case 5:
                return cell.getErrorCellValue() + "";
            default:
                return "";
            }
        }
    }
}

ExcelWrite

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelWrite {
    HSSFWorkbook wb;
    HSSFSheet sheet;
    String filename = "";
    HSSFRow row;

    public ExcelWrite(String filename) {
        this.filename = filename;
        this.wb = new HSSFWorkbook();
        this.sheet = this.wb.createSheet(filename);
    }

    public void WriteSingleCell(int rowth, String[] word) {
        this.row = this.sheet.createRow(rowth);

        for(int colth = 0; colth < word.length; ++colth) {
            this.row.createCell(colth).setCellValue(word[colth]);
        }

    }

    public void WriteSingleCell(int rowth, String word, int colth) {
        this.row = this.sheet.createRow(rowth);
        this.row.createCell(colth).setCellValue(word);
    }

    public void WriteBack() {
        try {
            FileOutputStream out = new FileOutputStream(this.filename);
            this.wb.write(out);
            out.close();
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        System.out.println("写完了!");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_26769591/article/details/82469311