stream_file.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // WriteS will write a row of cells to the current sheet. Every call to WriteS on the same sheet must
  51. // contain the same number of cells as the number of columns provided when the sheet was created or an error
  52. // will be returned. This function will always trigger a flush on success. WriteS supports all data types
  53. // and styles that are supported by StreamCell.
  54. func (sf *StreamFile) WriteS(cells []StreamCell) error {
  55. if sf.err != nil {
  56. return sf.err
  57. }
  58. err := sf.writeS(cells)
  59. if err != nil {
  60. sf.err = err
  61. return err
  62. }
  63. return sf.zipWriter.Flush()
  64. }
  65. func (sf *StreamFile) WriteAll(records [][]string) error {
  66. if sf.err != nil {
  67. return sf.err
  68. }
  69. for _, row := range records {
  70. err := sf.write(row)
  71. if err != nil {
  72. sf.err = err
  73. return err
  74. }
  75. }
  76. return sf.zipWriter.Flush()
  77. }
  78. // WriteAllS will write all the rows provided in records. All rows must have the same number of cells as
  79. // the number of columns given when creating the sheet. This function will always trigger a flush on success.
  80. // WriteS supports all data types and styles that are supported by StreamCell.
  81. func (sf *StreamFile) WriteAllS(records [][]StreamCell) error {
  82. if sf.err != nil {
  83. return sf.err
  84. }
  85. for _, row := range records {
  86. err := sf.writeS(row)
  87. if err != nil {
  88. sf.err = err
  89. return err
  90. }
  91. }
  92. return sf.zipWriter.Flush()
  93. }
  94. func (sf *StreamFile) write(cells []string) error {
  95. if sf.currentSheet == nil {
  96. return NoCurrentSheetError
  97. }
  98. if len(cells) != sf.currentSheet.columnCount {
  99. return WrongNumberOfRowsError
  100. }
  101. sf.currentSheet.rowCount++
  102. if err := sf.currentSheet.write(`<row r="` + strconv.Itoa(sf.currentSheet.rowCount) + `">`); err != nil {
  103. return err
  104. }
  105. for colIndex, cellData := range cells {
  106. // documentation for the c.t (cell.Type) attribute:
  107. // b (Boolean): Cell containing a boolean.
  108. // d (Date): Cell contains a date in the ISO 8601 format.
  109. // e (Error): Cell containing an error.
  110. // inlineStr (Inline String): Cell containing an (inline) rich string, i.e., one not in the shared string table.
  111. // 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).
  112. // n (Number): Cell containing a number.
  113. // s (Shared String): Cell containing a shared string.
  114. // str (String): Cell containing a formula string.
  115. cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
  116. cellType := "inlineStr"
  117. cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
  118. // Add in the style id if the cell isn't using the default style
  119. if colIndex < len(sf.currentSheet.styleIds) && sf.currentSheet.styleIds[colIndex] != 0 {
  120. cellOpen += ` s="` + strconv.Itoa(sf.currentSheet.styleIds[colIndex]) + `"`
  121. }
  122. cellOpen += `><is><t>`
  123. cellClose := `</t></is></c>`
  124. if err := sf.currentSheet.write(cellOpen); err != nil {
  125. return err
  126. }
  127. if err := xml.EscapeText(sf.currentSheet.writer, []byte(cellData)); err != nil {
  128. return err
  129. }
  130. if err := sf.currentSheet.write(cellClose); err != nil {
  131. return err
  132. }
  133. }
  134. if err := sf.currentSheet.write(`</row>`); err != nil {
  135. return err
  136. }
  137. return sf.zipWriter.Flush()
  138. }
  139. func (sf *StreamFile) writeS(cells []StreamCell) error {
  140. if sf.currentSheet == nil {
  141. return NoCurrentSheetError
  142. }
  143. if len(cells) != sf.currentSheet.columnCount {
  144. return WrongNumberOfRowsError
  145. }
  146. sf.currentSheet.rowCount++
  147. // Write the row opening
  148. if err := sf.currentSheet.write(`<row r="` + strconv.Itoa(sf.currentSheet.rowCount) + `">`); err != nil {
  149. return err
  150. }
  151. // Add cells one by one
  152. for colIndex, cell := range cells {
  153. cellParts, err := sf.marshallCell(cell, colIndex)
  154. if err != nil{
  155. return err
  156. }
  157. // Write the cell opening
  158. if err := sf.currentSheet.write(cellParts["cellOpen"]); err != nil {
  159. return err
  160. }
  161. // Write the cell contents opening
  162. if err := sf.currentSheet.write(cellParts["cellContentsOpen"]); err != nil {
  163. return err
  164. }
  165. // Write cell contents
  166. if err := xml.EscapeText(sf.currentSheet.writer, []byte(cellParts["cellContents"])); err != nil {
  167. return err
  168. }
  169. // Write cell contents ending
  170. if err := sf.currentSheet.write(cellParts["cellContentsClose"]); err != nil {
  171. return err
  172. }
  173. // Write the cell ending
  174. if err := sf.currentSheet.write(cellParts["cellClose"]); err != nil {
  175. return err
  176. }
  177. }
  178. // Write the row ending
  179. if err := sf.currentSheet.write(`</row>`); err != nil {
  180. return err
  181. }
  182. return sf.zipWriter.Flush()
  183. }
  184. func (sf *StreamFile) marshallCell(cell StreamCell, colIndex int) (map[string]string, error) {
  185. // Get the cell reference (location)
  186. cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
  187. // Get the cell type string
  188. cellType, err := getCellTypeAsString(cell.cellType)
  189. if err != nil {
  190. return nil, err
  191. }
  192. cellParts := make(map[string]string)
  193. // Build the XML cell opening
  194. cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
  195. // Add in the style id of the stream cell. If the streamStyle is empty, don't add a style,
  196. // default column style will be used
  197. if cell.cellStyle != (StreamStyle{}){
  198. if idx, ok := sf.styleIdMap[cell.cellStyle]; ok {
  199. cellOpen += ` s="` + strconv.Itoa(idx) + `"`
  200. } else {
  201. return nil, errors.New("trying to make use of a style that has not been added")
  202. }
  203. }
  204. cellOpen += `>`
  205. cellParts["cellOpen"] = cellOpen
  206. // The XML cell contents
  207. cellContentsOpen, cellContentsClose, err := getCellContentOpenAncCloseTags(cell.cellType)
  208. if err != nil {
  209. return nil, err
  210. }
  211. cellParts["cellContentsOpen"] = cellContentsOpen
  212. cellParts["cellContentsClose"] = cellContentsClose
  213. cellParts["cellContents"] = cell.cellData
  214. // The XMl cell ending
  215. cellClose := `</c>`
  216. cellParts["cellClose"] = cellClose
  217. return cellParts, nil
  218. }
  219. func getCellTypeAsString(cellType CellType) (string, error) {
  220. // documentation for the c.t (cell.Type) attribute:
  221. // b (Boolean): Cell containing a boolean.
  222. // d (Date): Cell contains a date in the ISO 8601 format.
  223. // e (Error): Cell containing an error.
  224. // inlineStr (Inline String): Cell containing an (inline) rich string, i.e., one not in the shared string table.
  225. // 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).
  226. // n (Number): Cell containing a number.
  227. // s (Shared String): Cell containing a shared string.
  228. // str (String): Cell containing a formula string.
  229. switch cellType {
  230. case CellTypeBool:
  231. return "b", nil
  232. case CellTypeDate:
  233. return "d", nil
  234. case CellTypeError:
  235. return "e", nil
  236. case CellTypeInline:
  237. return "inlineStr", nil
  238. case CellTypeNumeric:
  239. return "n", nil
  240. case CellTypeString:
  241. // TODO Currently shared strings are types as inline strings
  242. return "inlineStr", nil
  243. // return "s", nil
  244. case CellTypeStringFormula:
  245. return "str", nil
  246. default:
  247. return "", UnsupportedCellTypeError
  248. }
  249. }
  250. func getCellContentOpenAncCloseTags(cellType CellType) (string, string, error) {
  251. switch cellType {
  252. case CellTypeString:
  253. // TODO Currently shared strings are types as inline strings
  254. return `<is><t>`, `</t></is>`, nil
  255. case CellTypeInline:
  256. return `<is><t>`, `</t></is>`, nil
  257. case CellTypeStringFormula:
  258. // Formulas are currently not supported
  259. return ``, ``, UnsupportedCellTypeError
  260. default:
  261. return `<v>`, `</v>`, nil
  262. }
  263. }
  264. // Error reports any error that has occurred during a previous Write or Flush.
  265. func (sf *StreamFile) Error() error {
  266. return sf.err
  267. }
  268. func (sf *StreamFile) Flush() {
  269. if sf.err != nil {
  270. sf.err = sf.zipWriter.Flush()
  271. }
  272. }
  273. // NextSheet will switch to the next sheet. Sheets are selected in the same order they were added.
  274. // Once you leave a sheet, you cannot return to it.
  275. func (sf *StreamFile) NextSheet() error {
  276. if sf.err != nil {
  277. return sf.err
  278. }
  279. var sheetIndex int
  280. if sf.currentSheet != nil {
  281. if sf.currentSheet.index >= len(sf.xlsxFile.Sheets) {
  282. sf.err = AlreadyOnLastSheetError
  283. return AlreadyOnLastSheetError
  284. }
  285. if err := sf.writeSheetEnd(); err != nil {
  286. sf.currentSheet = nil
  287. sf.err = err
  288. return err
  289. }
  290. sheetIndex = sf.currentSheet.index
  291. }
  292. sheetIndex++
  293. sf.currentSheet = &streamSheet{
  294. index: sheetIndex,
  295. columnCount: len(sf.xlsxFile.Sheets[sheetIndex-1].Cols),
  296. styleIds: sf.styleIds[sheetIndex-1],
  297. rowCount: len(sf.xlsxFile.Sheets[sheetIndex-1].Rows),
  298. }
  299. sheetPath := sheetFilePathPrefix + strconv.Itoa(sf.currentSheet.index) + sheetFilePathSuffix
  300. fileWriter, err := sf.zipWriter.Create(sheetPath)
  301. if err != nil {
  302. sf.err = err
  303. return err
  304. }
  305. sf.currentSheet.writer = fileWriter
  306. if err := sf.writeSheetStart(); err != nil {
  307. sf.err = err
  308. return err
  309. }
  310. return nil
  311. }
  312. // Close closes the Stream File.
  313. // Any sheets that have not yet been written to will have an empty sheet created for them.
  314. func (sf *StreamFile) Close() error {
  315. if sf.err != nil {
  316. return sf.err
  317. }
  318. // If there are sheets that have not been written yet, call NextSheet() which will add files to the zip for them.
  319. // XLSX readers may error if the sheets registered in the metadata are not present in the file.
  320. if sf.currentSheet != nil {
  321. for sf.currentSheet.index < len(sf.xlsxFile.Sheets) {
  322. if err := sf.NextSheet(); err != nil {
  323. sf.err = err
  324. return err
  325. }
  326. }
  327. // Write the end of the last sheet.
  328. if err := sf.writeSheetEnd(); err != nil {
  329. sf.err = err
  330. return err
  331. }
  332. }
  333. err := sf.zipWriter.Close()
  334. if err != nil {
  335. sf.err = err
  336. }
  337. return err
  338. }
  339. // writeSheetStart will write the start of the Sheet's XML
  340. func (sf *StreamFile) writeSheetStart() error {
  341. if sf.currentSheet == nil {
  342. return NoCurrentSheetError
  343. }
  344. return sf.currentSheet.write(sf.sheetXmlPrefix[sf.currentSheet.index-1])
  345. }
  346. // writeSheetEnd will write the end of the Sheet's XML
  347. func (sf *StreamFile) writeSheetEnd() error {
  348. if sf.currentSheet == nil {
  349. return NoCurrentSheetError
  350. }
  351. if err := sf.currentSheet.write(endSheetDataTag); err != nil {
  352. return err
  353. }
  354. return sf.currentSheet.write(sf.sheetXmlSuffix[sf.currentSheet.index-1])
  355. }
  356. func (ss *streamSheet) write(data string) error {
  357. _, err := ss.writer.Write([]byte(data))
  358. return err
  359. }