View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2007-2019 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * TODO Document Me
29   * 
30   * @author lfrench
31   * 
32   */
33  public class CreateSpreadSheet {
34  
35      protected String filename;
36      protected SpreadSheetSchema schema;
37      /**
38       * @param args
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              // throw new Exception( "please delete previous file to prevent overwrite" );
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          // make the header
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              // System.out.println( header[i] );
76              ExcelUtil.setValue( spreadsheet, 0, i, header[i] );
77  
78          }
79      }
80  
81  }