lib.go 2.4 KB

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