1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.io.excel;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23
24 import org.apache.poi.hssf.usermodel.HSSFSheet;
25 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
26
27
28
29
30
31
32
33 public class CreateSpreadSheet {
34
35 protected String filename;
36 protected SpreadSheetSchema schema;
37
38
39
40 protected HSSFSheet spreadsheet;
41
42 protected HSSFWorkbook workbook;
43
44 public CreateSpreadSheet( String filename, SpreadSheetSchema schema ) {
45 if ( new File( filename ).exists() ) {
46
47 }
48 this.filename = filename;
49 this.schema = schema;
50
51 init();
52 }
53
54 public void init() {
55 try {
56 workbook = new HSSFWorkbook();
57 spreadsheet = workbook.createSheet();
58 } catch ( Exception e ) {
59 e.printStackTrace();
60 System.exit( 1 );
61 }
62
63 createHeader();
64 }
65
66 public void save() throws Exception {
67 FileOutputStream fileOut = new FileOutputStream( filename );
68 workbook.write( fileOut );
69 fileOut.close();
70 }
71
72 private void createHeader() {
73 String[] header = schema.getHeaderRow();
74 for ( int i = 0; i < header.length; i++ ) {
75
76 ExcelUtil.setValue( spreadsheet, 0, i, header[i] );
77
78 }
79 }
80
81 }