excelize.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. // Package excelize providing a set of functions that allow you to write to
  5. // and read from XLSX / XLSM / XLTM files. Supports reading and writing
  6. // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  7. // complex components by high compatibility, and provided streaming API for
  8. // generating or reading data from a worksheet with huge amounts of data. This
  9. // library needs Go version 1.10 or later.
  10. //
  11. // See https://xuri.me/excelize for more information about this package.
  12. package excelize
  13. import (
  14. "archive/zip"
  15. "bytes"
  16. "encoding/xml"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "path"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "golang.org/x/net/html/charset"
  27. )
  28. // File define a populated spreadsheet file struct.
  29. type File struct {
  30. sync.Mutex
  31. options *Options
  32. xmlAttr map[string][]xml.Attr
  33. checked map[string]bool
  34. sheetMap map[string]string
  35. streams map[string]*StreamWriter
  36. CalcChain *xlsxCalcChain
  37. Comments map[string]*xlsxComments
  38. ContentTypes *xlsxTypes
  39. Drawings map[string]*xlsxWsDr
  40. Path string
  41. SharedStrings *xlsxSST
  42. sharedStringsMap map[string]int
  43. Sheet map[string]*xlsxWorksheet
  44. SheetCount int
  45. Styles *xlsxStyleSheet
  46. Theme *xlsxTheme
  47. DecodeVMLDrawing map[string]*decodeVmlDrawing
  48. VMLDrawing map[string]*vmlDrawing
  49. WorkBook *xlsxWorkbook
  50. Relationships map[string]*xlsxRelationships
  51. XLSX map[string][]byte
  52. CharsetReader charsetTranscoderFn
  53. }
  54. type charsetTranscoderFn func(charset string, input io.Reader) (rdr io.Reader, err error)
  55. // Options define the options for open spreadsheet.
  56. type Options struct {
  57. Password string
  58. }
  59. // OpenFile take the name of an spreadsheet file and returns a populated spreadsheet file struct
  60. // for it. For example, open spreadsheet with password protection:
  61. //
  62. // f, err := excelize.OpenFile("Book1.xlsx", excelize.Options{Password: "password"})
  63. // if err != nil {
  64. // return
  65. // }
  66. //
  67. // Note that the excelize just support decrypt and not support encrypt currently, the spreadsheet
  68. // saved by Save and SaveAs will be without password unprotected.
  69. func OpenFile(filename string, opt ...Options) (*File, error) {
  70. file, err := os.Open(filename)
  71. if err != nil {
  72. return nil, err
  73. }
  74. defer file.Close()
  75. f, err := OpenReader(file, opt...)
  76. if err != nil {
  77. return nil, err
  78. }
  79. f.Path = filename
  80. return f, nil
  81. }
  82. // newFile is object builder
  83. func newFile() *File {
  84. return &File{
  85. xmlAttr: make(map[string][]xml.Attr),
  86. checked: make(map[string]bool),
  87. sheetMap: make(map[string]string),
  88. Comments: make(map[string]*xlsxComments),
  89. Drawings: make(map[string]*xlsxWsDr),
  90. sharedStringsMap: make(map[string]int),
  91. Sheet: make(map[string]*xlsxWorksheet),
  92. DecodeVMLDrawing: make(map[string]*decodeVmlDrawing),
  93. VMLDrawing: make(map[string]*vmlDrawing),
  94. Relationships: make(map[string]*xlsxRelationships),
  95. CharsetReader: charset.NewReaderLabel,
  96. }
  97. }
  98. // OpenReader read data stream from io.Reader and return a populated
  99. // spreadsheet file.
  100. func OpenReader(r io.Reader, opt ...Options) (*File, error) {
  101. b, err := ioutil.ReadAll(r)
  102. if err != nil {
  103. return nil, err
  104. }
  105. f := newFile()
  106. if bytes.Contains(b, oleIdentifier) && len(opt) > 0 {
  107. for _, o := range opt {
  108. f.options = &o
  109. }
  110. b, err = Decrypt(b, f.options)
  111. if err != nil {
  112. return nil, fmt.Errorf("decrypted file failed")
  113. }
  114. }
  115. zr, err := zip.NewReader(bytes.NewReader(b), int64(len(b)))
  116. if err != nil {
  117. return nil, err
  118. }
  119. file, sheetCount, err := ReadZipReader(zr)
  120. if err != nil {
  121. return nil, err
  122. }
  123. f.SheetCount, f.XLSX = sheetCount, file
  124. f.CalcChain = f.calcChainReader()
  125. f.sheetMap = f.getSheetMap()
  126. f.Styles = f.stylesReader()
  127. f.Theme = f.themeReader()
  128. return f, nil
  129. }
  130. // CharsetTranscoder Set user defined codepage transcoder function for open
  131. // XLSX from non UTF-8 encoding.
  132. func (f *File) CharsetTranscoder(fn charsetTranscoderFn) *File { f.CharsetReader = fn; return f }
  133. // Creates new XML decoder with charset reader.
  134. func (f *File) xmlNewDecoder(rdr io.Reader) (ret *xml.Decoder) {
  135. ret = xml.NewDecoder(rdr)
  136. ret.CharsetReader = f.CharsetReader
  137. return
  138. }
  139. // setDefaultTimeStyle provides a function to set default numbers format for
  140. // time.Time type cell value by given worksheet name, cell coordinates and
  141. // number format code.
  142. func (f *File) setDefaultTimeStyle(sheet, axis string, format int) error {
  143. s, err := f.GetCellStyle(sheet, axis)
  144. if err != nil {
  145. return err
  146. }
  147. if s == 0 {
  148. style, _ := f.NewStyle(&Style{NumFmt: format})
  149. err = f.SetCellStyle(sheet, axis, axis, style)
  150. }
  151. return err
  152. }
  153. // workSheetReader provides a function to get the pointer to the structure
  154. // after deserialization by given worksheet name.
  155. func (f *File) workSheetReader(sheet string) (ws *xlsxWorksheet, err error) {
  156. f.Lock()
  157. defer f.Unlock()
  158. var (
  159. name string
  160. ok bool
  161. )
  162. if name, ok = f.sheetMap[trimSheetName(sheet)]; !ok {
  163. err = fmt.Errorf("sheet %s is not exist", sheet)
  164. return
  165. }
  166. if ws = f.Sheet[name]; f.Sheet[name] == nil {
  167. if strings.HasPrefix(name, "xl/chartsheets") {
  168. err = fmt.Errorf("sheet %s is chart sheet", sheet)
  169. return
  170. }
  171. ws = new(xlsxWorksheet)
  172. if _, ok := f.xmlAttr[name]; !ok {
  173. d := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(name))))
  174. f.xmlAttr[name] = append(f.xmlAttr[name], getRootElement(d)...)
  175. }
  176. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(name)))).
  177. Decode(ws); err != nil && err != io.EOF {
  178. err = fmt.Errorf("xml decode error: %s", err)
  179. return
  180. }
  181. err = nil
  182. if f.checked == nil {
  183. f.checked = make(map[string]bool)
  184. }
  185. if ok = f.checked[name]; !ok {
  186. checkSheet(ws)
  187. if err = checkRow(ws); err != nil {
  188. return
  189. }
  190. f.checked[name] = true
  191. }
  192. f.Sheet[name] = ws
  193. }
  194. return
  195. }
  196. // checkSheet provides a function to fill each row element and make that is
  197. // continuous in a worksheet of XML.
  198. func checkSheet(ws *xlsxWorksheet) {
  199. var row int
  200. for _, r := range ws.SheetData.Row {
  201. if r.R != 0 && r.R > row {
  202. row = r.R
  203. continue
  204. }
  205. row++
  206. }
  207. sheetData := xlsxSheetData{Row: make([]xlsxRow, row)}
  208. row = 0
  209. for _, r := range ws.SheetData.Row {
  210. if r.R != 0 {
  211. sheetData.Row[r.R-1] = r
  212. row = r.R
  213. continue
  214. }
  215. row++
  216. r.R = row
  217. sheetData.Row[row-1] = r
  218. }
  219. for i := 1; i <= row; i++ {
  220. sheetData.Row[i-1].R = i
  221. }
  222. ws.SheetData = sheetData
  223. }
  224. // addRels provides a function to add relationships by given XML path,
  225. // relationship type, target and target mode.
  226. func (f *File) addRels(relPath, relType, target, targetMode string) int {
  227. var uniqPart = map[string]string{
  228. SourceRelationshipSharedStrings: "/xl/sharedStrings.xml",
  229. }
  230. rels := f.relsReader(relPath)
  231. if rels == nil {
  232. rels = &xlsxRelationships{}
  233. }
  234. var rID int
  235. for idx, rel := range rels.Relationships {
  236. ID, _ := strconv.Atoi(strings.TrimPrefix(rel.ID, "rId"))
  237. if ID > rID {
  238. rID = ID
  239. }
  240. if relType == rel.Type {
  241. if partName, ok := uniqPart[rel.Type]; ok {
  242. rels.Relationships[idx].Target = partName
  243. return rID
  244. }
  245. }
  246. }
  247. rID++
  248. var ID bytes.Buffer
  249. ID.WriteString("rId")
  250. ID.WriteString(strconv.Itoa(rID))
  251. rels.Relationships = append(rels.Relationships, xlsxRelationship{
  252. ID: ID.String(),
  253. Type: relType,
  254. Target: target,
  255. TargetMode: targetMode,
  256. })
  257. f.Relationships[relPath] = rels
  258. return rID
  259. }
  260. // UpdateLinkedValue fix linked values within a spreadsheet are not updating in
  261. // Office Excel 2007 and 2010. This function will be remove value tag when met a
  262. // cell have a linked value. Reference
  263. // https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating
  264. //
  265. // Notice: after open XLSX file Excel will be update linked value and generate
  266. // new value and will prompt save file or not.
  267. //
  268. // For example:
  269. //
  270. // <row r="19" spans="2:2">
  271. // <c r="B19">
  272. // <f>SUM(Sheet2!D2,Sheet2!D11)</f>
  273. // <v>100</v>
  274. // </c>
  275. // </row>
  276. //
  277. // to
  278. //
  279. // <row r="19" spans="2:2">
  280. // <c r="B19">
  281. // <f>SUM(Sheet2!D2,Sheet2!D11)</f>
  282. // </c>
  283. // </row>
  284. //
  285. func (f *File) UpdateLinkedValue() error {
  286. wb := f.workbookReader()
  287. // recalculate formulas
  288. wb.CalcPr = nil
  289. for _, name := range f.GetSheetList() {
  290. xlsx, err := f.workSheetReader(name)
  291. if err != nil {
  292. return err
  293. }
  294. for indexR := range xlsx.SheetData.Row {
  295. for indexC, col := range xlsx.SheetData.Row[indexR].C {
  296. if col.F != nil && col.V != "" {
  297. xlsx.SheetData.Row[indexR].C[indexC].V = ""
  298. xlsx.SheetData.Row[indexR].C[indexC].T = ""
  299. }
  300. }
  301. }
  302. }
  303. return nil
  304. }
  305. // AddVBAProject provides the method to add vbaProject.bin file which contains
  306. // functions and/or macros. The file extension should be .xlsm. For example:
  307. //
  308. // if err := f.SetSheetPrOptions("Sheet1", excelize.CodeName("Sheet1")); err != nil {
  309. // fmt.Println(err)
  310. // }
  311. // if err := f.AddVBAProject("vbaProject.bin"); err != nil {
  312. // fmt.Println(err)
  313. // }
  314. // if err := f.SaveAs("macros.xlsm"); err != nil {
  315. // fmt.Println(err)
  316. // }
  317. //
  318. func (f *File) AddVBAProject(bin string) error {
  319. var err error
  320. // Check vbaProject.bin exists first.
  321. if _, err = os.Stat(bin); os.IsNotExist(err) {
  322. return fmt.Errorf("stat %s: no such file or directory", bin)
  323. }
  324. if path.Ext(bin) != ".bin" {
  325. return errors.New("unsupported VBA project extension")
  326. }
  327. f.setContentTypePartVBAProjectExtensions()
  328. wb := f.relsReader(f.getWorkbookRelsPath())
  329. var rID int
  330. var ok bool
  331. for _, rel := range wb.Relationships {
  332. if rel.Target == "vbaProject.bin" && rel.Type == SourceRelationshipVBAProject {
  333. ok = true
  334. continue
  335. }
  336. t, _ := strconv.Atoi(strings.TrimPrefix(rel.ID, "rId"))
  337. if t > rID {
  338. rID = t
  339. }
  340. }
  341. rID++
  342. if !ok {
  343. wb.Relationships = append(wb.Relationships, xlsxRelationship{
  344. ID: "rId" + strconv.Itoa(rID),
  345. Target: "vbaProject.bin",
  346. Type: SourceRelationshipVBAProject,
  347. })
  348. }
  349. file, _ := ioutil.ReadFile(bin)
  350. f.XLSX["xl/vbaProject.bin"] = file
  351. return err
  352. }
  353. // setContentTypePartVBAProjectExtensions provides a function to set the
  354. // content type for relationship parts and the main document part.
  355. func (f *File) setContentTypePartVBAProjectExtensions() {
  356. var ok bool
  357. content := f.contentTypesReader()
  358. for _, v := range content.Defaults {
  359. if v.Extension == "bin" {
  360. ok = true
  361. }
  362. }
  363. for idx, o := range content.Overrides {
  364. if o.PartName == "/xl/workbook.xml" {
  365. content.Overrides[idx].ContentType = ContentTypeMacro
  366. }
  367. }
  368. if !ok {
  369. content.Defaults = append(content.Defaults, xlsxDefault{
  370. Extension: "bin",
  371. ContentType: ContentTypeVBA,
  372. })
  373. }
  374. }