如需转载分享,请标明出处,且不用于盈利为目的,谢谢合作!
将List 类型数据导出到DBF文件
简单介绍学习过程遇到的问题:
定义dbf文件字段fields[i].setName(names[i]);
时如果设置的名字是中文会报错,解决办法:
下载源码https://github.com/albfernandez/javadbf
打包 最新的javadbf版本1.10.2,支持dbf文件field字段为中文
步骤:
1.git clone https://github.com/albfernandez/javadbf.git
2.跳过test打包 mvn clean package compile -Dmaven.test.skip=true
3.将打的包放到本地maven库 mvn install:install-file -Dfile=D:\javadbf-1.10.2-SNAPSHOT.jar -DgroupId=commvn install:install-file -Dfile=D:\javadbf-1.10.2-SNAPSHOT.jar -DgroupId=com
1.添加pom.xml依赖
<!--javadbf-->
<dependency>
<groupId>com.github.albfernandez</groupId>
<artifactId>javadbf</artifactId>
<version>1.10.2</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.新建实体类User.java
package com.yss.file.filedown.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
}
3.新建类DataToDBF.java
package com.yss.file.filedown.util;
import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFUtils;
import com.linuxense.javadbf.DBFWriter;
import com.yss.file.filedown.entity.User;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class DataToDBF {
/**
* 创建dbf
* 下载源码打包 最新的javadbf版本1.10.2,支持dbf文件field字段为中文
* 步骤:1.git clone https://github.com/albfernandez/javadbf.git
* 2.跳过test打包 mvn clean package compile -Dmaven.test.skip=true
* 3.将打的包放到本地maven库mvn install:install-file -Dfile=D:\javadbf-1.10.2-SNAPSHOT.jar -DgroupId=commvn install:install-file -Dfile=D:\javadbf-1.10.2-SNAPSHOT.jar -DgroupId=com
* 4.pom中引入依赖
* <dependency>
* <groupId>com.github.albfernandez</groupId>
* <artifactId>javadbf</artifactId>
* <version>1.10.2</version>
* </dependency>
*
* @param src:文件路径
* @throws IOException
*/
public static void createDBF(String src)
throws Exception {
System.out.println("数据加载...");
List<User> list = new ArrayList<>();
User s1 = new User("张三", 22, "男");
User s2 = new User("李四", 22, "男");
User s3 = new User("王五", 22, "男");
User s4 = new User("赵敏", 22, "女");
User s5 = new User("张无忌", 22, "男");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
System.out.println("数据加载完成...");
Class clazz = Class.forName("com.yss.file.filedown.entity.User");
Field[] fs = clazz.getDeclaredFields();
int length = fs.length;
String[] names = {"姓名", "年龄", "性别"};
DBFWriter writer = null;
FileOutputStream fos = null;
try {
//定义DBF文件字段
DBFField[] fields = new DBFField[length];
for (int i = 0; i < fields.length; i++) {
fields[i] = new DBFField();
fields[i].setName(names[i]);
if (fs[i].getType().getName().equals("int")) {
fields[i].setType(DBFDataType.NUMERIC);
fields[i].setLength(10);
} else if (fs[i].getType().getName().equals("java.lang.String")) {
fields[i].setType(DBFDataType.CHARACTER);
fields[i].setLength(20);
} else if (fs[i].getType().getName().equals("java.lang.Boolean")) {
fields[i].setType(DBFDataType.LOGICAL);
fields[i].setLength(12);
} else if (fs[i].getType().getName().equals("java.util.Date")) {
fields[i].setType(DBFDataType.DATE);
fields[i].setLength(12);
} else if (fs[i].getType().getName().equals("java.math.BigDecimal")) {
fields[i].setType(DBFDataType.FLOATING_POINT);
fields[i].setLength(12);
}
}
/*//定义DBF文件字段
DBFField[] fields = new DBFField[3];
//分别定义各个字段信息,setFieldName和setName作用相同,
fields[0] = new DBFField();
fields[0].setName("姓名");
fields[0].setType(DBFDataType.CHARACTER);
fields[0].setLength(20);
fields[1] = new DBFField();
fields[1].setName("年龄");
fields[1].setType(DBFDataType.NUMERIC);
fields[1].setLength(3);
fields[2] = new DBFField();
fields[2].setName("性别");
fields[2].setType(DBFDataType.CHARACTER);
fields[2].setLength(20);*/
fos = new FileOutputStream(new File(src));
//定义DBFWriter实例用来写DBF文件
writer = new DBFWriter(fos, Charset.forName("GBK"));
//把字段信息写入DBFWriter实例,即定义表结构
writer.setFields(fields);
//一条条的写入记录
for (int i = 0; i < list.size(); i++) {
Object[] rowData = new Object[3];
rowData[0] = list.get(i).getName();
rowData[1] = list.get(i).getAge();
rowData[2] = list.get(i).getSex();
writer.addRecord(rowData);
}
//写入数据
writer.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBFUtils.close(fos);
DBFUtils.close(writer);
}
}
public static void main(String[] args) throws Exception {
createDBF("d:/test.dbf");
}
}
3.数据结果