如何将 excel 数据导入 mysql?
How to import excel data to mysql?
我有一个包含许多字段的 excel sheet。我有一个包含许多 table 的数据库,这些 table 相互链接。 excel sheet 包含的字段被插入到多个 table 中,这些 table 相互链接。
例如 sample.xls 或 .xlsx
id | Name | Number | Designation
1 | manan | 987654321 | software eng.
数据库:个人 table
id | Name | Number | Desig_Id
1 | manan | 987654321 | 1
数据库:名称table
Desig_id | Designation Name
1 | software eng.
这只是一个示例,我同时链接了许多 table。像这样的数据要怎么导入?
编码本身有点复杂,但如果你想要一个简单的答案;您可以使用 JDBC and you can interact with an Excel file from Java using Apache POI (their how-to 页面与 Java 中的数据库进行交互非常有用)。
您可以将所有记录分成多个 table,就像它们在您的数据库中一样。
您将为每个 table 创建一个 excel 文件并使用 mysql-excel mechanism. You can also export the excel files as .csv
and import it directly either by using MySQL Workbench or by command prompt
导入它
如果您不想规范化 excel 文件,另一种方法是解析文件并以编程方式导入它。您可以使用apache-poi读取excel文件,也可以将excel文件导出为csv文件并解析为文本文件。
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class App {
public static void main(String[] args) {
Workbook wb = null;
InputStream inp = null;
try {
// InputStream inp = new FileInputStream("workbook.xls");
inp = new FileInputStream("workbook.xlsx");
wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.println(getCellValue(cell));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if(wb != null) wb.close(); } catch (Exception e) {}
try { if(inp != null) inp.close(); } catch (Exception e) {}
}
}
private static Object getCellValue(Cell cell) {
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING :
return cell.getStringCellValue();
case Cell.CELL_TYPE_NUMERIC :
return cell.getNumericCellValue();
case Cell.CELL_TYPE_BOOLEAN :
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_ERROR :
return cell.getErrorCellValue();
case Cell.CELL_TYPE_FORMULA :
return cell.getCellFormula();
case Cell.CELL_TYPE_BLANK :
return null;
default :
return null;
}
}
}
excel 示例 (xlsx)
id Name Number Designation
1 manan_1 9876543211 software eng._1
2 manan_2 9876543212 software eng._2
3 manan_3 9876543213 software eng._3
依赖项(Maven)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.12</version>
</dependency>
我有一个包含许多字段的 excel sheet。我有一个包含许多 table 的数据库,这些 table 相互链接。 excel sheet 包含的字段被插入到多个 table 中,这些 table 相互链接。
例如 sample.xls 或 .xlsx
id | Name | Number | Designation
1 | manan | 987654321 | software eng.
数据库:个人 table
id | Name | Number | Desig_Id
1 | manan | 987654321 | 1
数据库:名称table
Desig_id | Designation Name
1 | software eng.
这只是一个示例,我同时链接了许多 table。像这样的数据要怎么导入?
编码本身有点复杂,但如果你想要一个简单的答案;您可以使用 JDBC and you can interact with an Excel file from Java using Apache POI (their how-to 页面与 Java 中的数据库进行交互非常有用)。
您可以将所有记录分成多个 table,就像它们在您的数据库中一样。
您将为每个 table 创建一个 excel 文件并使用 mysql-excel mechanism. You can also export the excel files as .csv
and import it directly either by using MySQL Workbench or by command prompt
如果您不想规范化 excel 文件,另一种方法是解析文件并以编程方式导入它。您可以使用apache-poi读取excel文件,也可以将excel文件导出为csv文件并解析为文本文件。
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class App {
public static void main(String[] args) {
Workbook wb = null;
InputStream inp = null;
try {
// InputStream inp = new FileInputStream("workbook.xls");
inp = new FileInputStream("workbook.xlsx");
wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
System.out.println(getCellValue(cell));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if(wb != null) wb.close(); } catch (Exception e) {}
try { if(inp != null) inp.close(); } catch (Exception e) {}
}
}
private static Object getCellValue(Cell cell) {
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING :
return cell.getStringCellValue();
case Cell.CELL_TYPE_NUMERIC :
return cell.getNumericCellValue();
case Cell.CELL_TYPE_BOOLEAN :
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_ERROR :
return cell.getErrorCellValue();
case Cell.CELL_TYPE_FORMULA :
return cell.getCellFormula();
case Cell.CELL_TYPE_BLANK :
return null;
default :
return null;
}
}
}
excel 示例 (xlsx)
id Name Number Designation
1 manan_1 9876543211 software eng._1
2 manan_2 9876543212 software eng._2
3 manan_3 9876543213 software eng._3
依赖项(Maven)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.12</version>
</dependency>