使用Fillo读取Excel多参数报错:Recordset.getField不兼容String[]参数
getField Argument Mismatch Error for Multi-Parameter Queries Hey there, let's break down why you're seeing that error and how to fix it quickly.
The Root Cause
The error message says it all: getField(String) only accepts a single string argument, but you're passing multi_data—which is a String array (since String... in Java is syntactic sugar for a String array). You can't pass an array directly to a method that expects a single string, hence the mismatch.
The Solution
You need to loop through each field name in the multi_data array, call getField() on each one individually, and concatenate the results. Using a StringBuilder is the most efficient way to do this instead of chaining string concatenations.
Here's the revised code:
public static void read_by_excel(String query, String field, String ... multi_data) throws FilloException { ArrayList<Object> excel_data = new ArrayList<Object>(); Fillo fillo = new Fillo(); Connection connection = fillo.getConnection("C:\\Users\\Vishrut\\Desktop\\read_openxl.xlsx"); String strQuery = query; Recordset rs = connection.executeQuery(strQuery); while (rs.next()) { // Use StringBuilder for efficient string concatenation StringBuilder fieldValues = new StringBuilder(); // Add the first required field fieldValues.append(rs.getField(field)); // Loop through all additional fields in multi_data for (String additionalField : multi_data) { // Optional: Add a separator like " | " for readability fieldValues.append(" | ").append(rs.getField(additionalField)); } excel_data.add(fieldValues.toString()); } for (Object data: excel_data) { System.out.println(data); } rs.close(); connection.close(); }
Key Changes Explained
- StringBuilder Usage: Avoids the overhead of creating multiple string objects with each concatenation, which is better for performance especially if you're dealing with many rows or fields.
- Loop Through
multi_data: Iterates over each field name in the variable arguments array, callinggetField()on each one to fetch its value. - Optional Separator: Added a
|separator between values to make the output more readable—you can change this to any character/string you prefer, or remove it if you want raw concatenation.
Your existing call function_class.read_by_excel("select * from Sheet1 where employee_id = 60546", "department","salary"); will work perfectly with this updated code.
内容的提问来源于stack exchange,提问作者user3814582

