file.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package excelize
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "os"
  8. )
  9. // NewFile provides a function to create new file by default template. For
  10. // example:
  11. //
  12. // xlsx := NewFile()
  13. //
  14. func NewFile() *File {
  15. file := make(map[string][]byte)
  16. file["_rels/.rels"] = []byte(XMLHeader + templateRels)
  17. file["docProps/app.xml"] = []byte(XMLHeader + templateDocpropsApp)
  18. file["docProps/core.xml"] = []byte(XMLHeader + templateDocpropsCore)
  19. file["xl/_rels/workbook.xml.rels"] = []byte(XMLHeader + templateWorkbookRels)
  20. file["xl/theme/theme1.xml"] = []byte(XMLHeader + templateTheme)
  21. file["xl/worksheets/sheet1.xml"] = []byte(XMLHeader + templateSheet)
  22. file["xl/styles.xml"] = []byte(XMLHeader + templateStyles)
  23. file["xl/workbook.xml"] = []byte(XMLHeader + templateWorkbook)
  24. file["[Content_Types].xml"] = []byte(XMLHeader + templateContentTypes)
  25. f := &File{
  26. sheetMap: make(map[string]string),
  27. Sheet: make(map[string]*xlsxWorksheet),
  28. SheetCount: 1,
  29. XLSX: file,
  30. }
  31. f.ContentTypes = f.contentTypesReader()
  32. f.Styles = f.stylesReader()
  33. f.WorkBook = f.workbookReader()
  34. f.WorkBookRels = f.workbookRelsReader()
  35. f.Sheet["xl/worksheets/sheet1.xml"] = f.workSheetReader("Sheet1")
  36. f.sheetMap["Sheet1"] = "xl/worksheets/sheet1.xml"
  37. f.Theme = f.themeReader()
  38. return f
  39. }
  40. // Save provides a function to override the xlsx file with origin path.
  41. func (f *File) Save() error {
  42. if f.Path == "" {
  43. return fmt.Errorf("No path defined for file, consider File.WriteTo or File.Write")
  44. }
  45. return f.SaveAs(f.Path)
  46. }
  47. // SaveAs provides a function to create or update to an xlsx file at the
  48. // provided path.
  49. func (f *File) SaveAs(name string) error {
  50. file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
  51. if err != nil {
  52. return err
  53. }
  54. defer file.Close()
  55. return f.Write(file)
  56. }
  57. // Write provides a function to write to an io.Writer.
  58. func (f *File) Write(w io.Writer) error {
  59. _, err := f.WriteTo(w)
  60. return err
  61. }
  62. // WriteTo implements io.WriterTo to write the file.
  63. func (f *File) WriteTo(w io.Writer) (int64, error) {
  64. buf := new(bytes.Buffer)
  65. zw := zip.NewWriter(buf)
  66. f.contentTypesWriter()
  67. f.workbookWriter()
  68. f.workbookRelsWriter()
  69. f.worksheetWriter()
  70. f.styleSheetWriter()
  71. for path, content := range f.XLSX {
  72. fi, err := zw.Create(path)
  73. if err != nil {
  74. return 0, err
  75. }
  76. _, err = fi.Write(content)
  77. if err != nil {
  78. return 0, err
  79. }
  80. }
  81. err := zw.Close()
  82. if err != nil {
  83. return 0, err
  84. }
  85. return buf.WriteTo(w)
  86. }