lib.go 2.4 KB

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