@[TOC]
文章所用jar文件
用到的jar
建议使用,版本冲突已经解决
DBF文件操作工具类--DbfWriterAndReadUtil
package com.yuanzhan.dbf.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFWriter;
public class DbfWriterAndReadUtil {
/**
* 创建dbf
*
* @param path
* 文件路径
* @param fieldList
* 字段
* @param charsetName
* 编码字符集
* @throws IOException
*/
public static void createDbf(String path,
List<Map<String, String>> fieldList, String charsetName)
throws IOException {
// 创建dbf文件列对象--根据传入集合长度确定该数组长度
DBFField[] fields = new DBFField[fieldList.size()];
int index = 0;
// 循环当前列数组,写入每行数据
for (Map<String, String> fieldMap : fieldList) {
// 创建dbf文件行数据对象
DBFField field = new DBFField();
// 获取键值--即表头
field.setName(fieldMap.get("name"));// 字段名称
// 设置该列数据类型
field.setType(DBFDataType.CHARACTER);// 指定字段类型为字符串
// 指定该列数据长度
field.setLength(Integer.valueOf(fieldMap.get("length")));// 指定长度
// 写入到列数组中
fields[index] = field;
index++;
}
// 定义DBFWriter实例用来写DBF文件
DBFWriter dbfWriter = new DBFWriter(new FileOutputStream(path),
Charset.forName(charsetName));
// 设置字段
dbfWriter.setFields(fields);
// 写入dbf文件并关闭
dbfWriter.close();
}
/**
* 获取字段名--表头名
*
* @param path
* 文件路径
* @param charsetName
* 编码类型
* @return
* @throws IOException
*/
public static String[] getFieldName(String path, String charsetName)
throws IOException {
// 读取文件数据对象
DBFReader dbfReader = new DBFReader(new FileInputStream(path),
Charset.forName(charsetName));
int fieldCount = dbfReader.getFieldCount();// 获取字段数量
// 创建数组
String[] fieldName = new String[fieldCount];
// 循环写入数据值
for (int i = 0; i < fieldCount; i++) {
// 获取表头名--写入数组
fieldName[i] = dbfReader.getField(i).getName();
}
// 关闭资源
dbfReader.close();
return fieldName;
}
/**
* 写dbf文件
*
* @param dbfName
* 文件名
* @param strutName
* 列名集合
* @param strutType
* 单元格数据类型集合
* @param strutLength
* 单元格数据长度
* @param data
* 传入的行数据--双重数组
*/
public static void generateDbfFromArray(String dbfName, String[] strutName,
byte[] strutType, int[] strutLength, Object[][] data) {
// io输出流
OutputStream fos = null;
try {
//列名数组长度--即列数
int fieldCount = strutName.length;
//创建单行数据
DBFField[] fields = new DBFField[fieldCount];
//设置单行数据单个数据列名,数据类型,数据长度
for (int i = 0; i < fieldCount; i++) {
//实例化单行数据
fields[i] = new DBFField();
//列名
fields[i].setName(strutName[i]);
//数据类型
fields[i].setDataType(strutType[i]);
//数据长度
fields[i].setFieldLength(strutLength[i]);
}
//写入流对象
DBFWriter writer = new DBFWriter();
//写入
writer.setFields(fields);
//循环单行写入
for (int i = 0; i < fieldCount - 1; i++) {
writer.addRecord(data[i]);
}
//输出流实例
fos = new FileOutputStream(dbfName);
//输出文件
writer.write(fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (Exception e) {
}
}
}
/**
* 读dbf文件
* @param path 文件路径
* @param charsetName 编码
* @return
* @throws IOException
*/
public static List<Map<String, String>> readDbf(String path,
String charsetName) throws IOException {
//存储读取到类容的集合
List<Map<String, String>> rowList = new ArrayList<Map<String, String>>();
//创建文件输入流
DBFReader dbfReader = new DBFReader(new FileInputStream(path),
Charset.forName(charsetName));
//声明单行数据数组
Object[] rowValues;
//循环读取
while ((rowValues = dbfReader.nextRecord()) != null) {
//键值方式存储单行数据
Map<String, String> rowMap = new HashMap<String, String>();
//循环写入当行数据到map集合
for (int i = 0; i < rowValues.length; i++) {
//key 列名 value 当前单元格值
rowMap.put(dbfReader.getField(i).getName(),
String.valueOf(rowValues[i]).trim());
}
//写入集合
rowList.add(rowMap);
}
//关闭输入流
dbfReader.close();
return rowList;
}
}
Excel文件操作工具类--
package com.yuanzhan.dbf.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWriterAndReadUtil {
/**
* 读取excel文件--返回dbf文件格式
* @param fileName 读取的文件路径
* @return
* @throws Exception
*/
public static List<Map<String,String>> readExcel(String fileName) throws Exception {
//打开需要读取的文件
FileInputStream inputStream = new FileInputStream(new File(fileName));
//读取工作簿
XSSFWorkbook wordBook = new XSSFWorkbook(inputStream);
//读取工作表,从0开始
XSSFSheet sheet = wordBook.getSheetAt(0);
//存储数据集合
List<Map<String,String>> mapList=new ArrayList<Map<String,String>>();
//存储表头数据
Map<Integer,String > stringMap=new HashMap<Integer, String>();
int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数
//循环按行读取数据
for(int i=0;i<=lastRowNum;i++){
//单个数据集合--保持和dbf文件格式相同--方便对比
Map<String,String> map=new HashMap<String,String>();
//读取第i行
XSSFRow row = sheet.getRow(i);
int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数
//循环读取单行数据
for(int j=0;j<columnNum;j++){
//读取j单元格
XSSFCell cell = row.getCell(j);//获取单元格对象
//设置cell数据类型
cell.setCellType(CellType.STRING);
//获取值
String value = cell.getStringCellValue();
//第一行是存储表头数据
if (i==0){
//单元格值
stringMap.put(j,value);
}else{
//否则存储表头--值数据
map.put(stringMap.get(j),value);
}
}
if (i!=0){
//存入集合
mapList.add(map);
}
}
//关闭输入流
inputStream.close();
//关闭工作簿
wordBook.close();
return mapList;
}
/**
* 写入工作簿文件
* @throws Exception
*/
public void writeExcel() throws Exception {
//创建工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//创建工作表
XSSFSheet sheet = workbook.createSheet();
//创建行
XSSFRow row = sheet.createRow(2);
//创建单元格,操作第三行第三列
XSSFCell cell = row.createCell(2,CellType.STRING);
cell.setCellValue("hellword");
//路径
FileOutputStream outputStream = new FileOutputStream(new File("D:/bb.xlsx"));
workbook.write(outputStream);
//关闭工作簿
workbook.close();
}
}
调用工具类示例
/**
* 项目名:DbfExcelDemo
* 日 期:2019/11/11
* 包 名:com.yuanzhan.dbf.main
*
* @author: liujia
*/
package com.yuanzhan.dbf.main;
import com.linuxense.javadbf.DBFField;
import com.yuanzhan.dbf.util.DbfWriterAndReadUtil;
import com.yuanzhan.dbf.util.ExcelWriterAndReadUtil;
import java.util.List;
import java.util.Map;
/**
* 对比,实例调用
* @author admin
*
*/
public class DemoMain {
public static Object FileConversion(String dbfFile, String excelFile,
String dowFile) {
List<Map<String, String>> mapList = null;
try {
// 读取Excel文件
mapList = ExcelWriterAndReadUtil.readExcel(excelFile);
} catch (Exception e) {
e.printStackTrace();
}
// 读取dbf文件
// 获取文件内容集合
List<Map<String, String>> getRowList = null;
// 写入的列名数组
String[] strutName = null;
// 单元格数据类型,与上面列名一一对应
byte[] strutType = null;
// 单元格数据长度
int[] strutLength = null;
// 获取dbf文件的列名集合
String[] fieldName = new String[] {};
try {
// 获取dbf文件的列名集合
fieldName = DbfWriterAndReadUtil.getFieldName(dbfFile, "GBK");
// 实例化数组
strutName = new String[fieldName.length];
// 实例化数组
strutType = new byte[fieldName.length];
//实例化数组
strutLength = new int[fieldName.length];
//设置数据列名,数据类型,数据长度
for (int i = 0; i < fieldName.length; i++) {
strutName[i] = fieldName[i];
strutType[i] = DBFField.FIELD_TYPE_C;
strutLength[i] = 50;
}
// 获取文件内容集合
getRowList = DbfWriterAndReadUtil.readDbf(dbfFile, "GBK");
// 多行数据集合
Object[][] data = new Object[getRowList.size()][];
int count = 0;
// 对比,,dbf文件数据
for (Map<String, String> map1 : getRowList) {
// 单行数据集合
String[] text = new String[fieldName.length];
//根据列名获取两边文件的单元格数据
//excel数据
for (Map<String, String> map2 : mapList) {
//根据name和address检索数据
if (map1.get("name").equals(map2.get("name"))
&& map1.get("address").equals(map2.get("address"))) {
//对比cardId数据
if (map1.get("cardId").equals(map2.get("cardId"))) {
//true,返还dbf原数据
for (int i = 0; i < strutName.length; i++) {
text[i] = map1.get(strutName[i]);
}
//结束循环
continue;
} else {
//false,返回Excel数据
for (int i = 0; i < strutName.length; i++) {
//获取表头--对比
if (strutName[i].equals("cardId")) {
text[i] = map2.get(strutName[i]);
} else {
text[i] = map1.get(strutName[i]);
}
}
continue;
}
}
}
//存储单行数据
data[count] = text;
count++;
}
// 写入新的dbf文件
DbfWriterAndReadUtil.generateDbfFromArray(dowFile, strutName,
strutType, strutLength, data);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
本Demo开发环境jdk1.7,开发工具:MyEclipse Professional 2014