lib.go 2.7 KB

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