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.

OldExcelExtractor.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.hssf.extractor;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import org.apache.poi.hssf.record.LabelRecord;
  21. import org.apache.poi.hssf.record.OldLabelRecord;
  22. import org.apache.poi.hssf.record.RecordInputStream;
  23. /**
  24. * A text extractor for very old (pre-OLE2) Excel files,
  25. * such as Excel 4 files.
  26. * <p>
  27. * Returns much (but not all) of the textual content of the file,
  28. * suitable for indexing by something like Apache Lucene, or used
  29. * by Apache Tika, but not really intended for display to the user.
  30. * </p>
  31. */
  32. public class OldExcelExtractor {
  33. private InputStream input;
  34. private boolean _includeSheetNames = true;
  35. public OldExcelExtractor(InputStream input) {
  36. this.input = input;
  37. }
  38. public OldExcelExtractor(File f) throws IOException {
  39. this.input = new FileInputStream(f);
  40. }
  41. public static void main(String[] args) throws Exception {
  42. if (args.length < 1) {
  43. System.err.println("Use:");
  44. System.err.println(" OldExcelExtractor <filename>");
  45. System.exit(1);
  46. }
  47. OldExcelExtractor extractor = new OldExcelExtractor(new File(args[0]));
  48. System.out.println(extractor.getText());
  49. }
  50. /**
  51. * Should sheet names be included? Default is true
  52. */
  53. public void setIncludeSheetNames(boolean includeSheetNames) {
  54. _includeSheetNames = includeSheetNames;
  55. }
  56. /**
  57. * Retrieves the text contents of the file, as best we can
  58. * for these old file formats
  59. */
  60. public String getText() {
  61. StringBuffer text = new StringBuffer();
  62. RecordInputStream ris = new RecordInputStream(input);
  63. while (ris.hasNextRecord()) {
  64. int sid = ris.getNextSid();
  65. ris.nextRecord();
  66. switch (sid) {
  67. case LabelRecord.sid:
  68. OldLabelRecord lr = new OldLabelRecord(ris);
  69. text.append(lr.getValue());
  70. text.append('\n');
  71. break;
  72. default:
  73. ris.readFully(new byte[ris.remaining()]);
  74. }
  75. // label - 5.63 - TODO Needs codepages
  76. // number - 5.71
  77. // rk - 5.87
  78. // string - 5.102
  79. }
  80. return text.toString();
  81. }
  82. }