使用spring批处理和apache poi写入来自多个源的数据

我目前有一个可用的Spring批处理应用程序,它使用ItemReader从Oracle数据库中读取SQL视图,并将该数据写入Excel文件。 但是,我想从多个视图中读取数据并写入同一个Excel文件 – 我该如何实现?

码:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:c24="http://schema.c24.biz/spring-core" xmlns:bat-c24="http://schema.c24.biz/spring-batch" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://schema.c24.biz/spring-core http://schema.c24.biz/spring-core.xsd http://schema.c24.biz/spring-batch http://schema.c24.biz/spring-batch.xsd"> <bean id="launchHelper" class="com.launcher.management.DummyPreJobHelper" /> <bean id="rowMapper" class="com.exporter.DynamicComplexDataObjectRowMapper"/> <bean id="itemReader" class="com.exporter.ViewJdbcCursorItemReader" scope="step"> <property name="dataSource" ref="dataSource" /> <property name="viewName" value="V_CARS" /> <property name="fetchSize" value="5000" /> <property name="rowMapper" ref="rowMapper" /> </bean> <bean id="itemWriter" class="com.exporter.ExcelFileItemWriter" scope="step"> <property name="resource" value="file:${working.directory}/#{jobParameters['output.file']}" /> </bean> <!-- Batch job configuration --> <batch:job id="excel-report-job"> <batch:step id="export"> <batch:tasklet allow-start-if-complete="true"> <batch:chunk reader="itemReader" writer="itemWriter" commit-interval="5000"> <batch:listeners> <batch:listener> <bean class="com.utils.LoggingStepListener" /> </batch:listener> </batch:listeners> </batch:chunk> </batch:tasklet> </batch:step> </batch:job> </beans> 

请参阅此链接从多个表中的单个Excel文件中写入不同的表格 。

我也想这样做一次,我build议你在“itemWriter”@BeforeStep尝试下面的步骤和代码。

  1. 加载exel文件并初始化现有的Excel工作簿对象
  2. 如果文件不存在,则创build新的Excel工作簿对象
  3. 使用上面的工作簿对象,并写入特定的选项卡,您希望您的数据所在。
 File xlsxFile = new File(outputFilename); if (xlsxFile.exists() && !xlsxFile.isDirectory()) { InputStream fileIn = null; try { fileIn = new BufferedInputStream(new FileInputStream(xlsxFile), 100); workbook = new SXSSFWorkbook(new XSSFWorkbook(fileIn), 100); } catch (Exception e) { e.printStackTrace(); } finally { if (fileIn != null) { try { fileIn.close(); } catch (IOException e) { e.printStackTrace(); } } } } else { workbook = new SXSSFWorkbook(100); }