You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

121 lines
3.5 KiB

  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. "errors"
  14. "fmt"
  15. "io"
  16. "os"
  17. )
  18. // NewFile provides a function to create new file by default template. For
  19. // example:
  20. //
  21. // xlsx := NewFile()
  22. //
  23. func NewFile() *File {
  24. file := make(map[string][]byte)
  25. file["_rels/.rels"] = []byte(XMLHeader + templateRels)
  26. file["docProps/app.xml"] = []byte(XMLHeader + templateDocpropsApp)
  27. file["docProps/core.xml"] = []byte(XMLHeader + templateDocpropsCore)
  28. file["xl/_rels/workbook.xml.rels"] = []byte(XMLHeader + templateWorkbookRels)
  29. file["xl/theme/theme1.xml"] = []byte(XMLHeader + templateTheme)
  30. file["xl/worksheets/sheet1.xml"] = []byte(XMLHeader + templateSheet)
  31. file["xl/styles.xml"] = []byte(XMLHeader + templateStyles)
  32. file["xl/workbook.xml"] = []byte(XMLHeader + templateWorkbook)
  33. file["[Content_Types].xml"] = []byte(XMLHeader + templateContentTypes)
  34. f := newFile()
  35. f.SheetCount, f.XLSX = 1, file
  36. f.CalcChain = f.calcChainReader()
  37. f.Comments = make(map[string]*xlsxComments)
  38. f.ContentTypes = f.contentTypesReader()
  39. f.Drawings = make(map[string]*xlsxWsDr)
  40. f.Styles = f.stylesReader()
  41. f.DecodeVMLDrawing = make(map[string]*decodeVmlDrawing)
  42. f.VMLDrawing = make(map[string]*vmlDrawing)
  43. f.WorkBook = f.workbookReader()
  44. f.Relationships = make(map[string]*xlsxRelationships)
  45. f.Relationships["xl/_rels/workbook.xml.rels"] = f.relsReader("xl/_rels/workbook.xml.rels")
  46. f.Sheet["xl/worksheets/sheet1.xml"], _ = f.workSheetReader("Sheet1")
  47. f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
  48. f.Theme = f.themeReader()
  49. return f
  50. }
  51. // Save provides a function to override the xlsx file with origin path.
  52. func (f *File) Save() error {
  53. if f.Path == "" {
  54. return fmt.Errorf("no path defined for file, consider File.WriteTo or File.Write")
  55. }
  56. return f.SaveAs(f.Path)
  57. }
  58. // SaveAs provides a function to create or update to an xlsx file at the
  59. // provided path.
  60. func (f *File) SaveAs(name string) error {
  61. if len(name) > FileNameLength {
  62. return errors.New("file name length exceeds maximum limit")
  63. }
  64. file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
  65. if err != nil {
  66. return err
  67. }
  68. defer file.Close()
  69. return f.Write(file)
  70. }
  71. // Write provides a function to write to an io.Writer.
  72. func (f *File) Write(w io.Writer) error {
  73. _, err := f.WriteTo(w)
  74. return err
  75. }
  76. // WriteTo implements io.WriterTo to write the file.
  77. func (f *File) WriteTo(w io.Writer) (int64, error) {
  78. buf, err := f.WriteToBuffer()
  79. if err != nil {
  80. return 0, err
  81. }
  82. return buf.WriteTo(w)
  83. }
  84. // WriteToBuffer provides a function to get bytes.Buffer from the saved file.
  85. func (f *File) WriteToBuffer() (*bytes.Buffer, error) {
  86. buf := new(bytes.Buffer)
  87. zw := zip.NewWriter(buf)
  88. f.calcChainWriter()
  89. f.commentsWriter()
  90. f.contentTypesWriter()
  91. f.drawingsWriter()
  92. f.vmlDrawingWriter()
  93. f.workBookWriter()
  94. f.workSheetWriter()
  95. f.relsWriter()
  96. f.sharedStringsWriter()
  97. f.styleSheetWriter()
  98. for path, content := range f.XLSX {
  99. fi, err := zw.Create(path)
  100. if err != nil {
  101. zw.Close()
  102. return buf, err
  103. }
  104. _, err = fi.Write(content)
  105. if err != nil {
  106. zw.Close()
  107. return buf, err
  108. }
  109. }
  110. return buf, zw.Close()
  111. }