Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Tabular/TabularExport.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ TabularExport >> workbook: anObject [
{ #category : #'as yet unclassified' }
TabularExport >> writeZip [

zip writeToFileNamed: workbook filename
workbook filename asFileReference binaryWriteStreamDo: [ :str |
zip writeTo: str ]

]
24 changes: 15 additions & 9 deletions src/Tabular/TabularExportImportTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,28 @@ TabularExportImportTest >> testExport8ExportManyWorksheets [
| wbk wsheet wbkimported wsheetimported rnd sheetNames cellPositions values |
wbk := TabularWorkbook new.
rnd := Random new.
sheetNames := (1 to: 10) collect: [ :i | 'Sheet ', i printString].
values := (1 to: 10) collect: [ :i | i even ifTrue: [rnd nextInt: 1000] ifFalse: [ (rnd nextInt: 100) printStringRadix: 16 ] ].
cellPositions := (1 to: 10) collect: [ :i | (rnd nextInt: 5) @ (rnd nextInt: 10) ].
1 to: 10 do: [ :i |
sheetNames := (1 to: 10) collect: [ :i | 'Sheet ' , i printString ].
values := (1 to: 10) collect: [ :i |
i even
ifTrue: [ rnd nextInteger: 1000 ]
ifFalse: [ (rnd nextInteger: 100) printStringRadix: 16 ] ].
cellPositions := (1 to: 10) collect: [ :i |
(rnd nextInteger: 5) @ (rnd nextInteger: 10) ].
1 to: 10 do: [ :i |
wsheet := TabularWorksheet new.
wsheet name: (sheetNames at: i).
wsheet at: (cellPositions at: i) putData: (values at: i).
wbk addSheet: wsheet].
wbk addSheet: wsheet ].

fname7 ensureDelete.
TabularXLSXExport workbook: wbk fileName: fname7.

wbkimported := XLSXImporter import: fname7 .
1 to: 10 do: [ :i |
wbkimported := XLSXImporter import: fname7.

1 to: 10 do: [ :i |
wsheetimported := wbkimported worksheets at: i.
self assert: wsheetimported name equals: (sheetNames at: i).
self assert: (wsheetimported at: (cellPositions at: i)) data equals: (values at: i)]
self
assert: (wsheetimported at: (cellPositions at: i)) data
equals: (values at: i) ]
]
1 change: 0 additions & 1 deletion src/Tabular/TabularWorksheetWriterWordML.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,3 @@ TabularWorksheetWriterWordML >> renderWorksheet [
ifTrue: [ self renderSheetData ]
"self renderMergeCells"
]

27 changes: 16 additions & 11 deletions src/Tabular/XLSXImporter.class.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"
I am ƖXLSXImporter which imports Excel files.

Key message for importing is in TabularCell>>extractFromXml: aXMLElement
When using a variant of my #import:Xyz: messages, you may pass a binary read stream or a FileReference. However, it is safer to pass a FileReference due to issues in Pharo's stream library and ZipArchive's use thereof. If interested in the gory details, see <https://github.com/pharo-project/pharo/issues/8657>

Key message for importing is TabularWorksheet>>#extractFromXml:

The attribute t=""s"" of a cell c seems to indicate that the content has
to be interpreted as a shared string.
Expand Down Expand Up @@ -50,21 +52,23 @@ Class {
}

{ #category : #importing }
XLSXImporter class >> import: aStream [
^ self new import: aStream
XLSXImporter class >> import: aFileReferenceOrStream [
"See class comment RE argument"
^ self new import: aFileReferenceOrStream


]

{ #category : #importing }
XLSXImporter class >> import: aFileReferenceOrAStream onlySheetsNamed: aCollection [
^ self new import: aFileReferenceOrAStream onlySheetsNamed: aCollection
XLSXImporter class >> import: aFileReferenceOrStream onlySheetsNamed: aCollection [
"See class comment RE argument"
^ self new import: aFileReferenceOrStream onlySheetsNamed: aCollection
]

{ #category : #importing }
XLSXImporter >> extractArchive: aStream [
(ZipArchive isZipArchive: aStream)
ifTrue: [archive := ZipArchive new readFrom: aStream]
ifTrue: [archive := ZipArchive new readFrom: (aStream position: 0) ]
ifFalse: [ self error: 'XLSX file do not represent valid Zip archive, as it should be' ]. "Maybe you missed it with XLS file?"


Expand Down Expand Up @@ -123,8 +127,9 @@ XLSXImporter >> getPathForSheet: rID [
]

{ #category : #importing }
XLSXImporter >> import: aStream [
self extractArchive: aStream.
XLSXImporter >> import: aFileReferenceOrStream [
"See class comment RE argument"
self extractArchive: aFileReferenceOrStream.
workbook := TabularWorkbook new. "self extractStyles."
self extractSharedStrings.
self extractWorksheets.
Expand All @@ -133,11 +138,11 @@ XLSXImporter >> import: aStream [
]

{ #category : #importing }
XLSXImporter >> import: aFileReference onlySheetsNamed: aCollection [

XLSXImporter >> import: aFileReferenceOrStream onlySheetsNamed: aCollection [
"See class comment RE argument"

sheetsToExtract := aCollection.
^ self import: aFileReference
^ self import: aFileReferenceOrStream
]

{ #category : #'as yet unclassified' }
Expand Down