You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReadWriteWorkbook.java 2.2KB

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