• 【工具】获取pojo类属性,并写入表格


    1、添加依赖

          <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.9</version>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.9</version>
            </dependency>       

    2、java代码

    public class CreateExcel {
        
        static String fileName = "work1.xsl";
        
        /**
         * 读取domain文件的属性名和类型
         * @param object
         * @return
         */
        public static List<Map<String, String>> getDomainV(Object object){
            List<Map<String, String>> result = new ArrayList<Map<String,String>> ();
            Field[] fields = object.getClass().getDeclaredFields();
            for(int i=0;i<fields.length;i++){
                Map<String, String> map = new HashMap<String, String>();
                String attributeName = fields[i].getName();  //获取属性名称
                String attributeType = fields[i].getGenericType().toString(); //获取属性类型
                String[] types = attributeType.split("\.");
                map.put("name", attributeName);
                map.put("type", types[types.length-1]);
                result.add(map);
            }        
            return result;
        }
    
        
        /**
         * 生成表格
         * @param list
         * @throws IOException
         */
        public static void createWorkBook(List<Map<String, String>> list,String fileName) throws IOException { 
                Workbook wb = new HSSFWorkbook();//创建excel工作簿 
                Sheet sheet = wb.createSheet("new sheet"); //创建第一个sheet(页),命名为 new sheet 
                
                for(int i=0;i<list.size();i++){
                    Row row = sheet.createRow(i); // 创建一行,在页sheet上                  
                    Cell cell = row.createCell(0); // 在row行上创建一个方格                
                    cell.setCellValue(list.get(i).get("name")); //设置方格的显示 
                    cell = row.createCell(1);
                    cell.setCellValue(list.get(i).get("type")); //
                }
                
                FileOutputStream fileOut = new FileOutputStream(fileName); 
                wb.write(fileOut); 
                fileOut.close(); 
            } 
         
         
        /**
        * 读取Excel表格
        * @param fileName
        * @throws Exception
        */
        public static void readWorkBook(String fileName) throws Exception { 
            InputStream inp = new FileInputStream(fileName); 
                 
            Workbook wb = WorkbookFactory.create(inp); 
            Sheet sheet = wb.getSheetAt(0); 
                 
            for (Row row : sheet) { //利用foreach循环 遍历sheet中的所有行                
                for (Cell cell : row) { //遍历row中的所有方格                      
                    System.out.print(cell.toString() + "  "); //输出方格中的内容,以空格间隔
                }               
                System.out.println();   //每一个行输出之后换行 
            }             
            inp.close(); //关闭输入流 
        } 
            
        public static void main(String[] args) throws Exception { 
            Student stu = new Student();
            List<Map<String, String>> list = getDomainV(stu);
            createWorkBook(list,fileName); 
            System.out.println("creat successful!");
            readWorkBook(fileName); 
        } 
    
    }
  • 相关阅读:
    winform 监视DataGridView的滚动条,加载数据
    SQL 自动生成行号
    SQL 删除重复的数据(多个字段判断),只保留一条数据
    C# 一个简单的 工厂模式 例子
    winform 重写TextBox的OnTextChanged方法(事件)
    winform 给文本框加载内容的时候 始终让文本框的滚动条到底(允许显示多行Multiline=True)
    winform 制定DataGridViewTextColumn列(更改DataGridView的Cell的状态很有用)
    winform 使用委托 实现多线程访问控件
    C# 一个简单的递归函数和 两个List<T> 合并
    ADO.NET  把数据库A的某表的数据内容复制到数据库B的某表内
  • 原文地址:https://www.cnblogs.com/cuglkb/p/7979529.html
Copyright © 2020-2023  润新知