lib.go 2.4 KB

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