excelize.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package excelize
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/xml"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. // File define a populated xlsx.File struct.
  11. type File struct {
  12. XLSX map[string]string
  13. Path string
  14. }
  15. // OpenFile take the name of an XLSX file and returns a populated
  16. // xlsx.File struct for it.
  17. func OpenFile(filename string) *File {
  18. var f *zip.ReadCloser
  19. file := make(map[string]string)
  20. f, _ = zip.OpenReader(filename)
  21. file, _ = ReadZip(f)
  22. return &File{
  23. XLSX: file,
  24. Path: filename,
  25. }
  26. }
  27. // SetCellInt provide function to set int type value of a cell
  28. func (f *File) SetCellInt(sheet string, axis string, value int) {
  29. axis = strings.ToUpper(axis)
  30. var xlsx xlsxWorksheet
  31. col := getColIndex(axis)
  32. row := getRowIndex(axis)
  33. xAxis := row - 1
  34. yAxis := titleToNumber(col)
  35. name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
  36. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  37. rows := xAxis + 1
  38. cell := yAxis + 1
  39. xlsx = checkRow(xlsx)
  40. xlsx = completeRow(xlsx, rows, cell)
  41. xlsx = completeCol(xlsx, rows, cell)
  42. xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
  43. xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
  44. output, err := xml.Marshal(xlsx)
  45. if err != nil {
  46. fmt.Println(err)
  47. }
  48. f.saveFileList(name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
  49. }
  50. // SetCellStr provide function to set string type value of a cell
  51. func (f *File) SetCellStr(sheet string, axis string, value string) {
  52. axis = strings.ToUpper(axis)
  53. var xlsx xlsxWorksheet
  54. col := getColIndex(axis)
  55. row := getRowIndex(axis)
  56. xAxis := row - 1
  57. yAxis := titleToNumber(col)
  58. name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
  59. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  60. rows := xAxis + 1
  61. cell := yAxis + 1
  62. xlsx = checkRow(xlsx)
  63. xlsx = completeRow(xlsx, rows, cell)
  64. xlsx = completeCol(xlsx, rows, cell)
  65. xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
  66. xlsx.SheetData.Row[xAxis].C[yAxis].V = value
  67. output, err := xml.Marshal(xlsx)
  68. if err != nil {
  69. fmt.Println(err)
  70. }
  71. f.saveFileList(name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
  72. }
  73. // Completion column element tags of XML in a sheet
  74. func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
  75. if len(xlsx.SheetData.Row) < cell {
  76. for i := len(xlsx.SheetData.Row); i < cell; i++ {
  77. xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{
  78. R: i + 1,
  79. })
  80. }
  81. }
  82. buffer := bytes.Buffer{}
  83. for k, v := range xlsx.SheetData.Row {
  84. if len(v.C) < cell {
  85. start := len(v.C)
  86. for iii := start; iii < cell; iii++ {
  87. buffer.WriteString(toAlphaString(iii + 1))
  88. buffer.WriteString(strconv.Itoa(k + 1))
  89. xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{
  90. R: buffer.String(),
  91. })
  92. buffer.Reset()
  93. }
  94. }
  95. }
  96. return xlsx
  97. }
  98. // Completion row element tags of XML in a sheet
  99. func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
  100. if len(xlsx.SheetData.Row) < row {
  101. for i := len(xlsx.SheetData.Row); i < row; i++ {
  102. xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{
  103. R: i + 1,
  104. })
  105. }
  106. buffer := bytes.Buffer{}
  107. for ii := 0; ii < row; ii++ {
  108. start := len(xlsx.SheetData.Row[ii].C)
  109. if start == 0 {
  110. for iii := start; iii < cell; iii++ {
  111. buffer.WriteString(toAlphaString(iii + 1))
  112. buffer.WriteString(strconv.Itoa(ii + 1))
  113. xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{
  114. R: buffer.String(),
  115. })
  116. buffer.Reset()
  117. }
  118. }
  119. }
  120. }
  121. return xlsx
  122. }
  123. // Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible Office Excel 2007
  124. func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
  125. oldXmlns := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
  126. newXmlns := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">`
  127. workbookMarshal = strings.Replace(workbookMarshal, oldXmlns, newXmlns, -1)
  128. workbookMarshal = strings.Replace(workbookMarshal, `></sheetPr>`, ` />`, -1)
  129. workbookMarshal = strings.Replace(workbookMarshal, `></dimension>`, ` />`, -1)
  130. workbookMarshal = strings.Replace(workbookMarshal, `></selection>`, ` />`, -1)
  131. workbookMarshal = strings.Replace(workbookMarshal, `></sheetFormatPr>`, ` />`, -1)
  132. workbookMarshal = strings.Replace(workbookMarshal, `></printOptions>`, ` />`, -1)
  133. workbookMarshal = strings.Replace(workbookMarshal, `></pageSetup>`, ` />`, -1)
  134. workbookMarshal = strings.Replace(workbookMarshal, `></pageMargins>`, ` />`, -1)
  135. workbookMarshal = strings.Replace(workbookMarshal, `></headerFooter>`, ` />`, -1)
  136. workbookMarshal = strings.Replace(workbookMarshal, `></drawing>`, ` />`, -1)
  137. return workbookMarshal
  138. }
  139. // Check XML tags and fix discontinuous case, for example:
  140. //
  141. // <row r="15" spans="1:22" x14ac:dyDescent="0.2">
  142. // <c r="A15" s="2" />
  143. // <c r="B15" s="2" />
  144. // <c r="F15" s="1" />
  145. // <c r="G15" s="1" />
  146. // </row>
  147. //
  148. // in this case, we should to change it to
  149. //
  150. // <row r="15" spans="1:22" x14ac:dyDescent="0.2">
  151. // <c r="A15" s="2" />
  152. // <c r="B15" s="2" />
  153. // <c r="C15" s="2" />
  154. // <c r="D15" s="2" />
  155. // <c r="E15" s="2" />
  156. // <c r="F15" s="1" />
  157. // <c r="G15" s="1" />
  158. // </row>
  159. //
  160. func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
  161. buffer := bytes.Buffer{}
  162. for k, v := range xlsx.SheetData.Row {
  163. lenCol := len(v.C)
  164. if lenCol < 1 {
  165. continue
  166. }
  167. endR := getColIndex(v.C[lenCol-1].R)
  168. endRow := getRowIndex(v.C[lenCol-1].R)
  169. endCol := titleToNumber(endR)
  170. if lenCol < endCol {
  171. oldRow := xlsx.SheetData.Row[k].C
  172. xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
  173. tmp := []xlsxC{}
  174. for i := 0; i <= endCol; i++ {
  175. buffer.WriteString(toAlphaString(i + 1))
  176. buffer.WriteString(strconv.Itoa(endRow))
  177. tmp = append(tmp, xlsxC{
  178. R: buffer.String(),
  179. })
  180. buffer.Reset()
  181. }
  182. xlsx.SheetData.Row[k].C = tmp
  183. for _, y := range oldRow {
  184. colAxis := titleToNumber(getColIndex(y.R))
  185. xlsx.SheetData.Row[k].C[colAxis] = y
  186. }
  187. }
  188. }
  189. return xlsx
  190. }