stream_file.go 13 KB

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