View Javadoc

1   package net.sf.flatpack.examples.exporttoexcel;
2   
3   /*
4    * Created on Dec 31, 2004
5    *
6    */
7   
8   import java.io.File;
9   
10  import net.sf.flatpack.DataError;
11  import net.sf.flatpack.DataSet;
12  import net.sf.flatpack.DefaultParserFactory;
13  import net.sf.flatpack.Parser;
14  import net.sf.flatpack.ordering.OrderBy;
15  import net.sf.flatpack.ordering.OrderColumn;
16  import net.sf.flatpack.util.ExcelTransformer;
17  
18  /**
19   * @author zepernick
20   * 
21   * TODO To change the template for this generated type comment go to Window -
22   * Preferences - Java - Code Style - Code Templates
23   */
24  public class DelimitedFileExportToExcel {
25      public static void main(final String[] args) throws Exception {
26          final String mapping = getDefaultMapping();
27          final String data = getDefaultDataFile();
28          call(mapping, data);
29  
30      }
31  
32      public static String getDefaultDataFile() {
33          return "PEOPLE-CommaDelimitedWithQualifier.txt";
34      }
35  
36      public static String getDefaultMapping() {
37          return "PEOPLE-Delimited.pzmap.xml";
38      }
39  
40      public static void call(final String mapping, final String data) throws Exception {
41          // delimited by a comma
42          // text qualified by double quotes
43          // ignore first record
44          final Parser pzparser = DefaultParserFactory.getInstance().newDelimitedParser(new File(mapping), new File(data), ',', '"', true);
45          final DataSet ds = pzparser.parse();
46  
47          // re order the data set by last name
48          final OrderBy orderby = new OrderBy();
49          orderby.addOrderColumn(new OrderColumn("CITY", false));
50          orderby.addOrderColumn(new OrderColumn("LASTNAME", true));
51          ds.orderRows(orderby);
52  
53          if (ds.getErrors() != null && ds.getErrors().size() > 0) {
54              for (int i = 0; i < ds.getErrors().size(); i++) {
55                  final DataError de = (DataError) ds.getErrors().get(i);
56                  System.out.println("Error Msg: " + de.getErrorDesc() + " Line: " + de.getLineNo());
57              }
58          }
59  
60          // lets write this file out to excel
61          final File xlFile = new File("MyExcelExport.xls");
62          final ExcelTransformer xlTransformer = new ExcelTransformer(ds, xlFile);
63          xlTransformer.writeExcelFile();
64          System.out.println("Excel Workbook Written To: " + xlFile.getAbsolutePath());
65  
66      }
67  }