Java项⽬之数据与表格的导⼊代码开发开发思路
代码开发
/*
*导⼊表格
*/
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import com.ts.dao.SchoolMessageDao;
import com.ts.dao.TeacherMessageDao;
import com.ts.dao.TitleMessageDao;
import com.ts.dao.UserMessageDao;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class ExcelTest {
Workbook workbook = null;
Sheet sheet = null;
//初始化对象
SchoolMessageDao schoolMessageDao =new SchoolMessageDao();
UserMessageDao userMessageDao =new UserMessageDao();
TeacherMessageDao  teacherMessageDao =new TeacherMessageDao();
public void importExcel(){
try{
// Excel⽂件
File file =new File("excel/import.xls");
// 创建workbook
workbook = Workbook(file);
// 获取sheet页
sheet = Sheet(0);
// 获取sheet⾏数
int rows = Rows();
// 遍历sheet,读取⼀条⼀条数据,存储在pojo对象中
String user = null;
String passward = null;
String sex=null;
String name=null;
String livel=null;
String sum=null;
String score=null;
String logtime=null;
String sql = null;
String sql1 = null;
String sql2 = null;
for(int i =1; i < rows; i++){
// 直接获取单元格
Cell userCell = Cell(0, i);
Cell passwardCell = Cell(1, i);
Cell sexCell = Cell(2, i);
Cell nameCell = Cell(3, i);
Cell livelCell = Cell(4, i);
Cell sumCell = Cell(5, i);
Cell sumCell = Cell(5, i);
Cell scoreCell = Cell(6, i);
Cell logtimeCell = Cell(7, i);
// 获取单元格中的内容
user = Contents();
passward = Contents();
sex = Contents();
name = Contents();
livel = Contents();
sum = Contents();
score = Contents();
Contents();
sql ="insert into login values(?,?)";
sql1 ="insert into User values(?,?,?,?,?)";
resultset 遍历sql2 ="insert into user_game valu"
+"es(?,?,?,?,?)";
SchoolMessageDao.saveUserInfo(sql, user, passward);
TeacherMessageDao.saveUserInfo(sql1, user, passward,name,sex,logtime);    UserMessageDao.saveUserInfo(sql2, user, name,sum,livel,score);
}
// 将读取到的数据存⼊数据库
}catch(Exception e){
}finally{
JOptionPane.showMessageDialog(null,"导⼊成功");
workbook.close();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。