You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 Names keyword 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?

  1. Get All Sheet Names: This keyword grabs every sheet name in your workbook, so you don't have to hardcode sheet names like Sheet1.
  2. Outer Loop: We loop over each sheet name, switching to that sheet with Set Active Sheet before extracting data.
  3. 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']}).
  4. 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 Names and Set Active Sheet keywords.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:42:30