lib.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package excelize
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "io"
  6. "log"
  7. "math"
  8. "os"
  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) ([]FileList, 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) ([]FileList, error) {
  23. var fileList []FileList
  24. for _, v := range r.File {
  25. singleFile := FileList{
  26. Key: v.Name,
  27. Value: readFile(v),
  28. }
  29. fileList = append(fileList, singleFile)
  30. }
  31. return fileList, nil
  32. }
  33. // Read XML content as string and replace drawing property in XML namespace of sheet
  34. func readXml(files []FileList, name string) string {
  35. for _, file := range files {
  36. if file.Key == name {
  37. return strings.Replace(file.Value, "<drawing r:id=", "<drawing rid=", -1)
  38. }
  39. }
  40. return ``
  41. }
  42. // Update given file content in file list of XLSX
  43. func saveFileList(files []FileList, name string, content string) []FileList {
  44. for k, v := range files {
  45. if v.Key == name {
  46. files = files[:k+copy(files[k:], files[k+1:])]
  47. files = append(files, FileList{
  48. Key: name,
  49. Value: XMLHeader + content,
  50. })
  51. return files
  52. }
  53. }
  54. files = append(files, FileList{
  55. Key: name,
  56. Value: XMLHeader + content,
  57. })
  58. return files
  59. }
  60. // Read file content as string in a archive file
  61. func readFile(file *zip.File) string {
  62. rc, err := file.Open()
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. buff := bytes.NewBuffer(nil)
  67. io.Copy(buff, rc)
  68. rc.Close()
  69. return string(buff.Bytes())
  70. }
  71. // Convert integer to Excel sheet column title
  72. func toAlphaString(value int) string {
  73. if value < 0 {
  74. return ``
  75. }
  76. var ans string
  77. i := value
  78. for i > 0 {
  79. ans = string((i-1)%26+65) + ans
  80. i = (i - 1) / 26
  81. }
  82. return ans
  83. }
  84. // Convert Excel sheet column title to int
  85. func titleToNumber(s string) int {
  86. weight := 0.0
  87. sum := 0
  88. for i := len(s) - 1; i >= 0; i-- {
  89. sum = sum + (int(s[i])-int('A')+1)*int(math.Pow(26, weight))
  90. weight++
  91. }
  92. return sum - 1
  93. }
  94. // Check the file exists
  95. func pathExist(_path string) bool {
  96. _, err := os.Stat(_path)
  97. if err != nil && os.IsNotExist(err) {
  98. return false
  99. }
  100. return true
  101. }
  102. // Split Excel sheet column title to string and integer, return XAxis
  103. func getColIndex(axis string) string {
  104. r, err := regexp.Compile(`[^\D]`)
  105. if err != nil {
  106. log.Fatal(err)
  107. }
  108. return string(r.ReplaceAll([]byte(axis), []byte("")))
  109. }
  110. // Split Excel sheet column title to string and integer, return YAxis
  111. func getRowIndex(axis string) int {
  112. r, err := regexp.Compile(`[\D]`)
  113. if err != nil {
  114. log.Fatal(err)
  115. }
  116. row, err := strconv.Atoi(string(r.ReplaceAll([]byte(axis), []byte(""))))
  117. if err != nil {
  118. log.Fatal(err)
  119. }
  120. return row
  121. }