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.

BiffDrawingToXml.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. 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 org.apache.poi.hssf.dev;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.apache.poi.ddf.EscherRecord;
  28. import org.apache.poi.hssf.model.InternalWorkbook;
  29. import org.apache.poi.hssf.record.DrawingGroupRecord;
  30. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  31. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  32. import org.apache.poi.util.StringUtil;
  33. /**
  34. * Utility for representing drawings contained in a binary Excel file as a XML tree
  35. */
  36. public class BiffDrawingToXml {
  37. private static final String SHEET_NAME_PARAM = "-sheet-name";
  38. private static final String SHEET_INDEXES_PARAM = "-sheet-indexes";
  39. private static final String EXCLUDE_WORKBOOK_RECORDS = "-exclude-workbook";
  40. private static int getAttributeIndex(String attribute, String[] params) {
  41. for (int i = 0; i < params.length; i++) {
  42. String param = params[i];
  43. if (attribute.equals(param)) {
  44. return i;
  45. }
  46. }
  47. return -1;
  48. }
  49. private static boolean isExcludeWorkbookRecords(String[] params) {
  50. return -1 != getAttributeIndex(EXCLUDE_WORKBOOK_RECORDS, params);
  51. }
  52. private static List<Integer> getIndexesByName(String[] params, HSSFWorkbook workbook) {
  53. List<Integer> list = new ArrayList<>();
  54. int pos = getAttributeIndex(SHEET_NAME_PARAM, params);
  55. if (-1 != pos) {
  56. if (pos >= params.length) {
  57. throw new IllegalArgumentException("sheet name param value was not specified");
  58. }
  59. String sheetName = params[pos + 1];
  60. int sheetPos = workbook.getSheetIndex(sheetName);
  61. if (-1 == sheetPos){
  62. throw new IllegalArgumentException("specified sheet name has not been found in xls file");
  63. }
  64. list.add(sheetPos);
  65. }
  66. return list;
  67. }
  68. private static List<Integer> getIndexesByIdArray(String[] params) {
  69. List<Integer> list = new ArrayList<>();
  70. int pos = getAttributeIndex(SHEET_INDEXES_PARAM, params);
  71. if (-1 != pos) {
  72. if (pos >= params.length) {
  73. throw new IllegalArgumentException("sheet list value was not specified");
  74. }
  75. String sheetParam = params[pos + 1];
  76. String[] sheets = sheetParam.split(",");
  77. for (String sheet : sheets) {
  78. list.add(Integer.parseInt(sheet));
  79. }
  80. }
  81. return list;
  82. }
  83. private static List<Integer> getSheetsIndexes(String[] params, HSSFWorkbook workbook) {
  84. List<Integer> list = new ArrayList<>();
  85. list.addAll(getIndexesByIdArray(params));
  86. list.addAll(getIndexesByName(params, workbook));
  87. if (0 == list.size()) {
  88. int size = workbook.getNumberOfSheets();
  89. for (int i = 0; i < size; i++) {
  90. list.add(i);
  91. }
  92. }
  93. return list;
  94. }
  95. private static String getInputFileName(String[] params) {
  96. return params[params.length - 1];
  97. }
  98. private static String getOutputFileName(String input) {
  99. if (input.contains("xls")) {
  100. return input.replace(".xls", ".xml");
  101. }
  102. return input + ".xml";
  103. }
  104. public static void main(String[] params) throws IOException {
  105. if (0 == params.length) {
  106. System.out.println("Usage: BiffDrawingToXml [options] inputWorkbook");
  107. System.out.println("Options:");
  108. System.out.println(" -exclude-workbook exclude workbook-level records");
  109. System.out.println(" -sheet-indexes <indexes> output sheets with specified indexes");
  110. System.out.println(" -sheet-namek <names> output sheets with specified name");
  111. return;
  112. }
  113. String input = getInputFileName(params);
  114. FileInputStream inp = new FileInputStream(input);
  115. String output = getOutputFileName(input);
  116. FileOutputStream outputStream = new FileOutputStream(output);
  117. writeToFile(outputStream, inp, isExcludeWorkbookRecords(params), params);
  118. inp.close();
  119. outputStream.close();
  120. }
  121. public static void writeToFile(OutputStream fos, InputStream xlsWorkbook, boolean excludeWorkbookRecords, String[] params) throws IOException {
  122. HSSFWorkbook workbook = new HSSFWorkbook(xlsWorkbook);
  123. InternalWorkbook internalWorkbook = workbook.getInternalWorkbook();
  124. DrawingGroupRecord r = (DrawingGroupRecord) internalWorkbook.findFirstRecordBySid(DrawingGroupRecord.sid);
  125. StringBuilder builder = new StringBuilder();
  126. builder.append("<workbook>\n");
  127. String tab = "\t";
  128. if (!excludeWorkbookRecords && r != null) {
  129. r.decode();
  130. List<EscherRecord> escherRecords = r.getEscherRecords();
  131. for (EscherRecord record : escherRecords) {
  132. builder.append(record.toXml(tab));
  133. }
  134. }
  135. List<Integer> sheets = getSheetsIndexes(params, workbook);
  136. for (Integer i : sheets) {
  137. HSSFPatriarch p = workbook.getSheetAt(i).getDrawingPatriarch();
  138. if(p != null ) {
  139. builder.append(tab).append("<sheet").append(i).append(">\n");
  140. builder.append(p.getBoundAggregate().toXml(tab + "\t"));
  141. builder.append(tab).append("</sheet").append(i).append(">\n");
  142. }
  143. }
  144. builder.append("</workbook>\n");
  145. fos.write(builder.toString().getBytes(StringUtil.UTF8));
  146. fos.close();
  147. workbook.close();
  148. }
  149. }