发布网友 发布时间:2022-04-19 10:50
共2个回答
懂视网 时间:2022-04-08 15:26
不管是做软件还是做网站,相信很多人在做的时候都要用到数据库,而数据库的数据从何而来呢,可以使手动添加的,但是大多数情况下我们使用的是已有的数据,我们想借助开发的工具管理目前已有的数据,如果是小量的数据,手动录入也无妨,但是对于大量数据呢,手动录入显然已经不显示,而且,更多的时候,我们呢想在软件上增加一个数据的批量导入,这样不管是谁在用这款软件,都可以方便的管理已有数据,那么我们最常用的数据管理器是什么呢,毫无疑问Excel,但是我们做软件是几乎没有人会使用Excel作为DB,所以我们遇到的问题就是如何在软件中实现数据批量导入,数据源是Excel,有人会说用ODBC啊,可以导入到任何地方,但是这种做法好像没有什么技术含量,而且让用户没次批量导入的时候都设置一个ODBC数据源是不现实的,今天博主将给大家介绍用春java实现读取Excel数据文件放入Access数据库(各个数据库的原理是一样的),我们用到的是一个开源API,jxl,代码如下:
public static boolean readExcel(String filePath){
boolean success=false;
Workbook rwb = null;
try{
InputStream is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
// 获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
for (int k = 1; k < rs.getRows(); k++){
String major=rs.getCell(0, k).getContents();
String year=rs.getCell(1, k).getContents();
String class1=rs.getCell(2, k).getContents();
String number1=rs.getCell(3, k).getContents();
String name=rs.getCell(4, k).getContents();
String sex=rs.getCell(5, k).getContents();
String people=rs.getCell(6, k).getContents();
String area=rs.getCell(7, k).getContents();
String birth=rs.getCell(8, k).getContents();
String bodyid=rs.getCell(9, k).getContents();
String phone=rs.getCell(10, k).getContents();
String hostel=rs.getCell(11, k).getContents();
String father=rs.getCell(12, k).getContents();
String mather=rs.getCell(13, k).getContents();
String tel=rs.getCell(14, k).getContents();
String studentstate=rs.getCell(15, k).getContents();
String level1=rs.getCell(16, k).getContents();
String party=rs.getCell(17, k).getContents();
String duties=rs.getCell(18, k).getContents();
String english=rs.getCell(19, k).getContents();
String computer=rs.getCell(20, k).getContents();
String address=rs.getCell(21, k).getContents();
String personstation=rs.getCell(22, k).getContents();
String fundingstation=rs.getCell(23, k).getContents();
String awardstation=rs.getCell(24, k).getContents();
String punishmentstation=rs.getCell(25, k).getContents();
String other1=rs.getCell(26, k).getContents();
String picturepath="";
if(major.equals("")||class1.equals("")||name.equals("")||number1.equals("")){
success=false;
break;
}else{
String str1="insert into information(major,year,class1,number1,name,sex,people,area,birth,bodyid,phone,hostel,father,mather,tel,studentstate,level1,party,duties,english,computer,address,personstation,fundingstation,awardstation,punishmentstation,other1,picturepath)values(‘"+major+"‘,‘"+year+"‘,‘"+class1+"‘,‘"+number1+"‘,‘"+name+"‘,‘"+sex+"‘,‘"+people+"‘,‘"+area+"‘,‘"+birth+"‘,‘"+bodyid+"‘,‘"+phone+"‘,‘"+hostel+"‘,‘"+father+"‘,‘"+mather+"‘,‘"+tel+"‘,‘"+studentstate+"‘,‘"+level1+"‘,‘"+party+"‘,‘"+duties+"‘,‘"+english+"‘,‘"+computer+"‘,‘"+address+"‘,‘"+personstation+"‘,‘"+fundingstation+"‘,‘"+awardstation+"‘,‘"+punishmentstation+"‘,‘"+other1+"‘,‘"+picturepath+"‘)";
CommonCode.addInfo(str1);
CommonCode.closeAll();
success=true;
}
}
rwb.close();
}catch (Exception e){
e.printStackTrace();
}
return success;
}
用java实现excel数据批量导入数据库
标签:
热心网友 时间:2022-04-08 12:34
java使用第三方工具包POI技术实现excel数据的批量导入导出。
举例如下:
1、下载apache的相关jar包。poi-ooxml-3.6.jar xmlbeans-2.3.0.jar等,如图:
2、编写相关的读写类
/**
* 读取xls文件内容
*/
private
List<XlsDto> readXls() throws
IOException {
InputStream is = new
FileInputStream("test.xls");
HSSFWorkbook hssfWorkbook = new
HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new
ArrayList<XlsDto>();
// 循环工作表Sheet
for
(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if
(hssfSheet == null) {
continue;
}
// 循环行Row
for
(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if
(hssfRow == null) {
continue;
}
xlsDto = new
XlsDto();
// 循环列Cell
// 0学号 1姓名 2学院 3课程名 4 成绩
// for (int cellNum = 0; cellNum <=4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if
(xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if
(xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if
(yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if
(kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if
(cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return
list;
}
3、导出就是输入到一个新的excel文件里面
public void writeXls(List<Student> list, String path) throws Exception {
if (list == null) {原始数据为空,直接返回
return;
}
int countColumnNum = list.size();//设置列数
HSSFWorkbook book = new HSSFWorkbook(); //创建工作表对象
HSSFSheet sheet = book.createSheet("studentSheet");
// 创建第一行
HSSFRow firstRow = sheet.createRow(0);
HSSFCell[] firstCells = new HSSFCell[countColumnNum];
//创建表头
String[] options = { "no", "name", "age", "score" };
//循环数据域
for (int j = 0; j < options.length; j++) {
firstCells[j] = firstRow.createCell(j);
firstCells[j].setCellValue(new HSSFRichTextString(options[j]));
}
//处理每一个cell的值
for (int i = 0; i < countColumnNum; i++) {
HSSFRow row = sheet.createRow(i + 1);
Student student = list.get(i);
for (int column = 0; column < options.length; column++) {
HSSFCell no = row.createCell(0);
HSSFCell name = row.createCell(1);
HSSFCell age = row.createCell(2);
HSSFCell score = row.createCell(3);
no.setCellValue(student.getNo());
name.setCellValue(student.getName());
age.setCellValue(student.getAge());
score.setCellValue(student.getScore());
}
}
File file = new File(path);
OutputStream os = new FileOutputStream(file);
System.out.println(Common.WRITE_DATA + path);
book.write(os);
os.close();
}