Robot Framework中如何获取Excel多个工作表的数据?
How to Fetch Data from Multiple Excel Sheets in Robot Framework
Got it, let's tweak your code to pull data from all sheets in your Excel file instead of just one. The core idea is to first get a list of all sheet names, then loop through each sheet and apply your existing data extraction logic to each one.
Key Changes Needed:
- Use the
Get All Sheet Nameskeyword to retrieve every sheet in the workbook - Add an outer loop to iterate over each sheet name
- Store data in a structured format (like a dictionary) so you can track which data belongs to which sheet
Adjusted Full Code
# First, define your base variables ${file}= Normalised Path ${_downloads}/${Filename} ${filename}= Get File Name ${file} include_extension=True # Open the Excel document once (no need to re-open for each sheet) Open Excel Document ${file} alias=importantfile # Get ALL sheet names from the workbook @{all_sheets}= Get All Sheet Names alias=importantfile # Create a dictionary to store data per sheet (key: sheet name, value: list of rows) ${all_sheet_data}= Create Dictionary # Loop through each sheet in the workbook FOR ${sheet_name} IN @{all_sheets} # Switch to the current sheet Set Active Sheet ${sheet_name} alias=importantfile # Get all values from the sheet @{sheetvalues}= Get Sheet Values alias=importantfile # Create a list to store filtered data for this sheet ${sheet_data}= Create List # Apply your existing row filtering logic FOR ${row} IN @{sheetvalues} # Skip rows where first column is 'date' Continue For Loop If ${row}[0] == 'date' Append To List ${sheet_data} ${row} END # Add the filtered data to our main dictionary, mapped to the sheet name Set To Dictionary ${all_sheet_data} ${sheet_name}=${sheet_data} END # Optional: Close the Excel document when done Close Excel Document alias=importantfile
What's Different?
Get All Sheet Names: This keyword grabs every sheet name in your workbook, so you don't have to hardcode sheet names likeSheet1.- Outer Loop: We loop over each sheet name, switching to that sheet with
Set Active Sheetbefore extracting data. - Dictionary Storage: Using a dictionary (
${all_sheet_data}) lets you easily access data from specific sheets later (e.g.,${all_sheet_data['Sheet1']}or${all_sheet_data['SalesData']}). - Reusable Logic: Your original row filtering (skipping rows starting with 'date') is preserved and applied to every sheet automatically.
Notes:
- Make sure you're using the ExcelLibrary (or compatible library) that supports
Get All Sheet NamesandSet Active Sheetkeywords. - If you prefer to combine all data into a single list instead of tracking per sheet, you can skip the dictionary and just append all
${sheet_data}to a master list.
内容的提问来源于stack exchange,提问作者liki
相关产品推荐
相关产品推荐

