1

此代码能够将数据写入 csv 文件,但唯一的问题是数据仅写入单列。

我希望数据出现在不同的列中。我是 bean io 的新手,无法弄清楚。

我尝试了下面给定的代码,但无法以正确的格式获得输出:

public class XlsWriter {

public static void main(String[] args) throws Exception {

StreamFactory factory = StreamFactory.newInstance();




  factory.load("C:\\Users\\PV5057094\\Demo_workspace\\XlsxMapper\\src\\main\\resources\\Employee.xml");

          Field[] fields = Employee.class.getDeclaredFields();

          System.out.println("fileds" + fields.length);
          List<Object> list = new ArrayList<Object>();
          for (Field field : fields) {

                 list.add(field.getName());

          }


          BeanReader in = factory.createReader("EmployeeInfo", new File("C:\\Temp\\Soc\\textInput.txt"));




          BeanWriter out = factory.createWriter("EmployeeInfo", new File("C:\\Temp\\Soc\\output.csv"));

          Object record;

          while ((record = in.read()) != null) {
                 System.out.println(record.toString().length());


                 out.write(record);

                 System.out.println("Record Written:" + record.toString());

          }

          in.close();
          out.flush();
          out.close();
   }


}

textInput.txt
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC
AAAAABBBBBCCCCC



<?xml version="1.0" encoding="UTF-8"?>
<beanio xmlns="http://www.beanio.org/2012/03"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

   <stream name="EmployeeInfo" format="fixedlength">

          <record name="employee"
                 class="com.aexp.gmnt.imc.record.submission.Employee" minOccurs="0"
                 maxOccurs="unbounded" order="1">
                 <field name="firstName" length="5" padding="0" justify="right" />
                 <field name="lastName"  length="5" padding="0" justify="right"/>
                 <field name="title" length="5" padding="0" justify="right"/>

          </record>
   </stream>

我想要 CSV 文件的不同列中的每个记录值,但目前它仅在单个列中出现,请帮助。

4

1 回答 1

0

您需要在映射文件中有不同的流定义才能写入 CSV 文件。流EmployeeInfo只能处理固定长度的内容,因为它是这样配置的。

您需要添加第二个<stream>定义来处理要生成的 CSV 文件,并且BeanWriter需要引用新的 CSV 流而不是固定长度的流。

将新<stream>定义添加到现有的 mapping.xml 文件中:

<stream name="EmployeeInfoCSV" format="csv">
  <record name="employee" class="com.aexp.gmnt.imc.record.submission.Employee" minOccurs="0" maxOccurs="unbounded">
    <field name="firstName" />
    <field name="lastName" />
    <field name="title" />
  </record>
</stream>

请注意 的名称<stream>format设置为的更改csv。在此<stream>定义中,如果您愿意,您还可以更改将数据写入 csv 文件BeanReader的顺序,而不会影响它预期读取数据的顺序。csv文件不需要,length和属性。paddingjustify

现在您只需要更改配置BeanWriterfrom 的方式:

BeanWriter out = factory.createWriter("EmployeeInfo", new File("C:\\Temp\\Soc\\output.csv"));

BeanWriter out = factory.createWriter("EmployeeInfoCSV", new File("C:\\Temp\\Soc\\output.csv"));

请注意在createWriter方法参数中使用 csv 流名称的更改。


编辑以从评论中回答这个问题:

只是一个附加的问题,我需要将带有标题值的第一行添加为字段值而不将它们写为bean io中的标题记录类型,那么是否可以通过反射或其他方式?

无需反思或跳过箍来完成它。您可以创建一个Writer可用于先将标题(列)名称写入文件的方法,然后再将写入器传递给BeanWriter用于附加输出的其余部分。

而不是使用BeanWriter上面的类似:

BeanWriter out = factory.createWriter("EmployeeInfoCSV", new File("C:\\Temp\\Soc\\output.csv"));

您现在将执行以下操作:

BufferedWriter writer = new BufferedWriter(new FileWriter(new File("C:\\Temp\\Soc\\output.csv")));
writer.write("First Name,Last Name,Title");
writer.newLine();

BeanWriter out = factory.createWriter("EmployeeInfoCSV", writer);

BeanIO然后将继续将其输出写入writer将数据附加到现有文件中。完成后也要记住close()作者。

于 2019-06-27T08:38:06.357 回答