lib.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package excelize
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/xml"
  6. "io"
  7. "log"
  8. "math"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. )
  13. // ReadZip takes a pointer to a zip.ReadCloser and returns a
  14. // xlsx.File struct populated with its contents. In most cases
  15. // ReadZip is not used directly, but is called internally by OpenFile.
  16. func ReadZip(f *zip.ReadCloser) (map[string]string, int, error) {
  17. defer f.Close()
  18. return ReadZipReader(&f.Reader)
  19. }
  20. // ReadZipReader can be used to read an XLSX in memory without
  21. // touching the filesystem.
  22. func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
  23. fileList := make(map[string]string)
  24. worksheets := 0
  25. for _, v := range r.File {
  26. fileList[v.Name] = readFile(v)
  27. if len(v.Name) > 18 {
  28. if v.Name[0:19] == "xl/worksheets/sheet" {
  29. var xlsx xlsxWorksheet
  30. xml.Unmarshal([]byte(strings.Replace(fileList[v.Name], "<drawing r:id=", "<drawing rid=", -1)), &xlsx)
  31. xlsx = checkRow(xlsx)
  32. output, _ := xml.Marshal(xlsx)
  33. fileList[v.Name] = replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output)))
  34. worksheets++
  35. }
  36. }
  37. }
  38. return fileList, worksheets, nil
  39. }
  40. // Read XML content as string and replace drawing property in XML namespace of sheet
  41. func (f *File) readXML(name string) string {
  42. if content, ok := f.XLSX[name]; ok {
  43. return strings.Replace(content, "<drawing r:id=", "<drawing rid=", -1)
  44. }
  45. return ``
  46. }
  47. // Update given file content in file list of XLSX
  48. func (f *File) saveFileList(name string, content string) {
  49. f.XLSX[name] = XMLHeader + content
  50. }
  51. // Read file content as string in a archive file
  52. func readFile(file *zip.File) string {
  53. rc, err := file.Open()
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. buff := bytes.NewBuffer(nil)
  58. io.Copy(buff, rc)
  59. rc.Close()
  60. return string(buff.Bytes())
  61. }
  62. // Convert integer to Excel sheet column title
  63. func toAlphaString(value int) string {
  64. if value < 0 {
  65. return ``
  66. }
  67. var ans string
  68. i := value
  69. for i > 0 {
  70. ans = string((i-1)%26+65) + ans
  71. i = (i - 1) / 26
  72. }
  73. return ans
  74. }
  75. // Convert Excel sheet column title to int
  76. func titleToNumber(s string) int {
  77. weight := 0.0
  78. sum := 0
  79. for i := len(s) - 1; i >= 0; i-- {
  80. sum = sum + (int(s[i])-int('A')+1)*int(math.Pow(26, weight))
  81. weight++
  82. }
  83. return sum - 1
  84. }
  85. // Split Excel sheet column title to string and integer, return XAxis
  86. func getColIndex(axis string) string {
  87. r, err := regexp.Compile(`[^\D]`)
  88. if err != nil {
  89. log.Fatal(err)
  90. }
  91. return string(r.ReplaceAll([]byte(axis), []byte("")))
  92. }
  93. // Split Excel sheet column title to string and integer, return YAxis
  94. func getRowIndex(axis string) int {
  95. r, err := regexp.Compile(`[\D]`)
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. row, err := strconv.Atoi(string(r.ReplaceAll([]byte(axis), []byte(""))))
  100. if err != nil {
  101. log.Fatal(err)
  102. }
  103. return row
  104. }