天天看點

Java實作DBF導入導出

原文位址:

https://blog.csdn.net/xzknet/article/details/18045761

首先加入maven依賴

<dependency>
    <groupId>com.github.albfernandez</groupId>
    <artifactId>javadbf</artifactId>
    <version>1.9.4</version>
</dependency>
           

1.讀DBF

package dbf;  
  
import java.io.FileInputStream;  
import java.io.InputStream;  
  
import com.linuxense.javadbf.DBFField;  
import com.linuxense.javadbf.DBFReader;  
  
public class readDBF {  
    public static void readDBF(String path) {  
        InputStream fis = null;  
        try {  
            // 讀取檔案的輸入流  
            fis = new FileInputStream(path);  
            // 根據輸入流初始化一個DBFReader執行個體,用來讀取DBF檔案資訊  
            DBFReader reader = new DBFReader(fis);  
            // 調用DBFReader對執行個體方法得到path檔案中字段的個數  
            int fieldsCount = reader.getFieldCount();  
            // 取出字段資訊  
            for (int i = 0; i < fieldsCount; i++) {  
                DBFField field = reader.getField(i);  
                System.out.println(field.getName());  
            }  
            Object[] rowValues;  
            // 一條條取出path檔案中記錄  
            while ((rowValues = reader.nextRecord()) != null) {  
                for (int i = 0; i < rowValues.length; i++) {  
                    System.out.println(rowValues[i]);  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                fis.close();  
            } catch (Exception e) {  
            }  
        }  
    }  
      
    public static void main(String[] args) {  
        readDBF.readDBF("c:/到貨明細表.DBF");  
    }  
}  
           

2.寫DBF

public void getDbf(HttpServletResponse response) {
        String fileName = "表名.dbf";
        response.setContentType("application/x-dbf");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + CodecUtil.encode(fileName));
        try {
            // 定義DBF檔案字段
            DBFField[] fields = new DBFField[3];
            // 分别定義各個字段資訊,setFieldName和setName作用相同,
            // 隻是setFieldName已經不建議使用
            fields[0] = new DBFField();
            // fields[0].setFieldName("emp_code");
            fields[0].setName("ch_name");
            fields[0].setDataType(DBFField.FIELD_TYPE_C);
            fields[0].setFieldLength(10);
            fields[1] = new DBFField();
            // fields[1].setFieldName("emp_name");
            fields[1].setName("en_name");
            fields[1].setDataType(DBFField.FIELD_TYPE_C);
            fields[1].setFieldLength(20);
            fields[2] = new DBFField();
            // fields[2].setFieldName("salary");
            fields[2].setName("salary");
            fields[2].setDataType(DBFField.FIELD_TYPE_N);
            fields[2].setFieldLength(12);
            fields[2].setDecimalCount(2);
            // 定義DBFWriter執行個體用來寫DBF檔案
            DBFWriter writer = new DBFWriter(response.getOutputStream());
            // 把字段資訊寫入DBFWriter執行個體,即定義表結構
            writer.setFields(fields);
            // 一條條的寫入記錄
            for(int i = 0;i < 100;i ++){
                Object[] rowData = new Object[3];
                rowData[0] = "小萬";
                rowData[1] = "XiaoWan";
                rowData[2] = new Double(5000.00);
                writer.addRecord(rowData);
            }
            // 寫入資料
            writer.write();

            if(writer != null){
                writer.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
           

用excel即可打開dbf檔案