lib.go 2.7 KB

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