Java生成DBF文件的工具类
import com.linuxense.javadbf.DBF;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFRecord;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
public class DBFGenerator {
public static void createDBFFile(String filePath, Map<String, String> fields, List<Map<String, Object>> data,
String dateFormat, String nullValue) throws IOException {
checkDuplicateFields(fields);
DBF dbf = new DBF();
for (Map.Entry<String, String> field : fields.entrySet()) {
DBFField dbfField = new DBFField();
dbfField.setName(field.getKey());
switch (field.getValue().toUpperCase()) {
case "C":
dbfField.setType(DBFField.FIELD_TYPE_C);
dbfField.setLength(50);
break;
case "N":
dbfField.setType(DBFField.FIELD_TYPE_N);
dbfField.setLength(20);
break;
case "D":
dbfField.setType(DBFField.FIELD_TYPE_D);
break;
case "L":
dbfField.setType(DBFField.FIELD_TYPE_L);
break;
case "F":
dbfField.setType(DBFField.FIELD_TYPE_F);
dbfField.setLength(20);
break;
default:
throw new IllegalArgumentException("不支持的字段类型: " + field.getValue());
}
dbf.addField(dbfField);
}
for (Map<String, Object> row : data) {
DBFRecord record = new DBFRecord(dbf);
int index = 0;
for (String fieldName : fields.keySet()) {
Object value = row.get(fieldName);
if (value == null) {
value = nullValue;
}
setFieldValue(record, index, value, fields.get(fieldName), dateFormat);
index++;
}
dbf.addRecord(record);
}
dbf.write(new File(filePath));
System.out.println("DBF 文件创建成功: " + filePath);
}
private static void setFieldValue(DBFRecord record, int index, Object value, String fieldType, String dateFormat) {
try {
if (value instanceof String) {
record.setString(index, (String) value);
} else if (value instanceof Integer) {
record.setInt(index, (Integer) value);
} else if (value instanceof Double) {
record.setDouble(index, (Double) value);
} else if (value instanceof Boolean) {
record.setBoolean(index, (Boolean) value);
} else if (value instanceof java.util.Date && "D".equalsIgnoreCase(fieldType)) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
record.setDate(index, sdf.format((java.util.Date) value));
} else {
throw new IllegalArgumentException("不支持的字段值类型: " + value.getClass());
}
} catch (Exception e) {
throw new RuntimeException("字段值设置失败: " + e.getMessage(), e);
}
}
private static void checkDuplicateFields(Map<String, String> fields) {
if (fields.size() != fields.keySet().size()) {
throw new IllegalArgumentException("字段名不能重复");
}
}
public static void main(String[] args) {
try {
Map<String, String> fields = Map.of(
"NAME", "C",
"AGE", "N",
"BIRTHDAY", "D",
"IS_ACTIVE", "L",
"SALARY", "F"
);
List<Map<String, Object>> data = List.of(
Map.of("NAME", "Alice", "AGE", 30, "BIRTHDAY", new java.util.Date(), "IS_ACTIVE", true, "SALARY", 5000.75),
Map.of("NAME", "Bob", "AGE", 25, "BIRTHDAY", new java.util.Date(), "IS_ACTIVE", false, "SALARY", 4500.50)
);
createDBFFile("output.dbf", fields, data, "yyyy-MM-dd", "NULL");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
public class ExcelReader {
public static List<Map<String, Object>> readExcel(String filePath) throws IOException {
List<Map<String, Object>> resultList = new ArrayList<>();
FileInputStream fis = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
for (int rowIndex = 1; rowIndex <= sheet.getPhysicalNumberOfRows(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null) {
Map<String, Object> map = new HashMap<>();
for (int colIndex = 0; colIndex < row.getPhysicalNumberOfCells(); colIndex += 2) {
Cell studentIdCell = row.getCell(colIndex);
Cell nameCell = row.getCell(colIndex + 1);
if (studentIdCell != null && nameCell != null) {
map.put("学号", studentIdCell.getStringCellValue());
map.put("姓名", nameCell.getStringCellValue());
}
}
resultList.add(map);
}
}
workbook.close();
fis.close();
return resultList;
}
public static void main(String[] args) {
try {
List<Map<String, Object>> result = readExcel("students.xlsx");
for (Map<String, Object> map : result) {
System.out.println(map);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}