首页 > 解决方案 > 从 Excel 文件中读取并插入 Access

问题描述

我是编码新手,但遇到一个问题:我使用 Apache Poi 读取只有一列和大约 20 行的 Excel 文件。我想阅读该信息并将其插入我的 Access 数据库,但我不知道如何。

这是我的第一种方法:

Workbook readWorkbook = WorkbookFactory.create(new FileInputStream(FileChooser.FileChoose()));
Sheet sheet = readWorkbook.getSheetAt(4);
DataFormatter dataFormatter = new DataFormatter();

String cellValue = null;
for (int i = 16; i < 20; i++) {  //Statt 20 soll hier 39
    Row row = sheet.getRow(i);
    if(row == null) {
        continue;
    }
    for (int j = 2; j < 4; j++) {
        Cell cell = row.getCell(j);
        if(cell == null) {
            continue;
        }
        cellValue = dataFormatter.formatCellValue(cell);
        System.out.print(cellValue + "\n");
    }
}
return cellValue;

这是我的插入代码:

try {
    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://"+FileChooser.FileChoose());
    Statement stment = conn.createStatement();

    String query = "INSERT INTO Maschine (com_id, typ, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v) VALUES ('"+DatenBankInsert.ForEachLoop()+"')";
    stment.executeUpdate(query);

    System.out.println("successfull");
} 
catch (Exception e) {
    System.out.println("Not connected"+e);
}
return null;

标签: c#exceldatabasems-access

解决方案


推荐阅读