stream_file.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "errors"
  6. "io"
  7. "strconv"
  8. )
  9. type StreamFile struct {
  10. xlsxFile *File
  11. sheetXmlPrefix []string
  12. sheetXmlSuffix []string
  13. zipWriter *zip.Writer
  14. currentSheet *streamSheet
  15. styleIds [][]int
  16. styleIdMap map[StreamStyle]int
  17. streamingCellMetadatas map[int]*StreamingCellMetadata
  18. sheetStreamStyles map[int]cellStreamStyle
  19. sheetDefaultCellType map[int]defaultCellType
  20. err error
  21. }
  22. type streamSheet struct {
  23. // sheetIndex is the XLSX sheet index, which starts at 1
  24. index int
  25. // The number of rows that have been written to the sheet so far
  26. rowCount int
  27. // The number of columns in the sheet
  28. columnCount int
  29. // The writer to write to this sheet's file in the XLSX Zip file
  30. writer io.Writer
  31. styleIds []int
  32. }
  33. var (
  34. NoCurrentSheetError = errors.New("no Current Sheet")
  35. WrongNumberOfRowsError = errors.New("invalid number of cells passed to Write. All calls to Write on the same sheet must have the same number of cells")
  36. AlreadyOnLastSheetError = errors.New("NextSheet() called, but already on last sheet")
  37. UnsupportedCellTypeError = errors.New("the given cell type is not supported")
  38. )
  39. // Write will write a row of cells to the current sheet. Every call to Write on the same sheet must contain the
  40. // same number of cells as the header provided when the sheet was created or an error will be returned. This function
  41. // will always trigger a flush on success. Currently the only supported data type is string data.
  42. func (sf *StreamFile) Write(cells []string) error {
  43. if sf.err != nil {
  44. return sf.err
  45. }
  46. err := sf.write(cells)
  47. if err != nil {
  48. sf.err = err
  49. return err
  50. }
  51. return sf.zipWriter.Flush()
  52. }
  53. // WriteWithColumnDefaultMetadata will write a row of cells to the current sheet. Every call to WriteWithColumnDefaultMetadata
  54. // on the same sheet must contain the same number of cells as the header provided when the sheet was created or
  55. // an error will be returned. This function will always trigger a flush on success. Each cell will be encoded with the
  56. // default StreamingCellMetadata of the column that it belongs to. However, if the cell data string cannot be
  57. // parsed into the cell type in StreamingCellMetadata, we fall back on encoding the cell as a string and giving it a default
  58. // string style
  59. func (sf *StreamFile) WriteWithColumnDefaultMetadata(cells []string) error {
  60. if sf.err != nil {
  61. return sf.err
  62. }
  63. err := sf.writeWithColumnDefaultMetadata(cells)
  64. if err != nil {
  65. sf.err = err
  66. return err
  67. }
  68. return sf.zipWriter.Flush()
  69. }
  70. // WriteS will write a row of cells to the current sheet. Every call to WriteS on the same sheet must
  71. // contain the same number of cells as the number of columns provided when the sheet was created or an error
  72. // will be returned. This function will always trigger a flush on success. WriteS supports all data types
  73. // and styles that are supported by StreamCell.
  74. func (sf *StreamFile) WriteS(cells []StreamCell) error {
  75. if sf.err != nil {
  76. return sf.err
  77. }
  78. err := sf.writeS(cells)
  79. if err != nil {
  80. sf.err = err
  81. return err
  82. }
  83. return sf.zipWriter.Flush()
  84. }
  85. func (sf *StreamFile) WriteAll(records [][]string) error {
  86. if sf.err != nil {
  87. return sf.err
  88. }
  89. for _, row := range records {
  90. err := sf.write(row)
  91. if err != nil {
  92. sf.err = err
  93. return err
  94. }
  95. }
  96. return sf.zipWriter.Flush()
  97. }
  98. // WriteAllS will write all the rows provided in records. All rows must have the same number of cells as
  99. // the number of columns given when creating the sheet. This function will always trigger a flush on success.
  100. // WriteAllS supports all data types and styles that are supported by StreamCell.
  101. func (sf *StreamFile) WriteAllS(records [][]StreamCell) error {
  102. if sf.err != nil {
  103. return sf.err
  104. }
  105. for _, row := range records {
  106. err := sf.writeS(row)
  107. if err != nil {
  108. sf.err = err
  109. return err
  110. }
  111. }
  112. return sf.zipWriter.Flush()
  113. }
  114. func (sf *StreamFile) write(cells []string) error {
  115. if sf.currentSheet == nil {
  116. return NoCurrentSheetError
  117. }
  118. cellCount := len(cells)
  119. if cellCount != sf.currentSheet.columnCount {
  120. if sf.currentSheet.columnCount != 0 {
  121. return WrongNumberOfRowsError
  122. }
  123. sf.currentSheet.columnCount = cellCount
  124. }
  125. sf.currentSheet.rowCount++
  126. if err := sf.currentSheet.write(`<row r="` + strconv.Itoa(sf.currentSheet.rowCount) + `">`); err != nil {
  127. return err
  128. }
  129. for colIndex, cellData := range cells {
  130. // documentation for the c.t (cell.Type) attribute:
  131. // b (Boolean): Cell containing a boolean.
  132. // d (Date): Cell contains a date in the ISO 8601 format.
  133. // e (Error): Cell containing an error.
  134. // inlineStr (Inline String): Cell containing an (inline) rich string, i.e., one not in the shared string table.
  135. // If this cell type is used, then the cell value is in the is element rather than the v element in the cell (c element).
  136. // n (Number): Cell containing a number.
  137. // s (Shared String): Cell containing a shared string.
  138. // str (String): Cell containing a formula string.
  139. cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
  140. cellType := "inlineStr"
  141. cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
  142. // Add in the style id if the cell isn't using the default style
  143. if colIndex < len(sf.currentSheet.styleIds) && sf.currentSheet.styleIds[colIndex] != 0 {
  144. cellOpen += ` s="` + strconv.Itoa(sf.currentSheet.styleIds[colIndex]) + `"`
  145. }
  146. cellOpen += `><is><t>`
  147. cellClose := `</t></is></c>`
  148. if err := sf.currentSheet.write(cellOpen); err != nil {
  149. return err
  150. }
  151. if err := xml.EscapeText(sf.currentSheet.writer, []byte(cellData)); err != nil {
  152. return err
  153. }
  154. if err := sf.currentSheet.write(cellClose); err != nil {
  155. return err
  156. }
  157. }
  158. if err := sf.currentSheet.write(`</row>`); err != nil {
  159. return err
  160. }
  161. return sf.zipWriter.Flush()
  162. }
  163. func (sf *StreamFile) writeWithColumnDefaultMetadata(cells []string) error {
  164. if sf.currentSheet == nil {
  165. return NoCurrentSheetError
  166. }
  167. sheetIndex := sf.currentSheet.index - 1
  168. currentSheet := sf.xlsxFile.Sheets[sheetIndex]
  169. var streamCells []StreamCell
  170. if currentSheet.Cols == nil {
  171. panic("trying to use uninitialised ColStore")
  172. }
  173. if len(cells) != sf.currentSheet.columnCount {
  174. if sf.currentSheet.columnCount != 0 {
  175. return WrongNumberOfRowsError
  176. }
  177. sf.currentSheet.columnCount = len(cells)
  178. }
  179. cSS := sf.sheetStreamStyles[sheetIndex]
  180. cDCT := sf.sheetDefaultCellType[sheetIndex]
  181. for ci, c := range cells {
  182. // TODO: Legacy code paths like `StreamFileBuilder.AddSheet` could
  183. // leave style empty and if cell data cannot be parsed into cell type then
  184. // we need a sensible default StreamStyle to fall back to
  185. style := StreamStyleDefaultString
  186. // Because `cellData` could be anything we need to attempt to
  187. // parse into the default cell type and if parsing fails fall back
  188. // to some sensible default
  189. cellType := CellTypeInline
  190. if dct, ok := cDCT[ci]; ok {
  191. defaultType := dct
  192. cellType = defaultType.fallbackTo(cells[ci], CellTypeString)
  193. if ss, ok := cSS[ci]; ok {
  194. // TODO: Again `CellType` could be nil if sheet was created through
  195. // legacy code path so, like style, hardcoding for now
  196. if defaultType != nil && *defaultType == cellType {
  197. style = ss
  198. }
  199. }
  200. }
  201. streamCells = append(
  202. streamCells,
  203. NewStreamCell(
  204. c,
  205. style,
  206. cellType,
  207. ))
  208. }
  209. return sf.writeS(streamCells)
  210. }
  211. func (sf *StreamFile) writeS(cells []StreamCell) error {
  212. if sf.currentSheet == nil {
  213. return NoCurrentSheetError
  214. }
  215. if len(cells) != sf.currentSheet.columnCount {
  216. if sf.currentSheet.columnCount != 0 {
  217. return WrongNumberOfRowsError
  218. }
  219. sf.currentSheet.columnCount = len(cells)
  220. }
  221. sf.currentSheet.rowCount++
  222. // Write the row opening
  223. if err := sf.currentSheet.write(`<row r="` + strconv.Itoa(sf.currentSheet.rowCount) + `">`); err != nil {
  224. return err
  225. }
  226. // Add cells one by one
  227. for colIndex, cell := range cells {
  228. xlsxCell, err := sf.getXlsxCell(cell, colIndex)
  229. if err != nil {
  230. return err
  231. }
  232. marshaledCell, err := xml.Marshal(xlsxCell)
  233. if err != nil {
  234. return nil
  235. }
  236. // Write the cell
  237. if _, err := sf.currentSheet.writer.Write(marshaledCell); err != nil {
  238. return err
  239. }
  240. }
  241. // Write the row ending
  242. if err := sf.currentSheet.write(`</row>`); err != nil {
  243. return err
  244. }
  245. return sf.zipWriter.Flush()
  246. }
  247. func (sf *StreamFile) getXlsxCell(cell StreamCell, colIndex int) (xlsxC, error) {
  248. // Get the cell reference (location)
  249. cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
  250. var cellStyleId int
  251. if cell.cellStyle != (StreamStyle{}) {
  252. if idx, ok := sf.styleIdMap[cell.cellStyle]; ok {
  253. cellStyleId = idx
  254. } else {
  255. return xlsxC{}, errors.New("trying to make use of a style that has not been added")
  256. }
  257. }
  258. return makeXlsxCell(cell.cellType, cellCoordinate, cellStyleId, cell.cellData)
  259. }
  260. func makeXlsxCell(cellType CellType, cellCoordinate string, cellStyleId int, cellData string) (xlsxC, error) {
  261. // documentation for the c.t (cell.Type) attribute:
  262. // b (Boolean): Cell containing a boolean.
  263. // d (Date): Cell contains a date in the ISO 8601 format.
  264. // e (Error): Cell containing an error.
  265. // inlineStr (Inline String): Cell containing an (inline) rich string, i.e., one not in the shared string table.
  266. // If this cell type is used, then the cell value is in the is element rather than the v element in the cell (c element).
  267. // n (Number): Cell containing a number.
  268. // s (Shared String): Cell containing a shared string.
  269. // str (String): Cell containing a formula string.
  270. switch cellType {
  271. case CellTypeBool:
  272. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "b", V: cellData}, nil
  273. // Dates are better represented using CellTyleNumeric and the date formatting
  274. //case CellTypeDate:
  275. //return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "d", V: cellData}, nil
  276. case CellTypeError:
  277. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "e", V: cellData}, nil
  278. case CellTypeInline:
  279. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "inlineStr", Is: &xlsxSI{T: cellData}}, nil
  280. case CellTypeNumeric:
  281. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "n", V: cellData}, nil
  282. case CellTypeString:
  283. // TODO Currently shared strings are types as inline strings
  284. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "inlineStr", Is: &xlsxSI{T: cellData}}, nil
  285. // TODO currently not supported
  286. // case CellTypeStringFormula:
  287. // return xlsxC{}, UnsupportedCellTypeError
  288. default:
  289. return xlsxC{}, UnsupportedCellTypeError
  290. }
  291. }
  292. // Error reports any error that has occurred during a previous Write or Flush.
  293. func (sf *StreamFile) Error() error {
  294. return sf.err
  295. }
  296. func (sf *StreamFile) Flush() {
  297. if sf.err != nil {
  298. sf.err = sf.zipWriter.Flush()
  299. }
  300. }
  301. // NextSheet will switch to the next sheet. Sheets are selected in the same order they were added.
  302. // Once you leave a sheet, you cannot return to it.
  303. func (sf *StreamFile) NextSheet() error {
  304. if sf.err != nil {
  305. return sf.err
  306. }
  307. var sheetIndex int
  308. if sf.currentSheet != nil {
  309. if sf.currentSheet.index >= len(sf.xlsxFile.Sheets) {
  310. sf.err = AlreadyOnLastSheetError
  311. return AlreadyOnLastSheetError
  312. }
  313. if err := sf.writeSheetEnd(); err != nil {
  314. sf.currentSheet = nil
  315. sf.err = err
  316. return err
  317. }
  318. sheetIndex = sf.currentSheet.index
  319. }
  320. sheetIndex++
  321. sf.currentSheet = &streamSheet{
  322. index: sheetIndex,
  323. columnCount: sf.xlsxFile.Sheets[sheetIndex-1].MaxCol,
  324. styleIds: sf.styleIds[sheetIndex-1],
  325. rowCount: len(sf.xlsxFile.Sheets[sheetIndex-1].Rows),
  326. }
  327. sheetPath := sheetFilePathPrefix + strconv.Itoa(sf.currentSheet.index) + sheetFilePathSuffix
  328. fileWriter, err := sf.zipWriter.Create(sheetPath)
  329. if err != nil {
  330. sf.err = err
  331. return err
  332. }
  333. sf.currentSheet.writer = fileWriter
  334. if err := sf.writeSheetStart(); err != nil {
  335. sf.err = err
  336. return err
  337. }
  338. return nil
  339. }
  340. // Close closes the Stream File.
  341. // Any sheets that have not yet been written to will have an empty sheet created for them.
  342. func (sf *StreamFile) Close() error {
  343. if sf.err != nil {
  344. return sf.err
  345. }
  346. // If there are sheets that have not been written yet, call NextSheet() which will add files to the zip for them.
  347. // XLSX readers may error if the sheets registered in the metadata are not present in the file.
  348. if sf.currentSheet != nil {
  349. for sf.currentSheet.index < len(sf.xlsxFile.Sheets) {
  350. if err := sf.NextSheet(); err != nil {
  351. sf.err = err
  352. return err
  353. }
  354. }
  355. // Write the end of the last sheet.
  356. if err := sf.writeSheetEnd(); err != nil {
  357. sf.err = err
  358. return err
  359. }
  360. }
  361. err := sf.zipWriter.Close()
  362. if err != nil {
  363. sf.err = err
  364. }
  365. return err
  366. }
  367. // writeSheetStart will write the start of the Sheet's XML
  368. func (sf *StreamFile) writeSheetStart() error {
  369. if sf.currentSheet == nil {
  370. return NoCurrentSheetError
  371. }
  372. return sf.currentSheet.write(sf.sheetXmlPrefix[sf.currentSheet.index-1])
  373. }
  374. // writeSheetEnd will write the end of the Sheet's XML
  375. func (sf *StreamFile) writeSheetEnd() error {
  376. if sf.currentSheet == nil {
  377. return NoCurrentSheetError
  378. }
  379. if err := sf.currentSheet.write(endSheetDataTag); err != nil {
  380. return err
  381. }
  382. return sf.currentSheet.write(sf.sheetXmlSuffix[sf.currentSheet.index-1])
  383. }
  384. func (ss *streamSheet) write(data string) error {
  385. _, err := ss.writer.Write([]byte(data))
  386. return err
  387. }