file.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strconv"
  9. )
  10. // File is a high level structure providing a slice of Sheet structs
  11. // to the user.
  12. type File struct {
  13. worksheets map[string]*zip.File
  14. numFmtRefTable map[int]xlsxNumFmt
  15. referenceTable *RefTable
  16. Date1904 bool
  17. styles *xlsxStyles
  18. Sheets []*Sheet
  19. Sheet map[string]*Sheet
  20. }
  21. // Create a new File
  22. func NewFile() (file *File) {
  23. file = &File{}
  24. file.Sheet = make(map[string]*Sheet)
  25. file.Sheets = make([]*Sheet, 0)
  26. return
  27. }
  28. // OpenFile() take the name of an XLSX file and returns a populated
  29. // xlsx.File struct for it.
  30. func OpenFile(filename string) (*File, error) {
  31. var f *zip.ReadCloser
  32. f, err := zip.OpenReader(filename)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return ReadZip(f)
  37. }
  38. // A convenient wrapper around File.ToSlice, FileToSlice will
  39. // return the raw data contained in an Excel XLSX file as three
  40. // dimensional slice. The first index represents the sheet number,
  41. // the second the row number, and the third the cell number.
  42. //
  43. // For example:
  44. //
  45. // var mySlice [][][]string
  46. // var value string
  47. // mySlice = xlsx.FileToSlice("myXLSX.xlsx")
  48. // value = mySlice[0][0][0]
  49. //
  50. // Here, value would be set to the raw value of the cell A1 in the
  51. // first sheet in the XLSX file.
  52. func FileToSlice(path string) ([][][]string, error) {
  53. f, err := OpenFile(path)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return f.ToSlice()
  58. }
  59. // Save the File to an xlsx file at the provided path.
  60. func (f *File) Save(path string) (err error) {
  61. var parts map[string]string
  62. var target *os.File
  63. var zipWriter *zip.Writer
  64. parts, err = f.MarshallParts()
  65. if err != nil {
  66. return
  67. }
  68. target, err = os.Create(path)
  69. if err != nil {
  70. return
  71. }
  72. zipWriter = zip.NewWriter(target)
  73. for partName, part := range parts {
  74. var writer io.Writer
  75. writer, err = zipWriter.Create(partName)
  76. if err != nil {
  77. return
  78. }
  79. _, err = writer.Write([]byte(part))
  80. if err != nil {
  81. return
  82. }
  83. }
  84. err = zipWriter.Close()
  85. if err != nil {
  86. return
  87. }
  88. return target.Close()
  89. }
  90. // Add a new Sheet, with the provided name, to a File
  91. func (f *File) AddSheet(sheetName string) (sheet *Sheet) {
  92. sheet = &Sheet{Name: sheetName}
  93. f.Sheet[sheetName] = sheet
  94. f.Sheets = append(f.Sheets, sheet)
  95. return sheet
  96. }
  97. func (f *File) makeWorkbook() xlsxWorkbook {
  98. var workbook xlsxWorkbook
  99. workbook = xlsxWorkbook{}
  100. workbook.FileVersion = xlsxFileVersion{}
  101. workbook.FileVersion.AppName = "Go XLSX"
  102. workbook.WorkbookPr = xlsxWorkbookPr{BackupFile: false}
  103. workbook.BookViews = xlsxBookViews{}
  104. workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
  105. workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{}
  106. workbook.Sheets = xlsxSheets{}
  107. workbook.Sheets.Sheet = make([]xlsxSheet, len(f.Sheets))
  108. return workbook
  109. }
  110. // Construct a map of file name to XML content representing the file
  111. // in terms of the structure of an XLSX file.
  112. func (f *File) MarshallParts() (map[string]string, error) {
  113. var parts map[string]string
  114. var refTable *RefTable = NewSharedStringRefTable()
  115. refTable.isWrite = true
  116. var workbookRels WorkBookRels = make(WorkBookRels)
  117. var err error
  118. var workbook xlsxWorkbook
  119. var types xlsxTypes = MakeDefaultContentTypes()
  120. marshal := func(thing interface{}) (string, error) {
  121. body, err := xml.MarshalIndent(thing, " ", " ")
  122. if err != nil {
  123. return "", err
  124. }
  125. return xml.Header + string(body), nil
  126. }
  127. parts = make(map[string]string)
  128. workbook = f.makeWorkbook()
  129. sheetIndex := 1
  130. for _, sheet := range f.Sheets {
  131. xSheet := sheet.makeXLSXSheet(refTable)
  132. rId := fmt.Sprintf("rId%d", sheetIndex)
  133. sheetId := strconv.Itoa(sheetIndex)
  134. sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)
  135. partName := "xl/" + sheetPath
  136. types.Overrides = append(
  137. types.Overrides,
  138. xlsxOverride{
  139. PartName: partName,
  140. ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"})
  141. workbookRels[rId] = sheetPath
  142. workbook.Sheets.Sheet[sheetIndex-1] = xlsxSheet{
  143. Name: sheet.Name,
  144. SheetId: sheetId,
  145. Id: rId}
  146. parts[partName], err = marshal(xSheet)
  147. if err != nil {
  148. return parts, err
  149. }
  150. sheetIndex++
  151. }
  152. parts["xl/workbook.xml"], err = marshal(workbook)
  153. if err != nil {
  154. return parts, err
  155. }
  156. parts["_rels/.rels"] = `<?xml version="1.0" encoding="UTF-8"?>
  157. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  158. <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
  159. <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  160. <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
  161. </Relationships>`
  162. parts["docProps/app.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  163. <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  164. <TotalTime>0</TotalTime>
  165. <Application>Go XLSX</Application>
  166. </Properties>`
  167. // TODO - do this properly, modification and revision information
  168. parts["docProps/core.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  169. <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></cp:coreProperties>`
  170. xSST := refTable.makeXLSXSST()
  171. parts["xl/sharedStrings.xml"], err = marshal(xSST)
  172. if err != nil {
  173. return parts, err
  174. }
  175. xWRel := workbookRels.MakeXLSXWorkbookRels()
  176. parts["xl/_rels/workbook.xml.rels"], err = marshal(xWRel)
  177. if err != nil {
  178. return parts, err
  179. }
  180. parts["[Content_Types].xml"], err = marshal(types)
  181. if err != nil {
  182. return parts, err
  183. }
  184. parts["xl/styles.xml"] = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  185. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  186. </styleSheet>`
  187. return parts, nil
  188. }
  189. // Return the raw data contained in the File as three
  190. // dimensional slice. The first index represents the sheet number,
  191. // the second the row number, and the third the cell number.
  192. //
  193. // For example:
  194. //
  195. // var mySlice [][][]string
  196. // var value string
  197. // mySlice = xlsx.FileToSlice("myXLSX.xlsx")
  198. // value = mySlice[0][0][0]
  199. //
  200. // Here, value would be set to the raw value of the cell A1 in the
  201. // first sheet in the XLSX file.
  202. func (file *File) ToSlice() (output [][][]string, err error) {
  203. output = [][][]string{}
  204. for _, sheet := range file.Sheets {
  205. s := [][]string{}
  206. for _, row := range sheet.Rows {
  207. if row == nil {
  208. continue
  209. }
  210. r := []string{}
  211. for _, cell := range row.Cells {
  212. r = append(r, cell.String())
  213. }
  214. s = append(s, r)
  215. }
  216. output = append(output, s)
  217. }
  218. return output, nil
  219. }