将包含学生信息的Excel数据导入数据库
import java.sql.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelTosql {
public static void main(String[] args) throws ClassNotFoundException, sqlException, IOException, InvalidFormatException {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:ExcelTosql.db");
Statement stat = conn.createStatement();
stat.executeUpdate( "create table stu(姓名 varchar(4),性别 char(1),学号 varchar(15),电话 varchar(12),QQ varchar(11), " +
"身份证号 char(18),出生年月 varchar(10)," + "职务 varchar(6),邮箱 varchar(20),宿舍 varchar(8),籍贯 varchar(30)," +
"家庭电话 varchar(12),备注 varchar(10));" );
File file = new File("ExcelTosql.xlsx");
FileInputStream stream = new FileInputStream(file);
Workbook book = new XSSFWorkbook(file);
Sheet sheet = book.getSheetAt(0);
String name;
String sex;
String StuID;
String TelNum;
String QQ;
String ID;
double Birthday;
String duty;
String Email;
String bedroom;
String Address;
String HomeTelNum;
String PS;
for (int i = 1; i < 61; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(5);
name = cell.getStringCellValue();
cell = row.getCell(6);
sex = cell.getStringCellValue();
cell = row.getCell(4);
cell.setCellType(CellType.STRING);//将数值型转换成字符串
StuID = cell.getStringCellValue();
cell = row.getCell(3);
cell.setCellType(CellType.STRING);//将数值型转换成字符串
TelNum = cell.getStringCellValue();
cell = row.getCell(2);
cell.setCellType(CellType.STRING);//将数值型转换成字符串
QQ = cell.getStringCellValue();
cell = row.getCell(8);
ID = cell.getStringCellValue();
cell = row.getCell(10);
Birthday = cell.getNumericCellValue();
cell = row.getCell(13);
duty = cell.getStringCellValue();
cell = row.getCell(14);
Email = cell.getStringCellValue();
cell = row.getCell(15);
bedroom = cell.getStringCellValue();
cell = row.getCell(16);
Address = cell.getStringCellValue();
cell = row.getCell(24);
cell.setCellType(CellType.STRING);//将数值型转换成字符串
HomeTelNum = cell.getStringCellValue();
cell = row.getCell(25);
PS = cell.getStringCellValue();
stat.executeUpdate("insert into stu values('" + name + "','" + sex + "','" + StuID + "','" + TelNum + "','" + QQ + "','" + ID + "','" + Birthday + "','" + duty + "','" + Email + "','" + bedroom + "','" + Address + "','" + HomeTelNum + "','" + PS + "');");
}
conn.close();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。