lib.go 2.3 KB

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