java 导出dbf 格式
依赖
<!-- https://mvnrepository.com/artifact/com.github.albfernandez/javadbf -->
<dependency>
<groupId>com.github.albfernandez</groupId>
<artifactId>javadbf</artifactId>
<version>1.9.4</version>
</dependency>
导出dbf
package com.example.easyexcel.dbf;
import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* javadbf包 导入导出
*
* 参考文章:http://priede.bf.lu.lv/ftp/pub/DatuBazes/DBF/javadbf/javadbf-tutorial.html
* @author lcm
*/
public class DBFWriterTest {
public static void main( String args[])
throws DBFException, IOException {
// let us create field definitions first
// we will go for 3 fields
//
DBFField fields[] = new DBFField[ 3];
fields[0] = new DBFField();
fields[0].setName( "emp_code");
fields[0].setType(DBFDataType.CHARACTER);
fields[0].setLength( 10);
fields[1] = new DBFField();
fields[1].setName( "emp_name");
fields[1].setType(DBFDataType.CHARACTER);
fields[1].setLength( 20);
fields[2] = new DBFField();
fields[2].setName( "salary");
fields[2].setType(DBFDataType.FLOATING_POINT );
fields[2].setLength( 12);
fields[2].setDecimalCount( 2);
FileOutputStream fos = new FileOutputStream("C:\temp\test.dbf");
DBFWriter writer = new DBFWriter(fos, Charset.defaultCharset());
writer.setFields( fields);
// now populate DBFWriter
//
Object[] rowData = new Object[3];
rowData[0] = "1000";
rowData[1] = "John";
rowData[2] = 5000.00;
writer.addRecord( rowData);
rowData = new Object[3];
rowData[0] = "1001";
rowData[1] = "Lalit";
rowData[2] = 3400.00;
writer.addRecord( rowData);
rowData = new Object[3];
rowData[0] = "1002";
rowData[1] = "Rohit";
rowData[2] = 7350.00;
writer.addRecord( rowData);
writer.close();
fos.close();
}
}
导入dbf
package com.example.easyexcel.dbf;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class JavaDBFReaderTest {
public static void main( String args[]) {
try {
// create a DBFReader object
//
InputStream inputStream = new FileInputStream( args[ 0]); // take dbf file as program argument
DBFReader reader = new DBFReader( inputStream);
// get the field count if you want for some reasons like the following
//
int numberOfFields = reader.getFieldCount();
// use this count to fetch all field information
// if required
//
for( int i=0; i<numberOfFields; i++) {
DBFField field = reader.getField( i);
// do something with it if you want
// refer the JavaDoc API reference for more details
//
System.out.println( field.getName());
}
// Now, lets us start reading the rows
//
Object []rowObjects;
while( (rowObjects = reader.nextRecord()) != null) {
for( int i=0; i<rowObjects.length; i++) {
System.out.println( rowObjects[i]);
}
}
// By now, we have itereated through all of the rows
inputStream.close();
}
catch( DBFException e) {
System.out.println( e.getMessage());
}
catch( IOException e) {
System.out.println( e.getMessage());
}
}
}