excelize.go 6.1 KB

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