stream_file.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. // WriteWithStyle will write a row of cells to the current sheet. Every call to WriteWithStyle on the same sheet must
  51. // contain the same number of cells as the header provided when the sheet was created or an error will be returned.
  52. // This function will always trigger a flush on success. WriteWithStyle supports all data types and styles that
  53. // are supported by StreamCell.
  54. func (sf *StreamFile) WriteWithStyle(cells []StreamCell) error {
  55. if sf.err != nil {
  56. return sf.err
  57. }
  58. err := sf.writeWithStyle(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. // WriteAllWithStyle will write all the rows provided in records. All rows must have the same number of cells as
  79. // the headers. This function will always trigger a flush on success. WriteWithStyle supports all data types and
  80. // styles that are supported by StreamCell.
  81. func (sf *StreamFile) WriteAllWithStyle(records [][]StreamCell) error{
  82. if sf.err != nil {
  83. return sf.err
  84. }
  85. for _, row := range records {
  86. err := sf.writeWithStyle(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) writeWithStyle(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. // Get the cell reference (location)
  154. cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
  155. // Get the cell type string
  156. cellType, err := getCellTypeAsString(cell.cellType)
  157. if err != nil {
  158. return err
  159. }
  160. // Build the XML cell opening
  161. cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
  162. // Add in the style id of the stream cell.
  163. if idx, ok := sf.styleIdMap[cell.cellStyle]; ok {
  164. cellOpen += ` s="` + strconv.Itoa(idx) + `"`
  165. } else {
  166. return errors.New("trying to make use of a style that has not been added")
  167. }
  168. cellOpen += `>`
  169. // The XML cell contents
  170. cellContentsOpen, cellContentsClose, err := getCellContentOpenAncCloseTags(cell.cellType)
  171. if err != nil {
  172. return err
  173. }
  174. // The XMl cell ending
  175. cellClose := `</c>`
  176. // Write the cell opening
  177. if err := sf.currentSheet.write(cellOpen); err != nil {
  178. return err
  179. }
  180. // Write the cell contents opening
  181. if err := sf.currentSheet.write(cellContentsOpen); err != nil {
  182. return err
  183. }
  184. // Write cell contents
  185. if err:= xml.EscapeText(sf.currentSheet.writer, []byte(cell.cellData)); err != nil {
  186. return err
  187. }
  188. // Write cell contents ending
  189. if err := sf.currentSheet.write(cellContentsClose); err != nil {
  190. return err
  191. }
  192. // Write the cell ending
  193. if err := sf.currentSheet.write(cellClose); err != nil {
  194. return err
  195. }
  196. }
  197. // Write the row ending
  198. if err := sf.currentSheet.write(`</row>`); err != nil {
  199. return err
  200. }
  201. return sf.zipWriter.Flush()
  202. }
  203. func getCellTypeAsString(cellType CellType) (string, error) {
  204. // documentation for the c.t (cell.Type) attribute:
  205. // b (Boolean): Cell containing a boolean.
  206. // d (Date): Cell contains a date in the ISO 8601 format.
  207. // e (Error): Cell containing an error.
  208. // inlineStr (Inline String): Cell containing an (inline) rich string, i.e., one not in the shared string table.
  209. // 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).
  210. // n (Number): Cell containing a number.
  211. // s (Shared String): Cell containing a shared string.
  212. // str (String): Cell containing a formula string.
  213. switch cellType{
  214. case CellTypeBool:
  215. return "b", nil
  216. case CellTypeDate:
  217. return "d", nil
  218. case CellTypeError:
  219. return "e", nil
  220. case CellTypeInline:
  221. return "inlineStr", nil
  222. case CellTypeNumeric:
  223. return "n", nil
  224. case CellTypeString:
  225. // TODO Currently shared strings are types as inline strings
  226. return "inlineStr", nil
  227. // return "s", nil
  228. case CellTypeStringFormula:
  229. return "str", nil
  230. default:
  231. return "", UnsupportedCellTypeError
  232. }
  233. }
  234. func getCellContentOpenAncCloseTags(cellType CellType) (string, string, error) {
  235. switch cellType{
  236. case CellTypeString:
  237. // TODO Currently shared strings are types as inline strings
  238. return `<is><t>`, `</t></is>`, nil
  239. case CellTypeInline:
  240. return `<is><t>`, `</t></is>`, nil
  241. case CellTypeStringFormula:
  242. // Formulas are currently not supported
  243. return ``, ``, UnsupportedCellTypeError
  244. default:
  245. return `<v>`, `</v>`, nil
  246. }
  247. }
  248. // Error reports any error that has occurred during a previous Write or Flush.
  249. func (sf *StreamFile) Error() error {
  250. return sf.err
  251. }
  252. func (sf *StreamFile) Flush() {
  253. if sf.err != nil {
  254. sf.err = sf.zipWriter.Flush()
  255. }
  256. }
  257. // NextSheet will switch to the next sheet. Sheets are selected in the same order they were added.
  258. // Once you leave a sheet, you cannot return to it.
  259. func (sf *StreamFile) NextSheet() error {
  260. if sf.err != nil {
  261. return sf.err
  262. }
  263. var sheetIndex int
  264. if sf.currentSheet != nil {
  265. if sf.currentSheet.index >= len(sf.xlsxFile.Sheets) {
  266. sf.err = AlreadyOnLastSheetError
  267. return AlreadyOnLastSheetError
  268. }
  269. if err := sf.writeSheetEnd(); err != nil {
  270. sf.currentSheet = nil
  271. sf.err = err
  272. return err
  273. }
  274. sheetIndex = sf.currentSheet.index
  275. }
  276. sheetIndex++
  277. sf.currentSheet = &streamSheet{
  278. index: sheetIndex,
  279. columnCount: len(sf.xlsxFile.Sheets[sheetIndex-1].Cols),
  280. styleIds: sf.styleIds[sheetIndex-1],
  281. rowCount: 1,
  282. }
  283. sheetPath := sheetFilePathPrefix + strconv.Itoa(sf.currentSheet.index) + sheetFilePathSuffix
  284. fileWriter, err := sf.zipWriter.Create(sheetPath)
  285. if err != nil {
  286. sf.err = err
  287. return err
  288. }
  289. sf.currentSheet.writer = fileWriter
  290. if err := sf.writeSheetStart(); err != nil {
  291. sf.err = err
  292. return err
  293. }
  294. return nil
  295. }
  296. // Close closes the Stream File.
  297. // Any sheets that have not yet been written to will have an empty sheet created for them.
  298. func (sf *StreamFile) Close() error {
  299. if sf.err != nil {
  300. return sf.err
  301. }
  302. // If there are sheets that have not been written yet, call NextSheet() which will add files to the zip for them.
  303. // XLSX readers may error if the sheets registered in the metadata are not present in the file.
  304. if sf.currentSheet != nil {
  305. for sf.currentSheet.index < len(sf.xlsxFile.Sheets) {
  306. if err := sf.NextSheet(); err != nil {
  307. sf.err = err
  308. return err
  309. }
  310. }
  311. // Write the end of the last sheet.
  312. if err := sf.writeSheetEnd(); err != nil {
  313. sf.err = err
  314. return err
  315. }
  316. }
  317. err := sf.zipWriter.Close()
  318. if err != nil {
  319. sf.err = err
  320. }
  321. return err
  322. }
  323. // writeSheetStart will write the start of the Sheet's XML
  324. func (sf *StreamFile) writeSheetStart() error {
  325. if sf.currentSheet == nil {
  326. return NoCurrentSheetError
  327. }
  328. return sf.currentSheet.write(sf.sheetXmlPrefix[sf.currentSheet.index-1])
  329. }
  330. // writeSheetEnd will write the end of the Sheet's XML
  331. func (sf *StreamFile) writeSheetEnd() error {
  332. if sf.currentSheet == nil {
  333. return NoCurrentSheetError
  334. }
  335. if err := sf.currentSheet.write(endSheetDataTag); err != nil {
  336. return err
  337. }
  338. return sf.currentSheet.write(sf.sheetXmlSuffix[sf.currentSheet.index-1])
  339. }
  340. func (ss *streamSheet) write(data string) error {
  341. _, err := ss.writer.Write([]byte(data))
  342. return err
  343. }