mirror of https://github.com/apache/poi.git
Include the sheet name in the output of examples.XLS2CSVmra
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@697580 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d6250721e0
commit
c55c668573
|
@ -37,6 +37,7 @@
|
|||
|
||||
<!-- Don't forget to update status.xml too! -->
|
||||
<release version="3.2-alpha1" date="2008-??-??">
|
||||
<action dev="POI-DEVELOPERS" type="add">Include the sheet name in the output of examples.XLS2CSVmra</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">45784 - Support long chart titles in SeriesTextRecords</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
|
||||
<action dev="POI-DEVELOPERS" type="add">45844 - Addtional diagnostics for HSLF SlideShowRecordDumper</action>
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
<!-- Don't forget to update changes.xml too! -->
|
||||
<changes>
|
||||
<release version="3.2-alpha1" date="2008-??-??">
|
||||
<action dev="POI-DEVELOPERS" type="add">Include the sheet name in the output of examples.XLS2CSVmra</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">45784 - Support long chart titles in SeriesTextRecords</action>
|
||||
<action dev="POI-DEVELOPERS" type="fix">45777 - Throw an exception if HSSF Footer or Header is attemped to be set too long, rather than having it break during writing out</action>
|
||||
<action dev="POI-DEVELOPERS" type="add">45844 - Addtional diagnostics for HSLF SlideShowRecordDumper</action>
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.io.FileInputStream;
|
|||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
|
||||
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
|
||||
|
@ -34,6 +35,7 @@ import org.apache.poi.hssf.model.HSSFFormulaParser;
|
|||
import org.apache.poi.hssf.record.BOFRecord;
|
||||
import org.apache.poi.hssf.record.BlankRecord;
|
||||
import org.apache.poi.hssf.record.BoolErrRecord;
|
||||
import org.apache.poi.hssf.record.BoundSheetRecord;
|
||||
import org.apache.poi.hssf.record.FormulaRecord;
|
||||
import org.apache.poi.hssf.record.LabelRecord;
|
||||
import org.apache.poi.hssf.record.LabelSSTRecord;
|
||||
|
@ -70,6 +72,11 @@ public class XLS2CSVmra implements HSSFListener {
|
|||
private SSTRecord sstRecord;
|
||||
private FormatTrackingHSSFListener formatListener;
|
||||
|
||||
/** So we known which sheet we're on */
|
||||
private int sheetIndex = -1;
|
||||
private BoundSheetRecord[] orderedBSRs;
|
||||
private ArrayList boundSheetRecords = new ArrayList();
|
||||
|
||||
// For handling formulas with string results
|
||||
private int nextRow;
|
||||
private int nextColumn;
|
||||
|
@ -132,6 +139,9 @@ public class XLS2CSVmra implements HSSFListener {
|
|||
|
||||
switch (record.getSid())
|
||||
{
|
||||
case BoundSheetRecord.sid:
|
||||
boundSheetRecords.add(record);
|
||||
break;
|
||||
case BOFRecord.sid:
|
||||
BOFRecord br = (BOFRecord)record;
|
||||
if(br.getType() == BOFRecord.TYPE_WORKSHEET) {
|
||||
|
@ -139,6 +149,17 @@ public class XLS2CSVmra implements HSSFListener {
|
|||
if(workbookBuildingListener != null && stubWorkbook == null) {
|
||||
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
|
||||
}
|
||||
|
||||
// Output the worksheet name
|
||||
// Works by ordering the BSRs by the location of
|
||||
// their BOFRecords, and then knowing that we
|
||||
// process BOFRecords in byte offset order
|
||||
sheetIndex++;
|
||||
if(orderedBSRs == null) {
|
||||
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
|
||||
}
|
||||
output.println();
|
||||
output.println(orderedBSRs[sheetIndex].getSheetname() + ":");
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
|
||||
package org.apache.poi.hssf.record;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.util.BitField;
|
||||
import org.apache.poi.util.BitFieldFactory;
|
||||
import org.apache.poi.util.LittleEndian;
|
||||
|
@ -329,4 +333,31 @@ public final class BoundSheetRecord extends Record {
|
|||
public void setVeryHidden(boolean veryHidden) {
|
||||
field_2_option_flags = veryHiddenFlag.setShortBoolean(field_2_option_flags, veryHidden);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a list of BoundSheetRecords, and returns the all
|
||||
* ordered by the position of their BOFs.
|
||||
*/
|
||||
public static BoundSheetRecord[] orderByBofPosition(List boundSheetRecords) {
|
||||
BoundSheetRecord[] bsrs = (BoundSheetRecord[])boundSheetRecords.toArray(
|
||||
new BoundSheetRecord[boundSheetRecords.size()]);
|
||||
|
||||
// Sort
|
||||
Arrays.sort(bsrs, new BOFComparator());
|
||||
|
||||
// All done
|
||||
return bsrs;
|
||||
}
|
||||
private static class BOFComparator implements Comparator {
|
||||
public int compare(Object bsr1, Object bsr2) {
|
||||
return compare((BoundSheetRecord)bsr1, (BoundSheetRecord)bsr2);
|
||||
}
|
||||
public int compare(BoundSheetRecord bsr1, BoundSheetRecord bsr2) {
|
||||
if(bsr1.field_1_position_of_BOF < bsr2.field_1_position_of_BOF)
|
||||
return -1;
|
||||
if(bsr1.field_1_position_of_BOF == bsr2.field_1_position_of_BOF)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package org.apache.poi.hssf.record;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
import junit.framework.TestCase;
|
||||
|
@ -38,4 +39,24 @@ public final class TestBOFRecord extends TestCase {
|
|||
throw new AssertionFailedError("Identified bug 42794");
|
||||
}
|
||||
}
|
||||
|
||||
public void testOrdering() throws Exception {
|
||||
BoundSheetRecord bs1 = new BoundSheetRecord();
|
||||
BoundSheetRecord bs2 = new BoundSheetRecord();
|
||||
BoundSheetRecord bs3 = new BoundSheetRecord();
|
||||
bs1.setPositionOfBof(11);
|
||||
bs2.setPositionOfBof(33);
|
||||
bs3.setPositionOfBof(22);
|
||||
|
||||
ArrayList l = new ArrayList();
|
||||
l.add(bs1);
|
||||
l.add(bs2);
|
||||
l.add(bs3);
|
||||
|
||||
BoundSheetRecord[] r = BoundSheetRecord.orderByBofPosition(l);
|
||||
assertEquals(3, r.length);
|
||||
assertEquals(bs1, r[0]);
|
||||
assertEquals(bs3, r[1]);
|
||||
assertEquals(bs2, r[2]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue