OpenCSV: How to create CSV file from POJO with custom column headers and custom column positions?

后端 未结 19 1499
温柔的废话
温柔的废话 2020-12-08 04:14

I have created a MappingsBean class where all the columns of the CSV file are specified. Next I parse XML files and create a list of mappingbeans. Then I write that data int

19条回答
  •  天涯浪人
    2020-12-08 04:55

    Try something like below:

    private static class CustomMappingStrategy extends ColumnPositionMappingStrategy {
    
        String[] header;
    
        public CustomMappingStrategy(String[] cols) {
            header = cols;
        }
    
        @Override
        public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {
            return header;
        }
    }
    

    Then use it as follows:

    String[] columns = new String[]{"Name", "Age", "Company", "Salary"};
            CustomMappingStrategy mappingStrategy = new CustomMappingStrategy(columns);
    

    Where columns are columns of your bean and Employee is your bean

提交回复
热议问题