web-dev-qa-db-fra.com

StatefulBeanToCsv avec en-têtes de colonne

J'utilise opencsv-4.0 pour écrire un fichier csv et j'ai besoin d'ajouter des en-têtes de colonne dans le fichier de sortie.

Voici mon code.

public static void buildProductCsv(final List<Product> product,
        final String filePath) {

    try {

        Writer writer = new FileWriter(filePath);

        // mapping of columns with their positions
        ColumnPositionMappingStrategy<Product> mappingStrategy = new ColumnPositionMappingStrategy<Product>();
        // Set mappingStrategy type to Product Type
        mappingStrategy.setType(Product.class);
        // Fields in Product Bean
        String[] columns = new String[] { "productCode", "MFD", "EXD" };
        // Setting the colums for mappingStrategy
        mappingStrategy.setColumnMapping(columns);

        StatefulBeanToCsvBuilder<Product> builder = new StatefulBeanToCsvBuilder<Product>(writer);

        StatefulBeanToCsv<Product> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
        // Writing data to csv file
        beanWriter.write(product);
        writer.close();

        log.info("Your csv file has been generated!");

    } catch (Exception ex) {
        log.warning("Exception: " + ex.getMessage());
    }

}

Code ci-dessus créer un fichier csv avec des données. Mais cela n'inclut pas les en-têtes de colonne dans ce fichier.

Comment pourrais-je ajouter des en-têtes de colonne pour générer des csv?

6
Bishan

ColumnPositionMappingStrategy # generateHeader renvoie un tableau vide.

/**
 * This method returns an empty array.
 * The column position mapping strategy assumes that there is no header, and
 * thus it also does not write one, accordingly.
 * @return An empty array
 */
@Override
public String[] generateHeader() {
    return new String[0];
}

Si vous supprimez MappingStrategy du générateur BeanToCsv

// replace 
StatefulBeanToCsv<Product> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
// with
StatefulBeanToCsv<Product> beanWriter = builder.build(); 

Il écrira les membres de la classe Product en tant qu'en-tête CSV.

Si les noms de vos membres de la classe Product sont 

"productCode", "MFD", "EXD"

Cela devrait être la bonne solution

Sinon, ajouter une annotation @CsvBindByName

import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;

import Java.io.FileWriter;
import Java.io.Writer;
import Java.util.ArrayList;
import Java.util.List;

public class CsvTest {

    public static void main(String[] args) throws Exception {
        Writer writer = new FileWriter(fileName);

        StatefulBeanToCsvBuilder<Product> builder = new StatefulBeanToCsvBuilder<>(writer);
        StatefulBeanToCsv<Product> beanWriter = builder.build();

        List<Product> products = new ArrayList<>();
        products.add(new Product("1", "11", "111"));
        products.add(new Product("2", "22", "222"));
        products.add(new Product("3", "33", "333"));
        beanWriter.write(products);
        writer.close();
    }

    public static class Product {
        @CsvBindByName(column = "productCode")
        String id;
        @CsvBindByName(column = "MFD")
        String member2;
        @CsvBindByName(column = "EXD")
        String member3;

        Product(String id, String member2, String member3) {
            this.id = id;
            this.member2 = member2;
            this.member3 = member3;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getMember2() {
            return member2;
        }

        public void setMember2(String member2) {
            this.member2 = member2;
        }

        public String getMember3() {
            return member3;
        }

        public void setMember3(String member3) {
            this.member3 = member3;
        }
    }

}

Sortie:

"EXD", "MFD", "PRODUCTCODE" 

"111", "11", "1" 

"222", "22", "2" 

"333", "33", "3"

Faites attention; class, getters & setters doit être public en raison de l'utilisation de la bibliothèque Reflection by OpenCSV

6
Iddo

Pas si bien, mais le plus simple est d’ajouter un en-tête à un objet writer ...

        ...
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        osw.append("PRODUCTCODE;EXD;MFD\n");
        CSVWriter writer = new CSVWriter(osw, ';', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER,
                CSVWriter.DEFAULT_LINE_END);
        StatefulBeanToCsv<Product> beanToCsv = new StatefulBeanToCsvBuilder<Product>(writer)
                .build();
        beanToCsv.write(linhas);
0
fcecchin

J'ai peut-être manqué quelque chose d'évident ici, mais pourriez-vous simplement ajouter votre en-tête String à l'objet writer?

Writer writer = new FileWriter(filePath);
writer.append("header1, header2, header3, ...etc \n");

// This will be followed by your code with BeanToCsvBuilder 
// Note: the terminating \n might differ pending env.
0
Ithar