file.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/xml"
  6. "fmt"
  7. "io"
  8. "os"
  9. "strconv"
  10. "strings"
  11. )
  12. // File is a high level structure providing a slice of Sheet structs
  13. // to the user.
  14. type File struct {
  15. worksheets map[string]*zip.File
  16. referenceTable *RefTable
  17. Date1904 bool
  18. styles *xlsxStyleSheet
  19. Sheets []*Sheet
  20. Sheet map[string]*Sheet
  21. theme *theme
  22. DefinedNames []*xlsxDefinedName
  23. }
  24. // Create a new File
  25. func NewFile() (file *File) {
  26. file = &File{}
  27. file.Sheet = make(map[string]*Sheet)
  28. file.Sheets = make([]*Sheet, 0)
  29. file.DefinedNames = make([]*xlsxDefinedName, 0)
  30. return
  31. }
  32. // OpenFile() take the name of an XLSX file and returns a populated
  33. // xlsx.File struct for it.
  34. func OpenFile(filename string) (file *File, err error) {
  35. var f *zip.ReadCloser
  36. f, err = zip.OpenReader(filename)
  37. if err != nil {
  38. return nil, err
  39. }
  40. file, err = ReadZip(f)
  41. return
  42. }
  43. // OpenBinary() take bytes of an XLSX file and returns a populated
  44. // xlsx.File struct for it.
  45. func OpenBinary(bs []byte) (file *File, err error) {
  46. r := bytes.NewReader(bs)
  47. file, err = OpenReaderAt(r, int64(r.Len()))
  48. return
  49. }
  50. // OpenReaderAt() take io.ReaderAt of an XLSX file and returns a populated
  51. // xlsx.File struct for it.
  52. func OpenReaderAt(r io.ReaderAt, size int64) (file *File, err error) {
  53. var f *zip.Reader
  54. f, err = zip.NewReader(r, size)
  55. if err != nil {
  56. return nil, err
  57. }
  58. file, err = ReadZipReader(f)
  59. return
  60. }
  61. // A convenient wrapper around File.ToSlice, FileToSlice will
  62. // return the raw data contained in an Excel XLSX file as three
  63. // dimensional slice. The first index represents the sheet number,
  64. // the second the row number, and the third the cell number.
  65. //
  66. // For example:
  67. //
  68. // var mySlice [][][]string
  69. // var value string
  70. // mySlice = xlsx.FileToSlice("myXLSX.xlsx")
  71. // value = mySlice[0][0][0]
  72. //
  73. // Here, value would be set to the raw value of the cell A1 in the
  74. // first sheet in the XLSX file.
  75. func FileToSlice(path string) ([][][]string, error) {
  76. f, err := OpenFile(path)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return f.ToSlice()
  81. }
  82. // Save the File to an xlsx file at the provided path.
  83. func (f *File) Save(path string) (err error) {
  84. var target *os.File
  85. target, err = os.Create(path)
  86. if err != nil {
  87. return
  88. }
  89. err = f.Write(target)
  90. if err != nil {
  91. return
  92. }
  93. return target.Close()
  94. }
  95. // Write the File to io.Writer as xlsx
  96. func (f *File) Write(writer io.Writer) (err error) {
  97. var parts map[string]string
  98. var zipWriter *zip.Writer
  99. parts, err = f.MarshallParts()
  100. if err != nil {
  101. return
  102. }
  103. zipWriter = zip.NewWriter(writer)
  104. for partName, part := range parts {
  105. var writer io.Writer
  106. writer, err = zipWriter.Create(partName)
  107. if err != nil {
  108. return
  109. }
  110. _, err = writer.Write([]byte(part))
  111. if err != nil {
  112. return
  113. }
  114. }
  115. err = zipWriter.Close()
  116. return
  117. }
  118. // Add a new Sheet, with the provided name, to a File
  119. func (f *File) AddSheet(sheetName string) (sheet *Sheet, err error) {
  120. if _, exists := f.Sheet[sheetName]; exists {
  121. return nil, fmt.Errorf("Duplicate sheet name '%s'.", sheetName)
  122. }
  123. sheet = &Sheet{Name: sheetName, File: f}
  124. if len(f.Sheets) == 0 {
  125. sheet.Selected = true
  126. }
  127. f.Sheet[sheetName] = sheet
  128. f.Sheets = append(f.Sheets, sheet)
  129. return sheet, nil
  130. }
  131. func (f *File) makeWorkbook() xlsxWorkbook {
  132. var workbook xlsxWorkbook
  133. workbook = xlsxWorkbook{}
  134. workbook.FileVersion = xlsxFileVersion{}
  135. workbook.FileVersion.AppName = "Go XLSX"
  136. workbook.WorkbookPr = xlsxWorkbookPr{
  137. BackupFile: false,
  138. ShowObjects: "all"}
  139. workbook.BookViews = xlsxBookViews{}
  140. workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
  141. workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{
  142. ActiveTab: 0,
  143. FirstSheet: 0,
  144. ShowHorizontalScroll: true,
  145. ShowSheetTabs: true,
  146. ShowVerticalScroll: true,
  147. TabRatio: 204,
  148. WindowHeight: 8192,
  149. WindowWidth: 16384,
  150. XWindow: "0",
  151. YWindow: "0"}
  152. workbook.Sheets = xlsxSheets{}
  153. workbook.Sheets.Sheet = make([]xlsxSheet, len(f.Sheets))
  154. workbook.CalcPr.IterateCount = 100
  155. workbook.CalcPr.RefMode = "A1"
  156. workbook.CalcPr.Iterate = false
  157. workbook.CalcPr.IterateDelta = 0.001
  158. workbook.DefinedNames = xlsxDefinedNames{}
  159. return workbook
  160. }
  161. // Some tools that read XLSX files have very strict requirements about
  162. // the structure of the input XML. In particular both Numbers on the Mac
  163. // and SAS dislike inline XML namespace declarations, or namespace
  164. // prefixes that don't match the ones that Excel itself uses. This is a
  165. // problem because the Go XML library doesn't multiple namespace
  166. // declarations in a single element of a document. This function is a
  167. // horrible hack to fix that after the XML marshalling is completed.
  168. func replaceRelationshipsNameSpace(workbookMarshal string) string {
  169. newWorkbook := strings.Replace(workbookMarshal, `xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id`, `r:id`, -1)
  170. // Dirty hack to fix issues #63 and #91; encoding/xml currently
  171. // "doesn't allow for additional namespaces to be defined in the
  172. // root element of the document," as described by @tealeg in the
  173. // comments for #63.
  174. oldXmlns := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
  175. newXmlns := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">`
  176. return strings.Replace(newWorkbook, oldXmlns, newXmlns, 1)
  177. }
  178. // Construct a map of file name to XML content representing the file
  179. // in terms of the structure of an XLSX file.
  180. func (f *File) MarshallParts() (map[string]string, error) {
  181. var parts map[string]string
  182. var refTable *RefTable = NewSharedStringRefTable()
  183. refTable.isWrite = true
  184. var workbookRels WorkBookRels = make(WorkBookRels)
  185. var err error
  186. var workbook xlsxWorkbook
  187. var types xlsxTypes = MakeDefaultContentTypes()
  188. marshal := func(thing interface{}) (string, error) {
  189. body, err := xml.Marshal(thing)
  190. if err != nil {
  191. return "", err
  192. }
  193. return xml.Header + string(body), nil
  194. }
  195. parts = make(map[string]string)
  196. workbook = f.makeWorkbook()
  197. sheetIndex := 1
  198. if f.styles == nil {
  199. f.styles = newXlsxStyleSheet(f.theme)
  200. }
  201. f.styles.reset()
  202. for _, sheet := range f.Sheets {
  203. xSheet := sheet.makeXLSXSheet(refTable, f.styles)
  204. rId := fmt.Sprintf("rId%d", sheetIndex)
  205. sheetId := strconv.Itoa(sheetIndex)
  206. sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)
  207. partName := "xl/" + sheetPath
  208. types.Overrides = append(
  209. types.Overrides,
  210. xlsxOverride{
  211. PartName: "/" + partName,
  212. ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"})
  213. workbookRels[rId] = sheetPath
  214. workbook.Sheets.Sheet[sheetIndex-1] = xlsxSheet{
  215. Name: sheet.Name,
  216. SheetId: sheetId,
  217. Id: rId,
  218. State: "visible"}
  219. parts[partName], err = marshal(xSheet)
  220. if err != nil {
  221. return parts, err
  222. }
  223. sheetIndex++
  224. }
  225. workbookMarshal, err := marshal(workbook)
  226. if err != nil {
  227. return parts, err
  228. }
  229. workbookMarshal = replaceRelationshipsNameSpace(workbookMarshal)
  230. parts["xl/workbook.xml"] = workbookMarshal
  231. if err != nil {
  232. return parts, err
  233. }
  234. parts["_rels/.rels"] = TEMPLATE__RELS_DOT_RELS
  235. parts["docProps/app.xml"] = TEMPLATE_DOCPROPS_APP
  236. // TODO - do this properly, modification and revision information
  237. parts["docProps/core.xml"] = TEMPLATE_DOCPROPS_CORE
  238. parts["xl/theme/theme1.xml"] = TEMPLATE_XL_THEME_THEME
  239. xSST := refTable.makeXLSXSST()
  240. parts["xl/sharedStrings.xml"], err = marshal(xSST)
  241. if err != nil {
  242. return parts, err
  243. }
  244. xWRel := workbookRels.MakeXLSXWorkbookRels()
  245. parts["xl/_rels/workbook.xml.rels"], err = marshal(xWRel)
  246. if err != nil {
  247. return parts, err
  248. }
  249. parts["[Content_Types].xml"], err = marshal(types)
  250. if err != nil {
  251. return parts, err
  252. }
  253. parts["xl/styles.xml"], err = f.styles.Marshal()
  254. if err != nil {
  255. return parts, err
  256. }
  257. return parts, nil
  258. }
  259. // Return the raw data contained in the File as three
  260. // dimensional slice. The first index represents the sheet number,
  261. // the second the row number, and the third the cell number.
  262. //
  263. // For example:
  264. //
  265. // var mySlice [][][]string
  266. // var value string
  267. // mySlice = xlsx.FileToSlice("myXLSX.xlsx")
  268. // value = mySlice[0][0][0]
  269. //
  270. // Here, value would be set to the raw value of the cell A1 in the
  271. // first sheet in the XLSX file.
  272. func (file *File) ToSlice() (output [][][]string, err error) {
  273. output = [][][]string{}
  274. for _, sheet := range file.Sheets {
  275. s := [][]string{}
  276. for _, row := range sheet.Rows {
  277. if row == nil {
  278. continue
  279. }
  280. r := []string{}
  281. for _, cell := range row.Cells {
  282. str, err := cell.String()
  283. if err != nil {
  284. return output, err
  285. }
  286. r = append(r, str)
  287. }
  288. s = append(s, r)
  289. }
  290. output = append(output, s)
  291. }
  292. return output, nil
  293. }