簡體   English   中英

使用Spring Batch和Apache Poi從多個來源寫入數據

[英]Writing data from multiple sources using spring batch and apache poi

我目前有一個運行中的春季批處理應用程序,該應用程序使用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文件中寫入不同的表格

我也想這樣做一次,建議您在“ itemWriter” @BeforeStep中嘗試以下步驟和代碼。

  1. 加載exel文件並初始化您現有的Excel工作簿對象
  2. 如果文件不存在,請創建新的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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM