stream_file.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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) error {
  57. if sf.err != nil {
  58. return sf.err
  59. }
  60. err := sf.writeWithColumnDefaultMetadata(cells)
  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) error {
  157. if sf.currentSheet == nil {
  158. return NoCurrentSheetError
  159. }
  160. if len(cells) != sf.currentSheet.columnCount {
  161. return WrongNumberOfRowsError
  162. }
  163. currentSheet := sf.xlsxFile.Sheets[sf.currentSheet.index-1]
  164. var streamCells []StreamCell
  165. for colIndex, col := range currentSheet.Cols {
  166. // TODO: Legacy code paths like `StreamFileBuilder.AddSheet` could
  167. // leave style empty and if cell data cannot be parsed into cell type then
  168. // we need a sensible default StreamStyle to fall back to
  169. style := StreamStyleDefaultString
  170. // Because `cellData` could be anything we need to attempt to
  171. // parse into the default cell type and if parsing fails fall back
  172. // to some sensible default
  173. defaultType := col.defaultCellType
  174. // TODO: Again `CellType` could be nil if sheet was created through
  175. // legacy code path so, like style, hardcoding for now
  176. cellType := defaultType.fallbackTo(cells[colIndex], CellTypeString)
  177. if defaultType != nil && *defaultType == cellType {
  178. style = col.GetStreamStyle()
  179. }
  180. streamCells = append(
  181. streamCells,
  182. NewStreamCell(
  183. cells[colIndex],
  184. style,
  185. cellType,
  186. ))
  187. }
  188. return sf.writeS(streamCells)
  189. }
  190. func (sf *StreamFile) writeS(cells []StreamCell) error {
  191. if sf.currentSheet == nil {
  192. return NoCurrentSheetError
  193. }
  194. if len(cells) != sf.currentSheet.columnCount {
  195. return WrongNumberOfRowsError
  196. }
  197. sf.currentSheet.rowCount++
  198. // Write the row opening
  199. if err := sf.currentSheet.write(`<row r="` + strconv.Itoa(sf.currentSheet.rowCount) + `">`); err != nil {
  200. return err
  201. }
  202. // Add cells one by one
  203. for colIndex, cell := range cells {
  204. xlsxCell, err := sf.getXlsxCell(cell, colIndex)
  205. if err != nil {
  206. return err
  207. }
  208. marshaledCell, err := xml.Marshal(xlsxCell)
  209. if err != nil {
  210. return nil
  211. }
  212. // Write the cell
  213. if _, err := sf.currentSheet.writer.Write(marshaledCell); err != nil {
  214. return err
  215. }
  216. }
  217. // Write the row ending
  218. if err := sf.currentSheet.write(`</row>`); err != nil {
  219. return err
  220. }
  221. return sf.zipWriter.Flush()
  222. }
  223. func (sf *StreamFile) getXlsxCell(cell StreamCell, colIndex int) (xlsxC, error) {
  224. // Get the cell reference (location)
  225. cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
  226. var cellStyleId int
  227. if cell.cellStyle != (StreamStyle{}) {
  228. if idx, ok := sf.styleIdMap[cell.cellStyle]; ok {
  229. cellStyleId = idx
  230. } else {
  231. return xlsxC{}, errors.New("trying to make use of a style that has not been added")
  232. }
  233. }
  234. return makeXlsxCell(cell.cellType, cellCoordinate, cellStyleId, cell.cellData)
  235. }
  236. func makeXlsxCell(cellType CellType, cellCoordinate string, cellStyleId int, cellData string) (xlsxC, error) {
  237. // documentation for the c.t (cell.Type) attribute:
  238. // b (Boolean): Cell containing a boolean.
  239. // d (Date): Cell contains a date in the ISO 8601 format.
  240. // e (Error): Cell containing an error.
  241. // inlineStr (Inline String): Cell containing an (inline) rich string, i.e., one not in the shared string table.
  242. // 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).
  243. // n (Number): Cell containing a number.
  244. // s (Shared String): Cell containing a shared string.
  245. // str (String): Cell containing a formula string.
  246. switch cellType {
  247. case CellTypeBool:
  248. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "b", V: cellData}, nil
  249. // Dates are better represented using CellTyleNumeric and the date formatting
  250. //case CellTypeDate:
  251. //return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "d", V: cellData}, nil
  252. case CellTypeError:
  253. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "e", V: cellData}, nil
  254. case CellTypeInline:
  255. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "inlineStr", Is: &xlsxSI{T: cellData}}, nil
  256. case CellTypeNumeric:
  257. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "n", V: cellData}, nil
  258. case CellTypeString:
  259. // TODO Currently shared strings are types as inline strings
  260. return xlsxC{XMLName: xml.Name{Local: "c"}, R: cellCoordinate, S: cellStyleId, T: "inlineStr", Is: &xlsxSI{T: cellData}}, nil
  261. // TODO currently not supported
  262. // case CellTypeStringFormula:
  263. // return xlsxC{}, UnsupportedCellTypeError
  264. default:
  265. return xlsxC{}, UnsupportedCellTypeError
  266. }
  267. }
  268. // Error reports any error that has occurred during a previous Write or Flush.
  269. func (sf *StreamFile) Error() error {
  270. return sf.err
  271. }
  272. func (sf *StreamFile) Flush() {
  273. if sf.err != nil {
  274. sf.err = sf.zipWriter.Flush()
  275. }
  276. }
  277. // NextSheet will switch to the next sheet. Sheets are selected in the same order they were added.
  278. // Once you leave a sheet, you cannot return to it.
  279. func (sf *StreamFile) NextSheet() error {
  280. if sf.err != nil {
  281. return sf.err
  282. }
  283. var sheetIndex int
  284. if sf.currentSheet != nil {
  285. if sf.currentSheet.index >= len(sf.xlsxFile.Sheets) {
  286. sf.err = AlreadyOnLastSheetError
  287. return AlreadyOnLastSheetError
  288. }
  289. if err := sf.writeSheetEnd(); err != nil {
  290. sf.currentSheet = nil
  291. sf.err = err
  292. return err
  293. }
  294. sheetIndex = sf.currentSheet.index
  295. }
  296. sheetIndex++
  297. sf.currentSheet = &streamSheet{
  298. index: sheetIndex,
  299. columnCount: len(sf.xlsxFile.Sheets[sheetIndex-1].Cols),
  300. styleIds: sf.styleIds[sheetIndex-1],
  301. rowCount: len(sf.xlsxFile.Sheets[sheetIndex-1].Rows),
  302. }
  303. sheetPath := sheetFilePathPrefix + strconv.Itoa(sf.currentSheet.index) + sheetFilePathSuffix
  304. fileWriter, err := sf.zipWriter.Create(sheetPath)
  305. if err != nil {
  306. sf.err = err
  307. return err
  308. }
  309. sf.currentSheet.writer = fileWriter
  310. if err := sf.writeSheetStart(); err != nil {
  311. sf.err = err
  312. return err
  313. }
  314. return nil
  315. }
  316. // Close closes the Stream File.
  317. // Any sheets that have not yet been written to will have an empty sheet created for them.
  318. func (sf *StreamFile) Close() error {
  319. if sf.err != nil {
  320. return sf.err
  321. }
  322. // If there are sheets that have not been written yet, call NextSheet() which will add files to the zip for them.
  323. // XLSX readers may error if the sheets registered in the metadata are not present in the file.
  324. if sf.currentSheet != nil {
  325. for sf.currentSheet.index < len(sf.xlsxFile.Sheets) {
  326. if err := sf.NextSheet(); err != nil {
  327. sf.err = err
  328. return err
  329. }
  330. }
  331. // Write the end of the last sheet.
  332. if err := sf.writeSheetEnd(); err != nil {
  333. sf.err = err
  334. return err
  335. }
  336. }
  337. err := sf.zipWriter.Close()
  338. if err != nil {
  339. sf.err = err
  340. }
  341. return err
  342. }
  343. // writeSheetStart will write the start of the Sheet's XML
  344. func (sf *StreamFile) writeSheetStart() error {
  345. if sf.currentSheet == nil {
  346. return NoCurrentSheetError
  347. }
  348. return sf.currentSheet.write(sf.sheetXmlPrefix[sf.currentSheet.index-1])
  349. }
  350. // writeSheetEnd will write the end of the Sheet's XML
  351. func (sf *StreamFile) writeSheetEnd() error {
  352. if sf.currentSheet == nil {
  353. return NoCurrentSheetError
  354. }
  355. if err := sf.currentSheet.write(endSheetDataTag); err != nil {
  356. return err
  357. }
  358. return sf.currentSheet.write(sf.sheetXmlSuffix[sf.currentSheet.index-1])
  359. }
  360. func (ss *streamSheet) write(data string) error {
  361. _, err := ss.writer.Write([]byte(data))
  362. return err
  363. }