excelize.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // Copyright 2016 - 2021 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 Excel™ 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.15 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. if r.R != row {
  206. row++
  207. }
  208. }
  209. sheetData := xlsxSheetData{Row: make([]xlsxRow, row)}
  210. row = 0
  211. for _, r := range ws.SheetData.Row {
  212. if r.R == row {
  213. sheetData.Row[r.R-1].C = append(sheetData.Row[r.R-1].C, r.C...)
  214. continue
  215. }
  216. if r.R != 0 {
  217. sheetData.Row[r.R-1] = r
  218. row = r.R
  219. continue
  220. }
  221. row++
  222. r.R = row
  223. sheetData.Row[row-1] = r
  224. }
  225. for i := 1; i <= row; i++ {
  226. sheetData.Row[i-1].R = i
  227. }
  228. ws.SheetData = sheetData
  229. }
  230. // addRels provides a function to add relationships by given XML path,
  231. // relationship type, target and target mode.
  232. func (f *File) addRels(relPath, relType, target, targetMode string) int {
  233. var uniqPart = map[string]string{
  234. SourceRelationshipSharedStrings: "/xl/sharedStrings.xml",
  235. }
  236. rels := f.relsReader(relPath)
  237. if rels == nil {
  238. rels = &xlsxRelationships{}
  239. }
  240. var rID int
  241. for idx, rel := range rels.Relationships {
  242. ID, _ := strconv.Atoi(strings.TrimPrefix(rel.ID, "rId"))
  243. if ID > rID {
  244. rID = ID
  245. }
  246. if relType == rel.Type {
  247. if partName, ok := uniqPart[rel.Type]; ok {
  248. rels.Relationships[idx].Target = partName
  249. return rID
  250. }
  251. }
  252. }
  253. rID++
  254. var ID bytes.Buffer
  255. ID.WriteString("rId")
  256. ID.WriteString(strconv.Itoa(rID))
  257. rels.Relationships = append(rels.Relationships, xlsxRelationship{
  258. ID: ID.String(),
  259. Type: relType,
  260. Target: target,
  261. TargetMode: targetMode,
  262. })
  263. f.Relationships[relPath] = rels
  264. return rID
  265. }
  266. // UpdateLinkedValue fix linked values within a spreadsheet are not updating in
  267. // Office Excel 2007 and 2010. This function will be remove value tag when met a
  268. // cell have a linked value. Reference
  269. // https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating
  270. //
  271. // Notice: after open XLSX file Excel will be update linked value and generate
  272. // new value and will prompt save file or not.
  273. //
  274. // For example:
  275. //
  276. // <row r="19" spans="2:2">
  277. // <c r="B19">
  278. // <f>SUM(Sheet2!D2,Sheet2!D11)</f>
  279. // <v>100</v>
  280. // </c>
  281. // </row>
  282. //
  283. // to
  284. //
  285. // <row r="19" spans="2:2">
  286. // <c r="B19">
  287. // <f>SUM(Sheet2!D2,Sheet2!D11)</f>
  288. // </c>
  289. // </row>
  290. //
  291. func (f *File) UpdateLinkedValue() error {
  292. wb := f.workbookReader()
  293. // recalculate formulas
  294. wb.CalcPr = nil
  295. for _, name := range f.GetSheetList() {
  296. xlsx, err := f.workSheetReader(name)
  297. if err != nil {
  298. if err.Error() == fmt.Sprintf("sheet %s is chart sheet", trimSheetName(name)) {
  299. continue
  300. }
  301. return err
  302. }
  303. for indexR := range xlsx.SheetData.Row {
  304. for indexC, col := range xlsx.SheetData.Row[indexR].C {
  305. if col.F != nil && col.V != "" {
  306. xlsx.SheetData.Row[indexR].C[indexC].V = ""
  307. xlsx.SheetData.Row[indexR].C[indexC].T = ""
  308. }
  309. }
  310. }
  311. }
  312. return nil
  313. }
  314. // AddVBAProject provides the method to add vbaProject.bin file which contains
  315. // functions and/or macros. The file extension should be .xlsm. For example:
  316. //
  317. // if err := f.SetSheetPrOptions("Sheet1", excelize.CodeName("Sheet1")); err != nil {
  318. // fmt.Println(err)
  319. // }
  320. // if err := f.AddVBAProject("vbaProject.bin"); err != nil {
  321. // fmt.Println(err)
  322. // }
  323. // if err := f.SaveAs("macros.xlsm"); err != nil {
  324. // fmt.Println(err)
  325. // }
  326. //
  327. func (f *File) AddVBAProject(bin string) error {
  328. var err error
  329. // Check vbaProject.bin exists first.
  330. if _, err = os.Stat(bin); os.IsNotExist(err) {
  331. return fmt.Errorf("stat %s: no such file or directory", bin)
  332. }
  333. if path.Ext(bin) != ".bin" {
  334. return errors.New("unsupported VBA project extension")
  335. }
  336. f.setContentTypePartVBAProjectExtensions()
  337. wb := f.relsReader(f.getWorkbookRelsPath())
  338. var rID int
  339. var ok bool
  340. for _, rel := range wb.Relationships {
  341. if rel.Target == "vbaProject.bin" && rel.Type == SourceRelationshipVBAProject {
  342. ok = true
  343. continue
  344. }
  345. t, _ := strconv.Atoi(strings.TrimPrefix(rel.ID, "rId"))
  346. if t > rID {
  347. rID = t
  348. }
  349. }
  350. rID++
  351. if !ok {
  352. wb.Relationships = append(wb.Relationships, xlsxRelationship{
  353. ID: "rId" + strconv.Itoa(rID),
  354. Target: "vbaProject.bin",
  355. Type: SourceRelationshipVBAProject,
  356. })
  357. }
  358. file, _ := ioutil.ReadFile(bin)
  359. f.XLSX["xl/vbaProject.bin"] = file
  360. return err
  361. }
  362. // setContentTypePartVBAProjectExtensions provides a function to set the
  363. // content type for relationship parts and the main document part.
  364. func (f *File) setContentTypePartVBAProjectExtensions() {
  365. var ok bool
  366. content := f.contentTypesReader()
  367. for _, v := range content.Defaults {
  368. if v.Extension == "bin" {
  369. ok = true
  370. }
  371. }
  372. for idx, o := range content.Overrides {
  373. if o.PartName == "/xl/workbook.xml" {
  374. content.Overrides[idx].ContentType = ContentTypeMacro
  375. }
  376. }
  377. if !ok {
  378. content.Defaults = append(content.Defaults, xlsxDefault{
  379. Extension: "bin",
  380. ContentType: ContentTypeVBA,
  381. })
  382. }
  383. }