xml.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package excel
  2. import (
  3. "errors"
  4. "github.com/beevik/etree"
  5. )
  6. // GetDefaultSheetIndex 获取数据源工作表索引
  7. func GetDefaultSheetIndex(workbookPath string, defaultSheetName string) (defaultSheetIndex int, err error) {
  8. doc := etree.NewDocument()
  9. if err = doc.ReadFromFile(workbookPath); err != nil {
  10. return
  11. }
  12. flag := false
  13. workbook := doc.SelectElement("workbook")
  14. sheets := workbook.SelectElement("sheets")
  15. for index, sheet := range sheets.SelectElements("sheet") {
  16. for _, attr := range sheet.Attr {
  17. if attr.Key == "name" && attr.Value == defaultSheetName {
  18. defaultSheetIndex = index
  19. flag = true
  20. }
  21. }
  22. }
  23. if flag == false {
  24. err = errors.New(defaultSheetName + "工作表未找到")
  25. }
  26. return
  27. }
  28. // GetDefaultSheetIndex 获取数据源工作表索引
  29. func GetDefaultSheetData(sheetPath string, defaultSheetName string) (defaultSheetIndex int, err error) {
  30. doc := etree.NewDocument()
  31. if err = doc.ReadFromFile(sheetPath); err != nil {
  32. return
  33. }
  34. flag := false
  35. workbook := doc.SelectElement("worksheet")
  36. sheets := workbook.SelectElement("sheets")
  37. for index, sheet := range sheets.SelectElements("sheet") {
  38. for _, attr := range sheet.Attr {
  39. if attr.Key == "name" && attr.Value == defaultSheetName {
  40. defaultSheetIndex = index
  41. flag = true
  42. }
  43. }
  44. }
  45. if flag == false {
  46. err = errors.New(defaultSheetName + "工作表未找到")
  47. }
  48. return
  49. }