Spring批量xlxs文件到MySql数据库

时间:2017-05-04 08:58:10

标签: mysql spring spring-batch

我是春季批次的新手,我实现了一个cvs文件的例子,其中colmuns用逗号分隔 但我有一个xlxs文件,它只包含没有分隔符的colmuns我想插入数据而不在我的xlxs文件中使用分隔符只是列。

以下是工作报告的内容

<bean id="report" class="com.mkyong.model.Report" scope="prototype" />
<batch:job id="reportJob">
<batch:step id="step1">
  <batch:tasklet>
    <batch:chunk reader="cvsFileItemReader" writer="mysqlItemWriter"
        commit-interval="2">
    </batch:chunk>
  </batch:tasklet>
</batch:step>

  <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

<!-- Read a csv file -->
<property name="resource" value="classpath:cvs/report.csv" />

<property name="lineMapper">
    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
      <!-- split it -->
      <property name="lineTokenizer">
            <bean
          class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
            <property name="names" value="date,impressions,clicks,earning" />
        </bean>
      </property>
      <property name="fieldSetMapper">
             <!-- return back to reader, rather than a mapped object. -->
             <!--
         <bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
              -->
          <!-- map to an object -->
          <bean
            class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
            <property name="prototypeBeanName" value="report" />
          </bean>
      </property>

      </bean>
  </property>

  </bean>
 <bean id="mysqlItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql">
  <value>
        <![CDATA[
            insert into RAW_REPORT(DATE,IMPRESSIONS,CLICKS,EARNING)
        values (:date, :impressions, :clicks, :earning)
        ]]>
  </value>
</property>
<!-- It will take care matching between object property and sql name parameter -->
<property name="itemSqlParameterSourceProvider">
    <bean
    class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</property>

当我执行我的代码时出现此错误

org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[class path resource [cvs/report.csv]], input=[123456;139,237;37;123456]

PS:在我的文件中,每列包含没有分隔符的值。

1 个答案:

答案 0 :(得分:1)

如果要从使用Excel创建的.XLS或.XLSX文件中读取批处理作业的输入数据,则需要使用包含Excel的ItemReader实现的Spring Batch扩展。你需要从源代码构建jar。这是链接:https://github.com/spring-projects/spring-batch-extensions

并使用如下:

<bean id="excelReader" class="org.springframework.batch.item.excel.poi.PoiItemReader">
    <property name="resource" value="/path/to/your/excel/file" />
    <property name="rowMapper">
        <bean class="org.springframework.batch.item.excel.mapping.BeanWrapperowMapper">
            <property name="targetType" value="com.your.package.Model" />
        <bean>
    </property>
</bean>

参考:https://github.com/spring-projects/spring-batch-extensions/tree/master/spring-batch-excel