file.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.10 or later.
  9. package excelize
  10. import (
  11. "archive/zip"
  12. "bytes"
  13. "fmt"
  14. "io"
  15. "os"
  16. )
  17. // NewFile provides a function to create new file by default template. For
  18. // example:
  19. //
  20. // xlsx := NewFile()
  21. //
  22. func NewFile() *File {
  23. file := make(map[string][]byte)
  24. file["_rels/.rels"] = []byte(XMLHeader + templateRels)
  25. file["docProps/app.xml"] = []byte(XMLHeader + templateDocpropsApp)
  26. file["docProps/core.xml"] = []byte(XMLHeader + templateDocpropsCore)
  27. file["xl/_rels/workbook.xml.rels"] = []byte(XMLHeader + templateWorkbookRels)
  28. file["xl/theme/theme1.xml"] = []byte(XMLHeader + templateTheme)
  29. file["xl/worksheets/sheet1.xml"] = []byte(XMLHeader + templateSheet)
  30. file["xl/styles.xml"] = []byte(XMLHeader + templateStyles)
  31. file["xl/workbook.xml"] = []byte(XMLHeader + templateWorkbook)
  32. file["[Content_Types].xml"] = []byte(XMLHeader + templateContentTypes)
  33. f := newFile()
  34. f.SheetCount, f.XLSX = 1, file
  35. f.CalcChain = f.calcChainReader()
  36. f.Comments = make(map[string]*xlsxComments)
  37. f.ContentTypes = f.contentTypesReader()
  38. f.Drawings = make(map[string]*xlsxWsDr)
  39. f.Styles = f.stylesReader()
  40. f.DecodeVMLDrawing = make(map[string]*decodeVmlDrawing)
  41. f.VMLDrawing = make(map[string]*vmlDrawing)
  42. f.WorkBook = f.workbookReader()
  43. f.Relationships = make(map[string]*xlsxRelationships)
  44. f.Relationships["xl/_rels/workbook.xml.rels"] = f.relsReader("xl/_rels/workbook.xml.rels")
  45. f.Sheet["xl/worksheets/sheet1.xml"], _ = f.workSheetReader("Sheet1")
  46. f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
  47. f.Theme = f.themeReader()
  48. return f
  49. }
  50. // Save provides a function to override the xlsx file with origin path.
  51. func (f *File) Save() error {
  52. if f.Path == "" {
  53. return fmt.Errorf("no path defined for file, consider File.WriteTo or File.Write")
  54. }
  55. return f.SaveAs(f.Path)
  56. }
  57. // SaveAs provides a function to create or update to an xlsx file at the
  58. // provided path.
  59. func (f *File) SaveAs(name string) error {
  60. file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
  61. if err != nil {
  62. return err
  63. }
  64. defer file.Close()
  65. return f.Write(file)
  66. }
  67. // Write provides a function to write to an io.Writer.
  68. func (f *File) Write(w io.Writer) error {
  69. _, err := f.WriteTo(w)
  70. return err
  71. }
  72. // WriteTo implements io.WriterTo to write the file.
  73. func (f *File) WriteTo(w io.Writer) (int64, error) {
  74. buf, err := f.WriteToBuffer()
  75. if err != nil {
  76. return 0, err
  77. }
  78. return buf.WriteTo(w)
  79. }
  80. // WriteToBuffer provides a function to get bytes.Buffer from the saved file.
  81. func (f *File) WriteToBuffer() (*bytes.Buffer, error) {
  82. buf := new(bytes.Buffer)
  83. zw := zip.NewWriter(buf)
  84. f.calcChainWriter()
  85. f.commentsWriter()
  86. f.contentTypesWriter()
  87. f.drawingsWriter()
  88. f.vmlDrawingWriter()
  89. f.workBookWriter()
  90. f.workSheetWriter()
  91. f.relsWriter()
  92. f.sharedStringsWriter()
  93. f.styleSheetWriter()
  94. for path, content := range f.XLSX {
  95. fi, err := zw.Create(path)
  96. if err != nil {
  97. zw.Close()
  98. return buf, err
  99. }
  100. _, err = fi.Write(content)
  101. if err != nil {
  102. zw.Close()
  103. return buf, err
  104. }
  105. }
  106. return buf, zw.Close()
  107. }