1
2
3
4
5 package oscon2006.common;
6
7 import java.io.*;
8 import org.apache.poi.hssf.*;
9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
10
11 public class xls2csv
12 {
13 static private final String SHEETNAME_OPTION = "-s";
14 static private String argXlsFilename;
15 static private String argSheetname;
16
17 /**
18 *
19 * @param args
20 *
21 */
22 public static void main(String[] args)
23 {
24 if (args.length != 1)
25 {
26 System.exit(1);
27 }
28
29 processArgs(args);
30
31 InputStream in = null;
32
33 String csvFilename = "output.csv";
34
35 try
36 {
37 in = new FileInputStream(argXlsFilename);
38 HSSFWorkbook wb = new HSSFWorkbook(in);
39
40 CsvBuilder builder = new CsvBuilder();
41 builder.setData(wb);
42 builder.setTargetWorksheet(argSheetname);
43 builder.build(csvFilename);
44 }
45 catch (Exception ex)
46 {
47 ex.printStackTrace();
48 }
49 finally
50 {
51 try
52 {
53 in.close();
54 }
55 catch (IOException ignore)
56 {
57
58 }
59
60 }
61
62 }
63
64 static private void processArgs(String[] args)
65 {
66 for (int index = 0; index < args.length; index++)
67 {
68 String arg = args[index];
69 if (arg.equals(SHEETNAME_OPTION))
70 {
71 index++;
72 argSheetname = args[index];
73 }
74 else
75 {
76 argXlsFilename = args[index];
77 }
78 }
79 }
80 }