lib.go 2.5 KB

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