lib.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 xlsx.File struct
  11. // populated with its contents. In most cases ReadZip is not used directly, but
  12. // 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 touching the
  18. // 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] = replaceWorkSheetsRelationshipsNameSpace(string(output))
  31. worksheets++
  32. }
  33. }
  34. }
  35. return fileList, worksheets, nil
  36. }
  37. // readXML provides function to 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. // saveFileList provides function to update given file content in file list of
  45. // 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. // toAlphaString provides function to convert integer to Excel sheet column
  61. // title.
  62. func toAlphaString(value int) string {
  63. if value < 0 {
  64. return ""
  65. }
  66. var ans string
  67. i := value
  68. for i > 0 {
  69. ans = string((i-1)%26+65) + ans
  70. i = (i - 1) / 26
  71. }
  72. return ans
  73. }
  74. // titleToNumber provides function to convert Excel sheet column title to int.
  75. func titleToNumber(s string) int {
  76. weight := 0.0
  77. sum := 0
  78. for i := len(s) - 1; i >= 0; i-- {
  79. sum = sum + (int(s[i])-int('A')+1)*int(math.Pow(26, weight))
  80. weight++
  81. }
  82. return sum - 1
  83. }
  84. // letterOnlyMapF is used in conjunction with strings.Map to return only the
  85. // characters A-Z and a-z in a string.
  86. func letterOnlyMapF(rune rune) rune {
  87. switch {
  88. case 'A' <= rune && rune <= 'Z':
  89. return rune
  90. case 'a' <= rune && rune <= 'z':
  91. return rune - 32
  92. }
  93. return -1
  94. }
  95. // intOnlyMapF is used in conjunction with strings.Map to return only the
  96. // numeric portions of a string.
  97. func intOnlyMapF(rune rune) rune {
  98. if rune >= 48 && rune < 58 {
  99. return rune
  100. }
  101. return -1
  102. }