正如coberty所指出的,您可以使用
<worksheet idx="0">
但为了完整起见:我遇到了类似的问题,但我需要在运行时指定工作表名称(用户从用户选择的 Excel 文件的工作表列表中选择,通过 Apache POI 读取工作表名称)。我通过使用网站上的 JXLS 阅读器示例并稍作更改来完成此操作:
代替:
final XLSReader reader = ReaderBuilder.buildFromXML(xmlInputStream);
我使用 Apache Digester setSubstitutor 使用此方法的副本,稍作更改(不幸的是,ReaderBuilder 不允许将替代器作为参数传递......):
final XLSReader reader = buildFromXML(xmlInputStream, sheetName);
...
public static XLSReader buildFromXML(final InputStream xmlStream, final String sheetName)
throws IOException, SAXException {
final Digester digester = new Digester();
// hier geschieht die Substituierung des Tabellenblattnamens -
// der Rest der Methode entspricht dem Original
substituteSheetName(sheetName, digester);
digester.setValidating(false);
digester.addObjectCreate("workbook", "org.jxls.reader.XLSReaderImpl");
digester.addObjectCreate("workbook/worksheet", "org.jxls.reader.XLSSheetReaderImpl");
digester.addSetProperties("workbook/worksheet", "name", "sheetName");
digester.addSetProperties("workbook/worksheet", "idx", "sheetIdx");
digester.addSetNext("workbook/worksheet", "addSheetReader");
digester.addObjectCreate("*/loop", "org.jxls.reader.XLSForEachBlockReaderImpl");
digester.addSetProperties("*/loop");
digester.addSetNext("*/loop", "addBlockReader");
digester.addObjectCreate("*/section", "org.jxls.reader.SimpleBlockReaderImpl");
digester.addSetProperties("*/section");
digester.addSetNext("*/section", "addBlockReader");
digester.addObjectCreate("*/mapping", "org.jxls.reader.BeanCellMapping");
digester.addSetProperties("*/mapping");
digester.addCallMethod("*/mapping", "setFullPropertyName", 1);
digester.addCallParam("*/mapping", 0);
digester.addSetNext("*/mapping", "addMapping");
digester.addObjectCreate("*/loop/loopbreakcondition", "org.jxls.reader.SimpleSectionCheck");
digester.addSetNext("*/loop/loopbreakcondition", "setLoopBreakCondition");
digester.addObjectCreate("*/loopbreakcondition/rowcheck", "org.jxls.reader.OffsetRowCheckImpl");
digester.addSetProperties("*/loopbreakcondition/rowcheck");
digester.addSetNext("*/loopbreakcondition/rowcheck", "addRowCheck");
digester.addObjectCreate("*/rowcheck/cellcheck", "org.jxls.reader.OffsetCellCheckImpl");
digester.addSetProperties("*/rowcheck/cellcheck");
digester.addCallMethod("*/rowcheck/cellcheck", "setValue", 1);
digester.addCallParam("*/rowcheck/cellcheck", 0);
digester.addSetNext("*/rowcheck/cellcheck", "addCellCheck");
return (XLSReader) digester.parse(xmlStream);
}
private static void substituteSheetName(final String sheetName, final Digester digester) {
// set up the variables the input xml can reference
final Map<String, Object> vars = new HashMap<String, Object>();
vars.put("sheetName", sheetName);
// map ${varname} to the entries in the var map
final MultiVariableExpander expander = new MultiVariableExpander();
expander.addSource("$", vars);
// allow expansion in both xml attributes and element text
final Substitutor substitutor = new VariableSubstitutor(expander);
digester.setSubstitutor(substitutor);
}
现在我可以在 JXLS Reader XML 配置中使用变量 sheetName:
<worksheet name="${sheetName}">