package excel import ( "errors" "github.com/beevik/etree" ) // GetDefaultSheetIndex 获取数据源工作表索引 func GetDefaultSheetIndex(workbookPath string, defaultSheetName string) (defaultSheetIndex int, err error) { doc := etree.NewDocument() if err = doc.ReadFromFile(workbookPath); err != nil { return } flag := false workbook := doc.SelectElement("workbook") sheets := workbook.SelectElement("sheets") for index, sheet := range sheets.SelectElements("sheet") { for _, attr := range sheet.Attr { if attr.Key == "name" && attr.Value == defaultSheetName { defaultSheetIndex = index flag = true } } } if flag == false { err = errors.New(defaultSheetName + "工作表未找到") } return } // GetDefaultSheetIndex 获取数据源工作表索引 func GetDefaultSheetData(sheetPath string, defaultSheetName string) (defaultSheetIndex int, err error) { doc := etree.NewDocument() if err = doc.ReadFromFile(sheetPath); err != nil { return } flag := false workbook := doc.SelectElement("worksheet") sheets := workbook.SelectElement("sheets") for index, sheet := range sheets.SelectElements("sheet") { for _, attr := range sheet.Attr { if attr.Key == "name" && attr.Value == defaultSheetName { defaultSheetIndex = index flag = true } } } if flag == false { err = errors.New(defaultSheetName + "工作表未找到") } return }