Skip to content

Commit e7f1e67

Browse files
committed
fix bug on absolute path in _rels/workbook.xml.rels
1 parent d4b00cb commit e7f1e67

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/OPCPackage.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,16 @@ private static String relsNameFor(String entryName) {
6161
}
6262

6363
private Map<String, String> readWorkbookPartsIds(String workbookRelsEntryName) throws IOException, XMLStreamException {
64+
String xlFolder = workbookRelsEntryName.substring(0, workbookRelsEntryName.indexOf("_rel"));
6465
Map<String, String> partsIdById = new HashMap<>();
6566
SimpleXmlReader rels = new SimpleXmlReader(factory, getRequiredEntryContent(workbookRelsEntryName));
6667
while (rels.goTo("Relationship")) {
6768
String id = rels.getAttribute("Id");
6869
String target = rels.getAttribute("Target");
70+
// if name does not start with /, it is a relative path
71+
if (!target.startsWith("/")) {
72+
target = xlFolder + target;
73+
} // else it is an absolute path
6974
partsIdById.put(id, target);
7075
}
7176
return partsIdById;
@@ -92,7 +97,7 @@ private PartEntryNames extractPartEntriesFromContentTypes() throws XMLStreamExce
9297
if (entries.workbook == null) {
9398
// in case of a default workbook path, we got this
9499
// <Default Extension="xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />
95-
entries.workbook = "xl/workbook.xml";
100+
entries.workbook = "/xl/workbook.xml";
96101
}
97102
}
98103
return entries;
@@ -178,10 +183,6 @@ public InputStream getSheetContent(Sheet sheet) throws IOException {
178183
sheet.getIndex(), sheet.getName(), sheet.getId());
179184
throw new ExcelReaderException(msg);
180185
}
181-
// if name does not start with /, it is a relative path
182-
if (!name.startsWith("/")) {
183-
name = "xl/" + name;
184-
} // else it is an absolute path
185186
return getRequiredEntryContent(name);
186187
}
187188

fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/InvalidOPCPackageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void expectErrors() throws IOException {
1414
expectError("/invalid/only-content-types.xlsx", "/xl/_rels/custom1-workbook.xml.rels not found");
1515
expectError("/invalid/no-workbook-rels.xlsx", "/xl/_rels/custom1-workbook.xml.rels not found");
1616
expectError("/invalid/no-workbook-xml.xlsx", "/xl/custom1-workbook.xml not found");
17-
expectError("/invalid/no-sheet.xlsx", "xl/worksheets/custom3-sheet1.xml not found");
17+
expectError("/invalid/no-sheet.xlsx", "/xl/worksheets/custom3-sheet1.xml not found");
1818
expectError("/invalid/missing-sheet-entry.xlsx", "Sheet#0 'Feuil1' is missing an entry in workbook rels (for id: 'rId42')");
1919
}
2020

fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/SimpleReaderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20+
import java.io.FileInputStream;
2021
import java.io.IOException;
2122
import java.io.InputStream;
2223
import java.math.BigDecimal;
@@ -116,4 +117,21 @@ public void testDefaultWorkbookPath() throws IOException {
116117
}
117118
}
118119
}
120+
121+
@Test
122+
public void testDefaultWorkbookPath2() throws IOException {
123+
try (InputStream is = Resources.open("/xlsx/absolutePath.xlsx");
124+
ReadableWorkbook wb = new ReadableWorkbook(is, new ReadingOptions(false, true))) {
125+
Sheet sheet = wb.getFirstSheet();
126+
try (Stream<Row> rows = sheet.openStream()) {
127+
Iterator<Row> it = rows.iterator();
128+
assertTrue(it.hasNext());
129+
Iterator<Cell> cellIt = it.next().iterator();
130+
assertTrue(cellIt.hasNext());
131+
Cell cell = cellIt.next();
132+
assertEquals(CellType.NUMBER, cell.getType());
133+
assertEquals(BigDecimal.ONE, cell.getValue());
134+
}
135+
}
136+
}
119137
}
5.25 KB
Binary file not shown.

0 commit comments

Comments
 (0)