01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hssf.usermodel.examples;
19:
20: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
21: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
22: import org.apache.poi.hssf.usermodel.HSSFSheet;
23: import org.apache.poi.hssf.usermodel.HSSFRow;
24: import org.apache.poi.hssf.usermodel.HSSFCell;
25:
26: import java.io.FileInputStream;
27: import java.io.FileOutputStream;
28: import java.io.IOException;
29:
30: /**
31: * This example demonstrates opening a workbook, modifying it and writing
32: * the results back out.
33: *
34: * @author Glen Stampoultzis (glens at apache.org)
35: */
36: public class ReadWriteWorkbook {
37: public static void main(String[] args) throws IOException {
38: FileInputStream fileIn = null;
39: FileOutputStream fileOut = null;
40:
41: try {
42: fileIn = new FileInputStream("workbook.xls");
43: POIFSFileSystem fs = new POIFSFileSystem(fileIn);
44: HSSFWorkbook wb = new HSSFWorkbook(fs);
45: HSSFSheet sheet = wb.getSheetAt(0);
46: HSSFRow row = sheet.getRow(2);
47: if (row == null)
48: row = sheet.createRow(2);
49: HSSFCell cell = row.getCell((short) 3);
50: if (cell == null)
51: cell = row.createCell((short) 3);
52: cell.setCellType(HSSFCell.CELL_TYPE_STRING);
53: cell.setCellValue("a test");
54:
55: // Write the output to a file
56: fileOut = new FileOutputStream("workbookout.xls");
57: wb.write(fileOut);
58: } finally {
59: if (fileOut != null)
60: fileOut.close();
61: if (fileIn != null)
62: fileIn.close();
63: }
64: }
65: }
|