excelize.go 6.0 KB

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