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.

120 lines
3.5KB

  1. // Copyright 2016 - 2019 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 := &File{
  34. sheetMap: make(map[string]string),
  35. Sheet: make(map[string]*xlsxWorksheet),
  36. SheetCount: 1,
  37. XLSX: file,
  38. }
  39. f.CalcChain = f.calcChainReader()
  40. f.Comments = make(map[string]*xlsxComments)
  41. f.ContentTypes = f.contentTypesReader()
  42. f.Drawings = make(map[string]*xlsxWsDr)
  43. f.Styles = f.stylesReader()
  44. f.DecodeVMLDrawing = make(map[string]*decodeVmlDrawing)
  45. f.VMLDrawing = make(map[string]*vmlDrawing)
  46. f.WorkBook = f.workbookReader()
  47. f.Relationships = make(map[string]*xlsxRelationships)
  48. f.Relationships["xl/_rels/workbook.xml.rels"] = f.relsReader("xl/_rels/workbook.xml.rels")
  49. f.Sheet["xl/worksheets/sheet1.xml"], _ = f.workSheetReader("Sheet1")
  50. f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
  51. f.Theme = f.themeReader()
  52. return f
  53. }
  54. // Save provides a function to override the xlsx file with origin path.
  55. func (f *File) Save() error {
  56. if f.Path == "" {
  57. return fmt.Errorf("no path defined for file, consider File.WriteTo or File.Write")
  58. }
  59. return f.SaveAs(f.Path)
  60. }
  61. // SaveAs provides a function to create or update to an xlsx file at the
  62. // provided path.
  63. func (f *File) SaveAs(name string) error {
  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.styleSheetWriter()
  97. for path, content := range f.XLSX {
  98. fi, err := zw.Create(path)
  99. if err != nil {
  100. zw.Close()
  101. return buf, err
  102. }
  103. _, err = fi.Write(content)
  104. if err != nil {
  105. zw.Close()
  106. return buf, err
  107. }
  108. }
  109. return buf, zw.Close()
  110. }