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