欢迎来到天天文库
浏览记录
ID:57687760
大小:19.50 KB
页数:2页
时间:2020-09-01
《excel中读取数据并且保存到数据库中.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1、添加POIjar包到项目的lib目录下2、Excel文件目录:d://excel.xls3、数据库字段为:num1num2num3num4num5num64、数据库名:blog5、表名:test6、编写类:连接mysql的字符串方法、插入的方法、实体类importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.apache.commons.logging.Log;
2、importorg.apache.commons.logging.LogFactory;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;publicclassTestExcel{//记录类的输出
3、信息staticLoglog=LogFactory.getLog(TestExcel.class);//获取Excel文档的路径publicstaticStringfilePath="D://excel.xls";publicstaticvoidmain(String[]args){try{//创建对Excel工作簿文件的引用HSSFWorkbookwookbook=newHSSFWorkbook(newFileInputStream(filePath));//在Excel文档中,第一张工作表的缺省索引是0/
4、/其语句为:HSSFSheetsheet=workbook.getSheetAt(0);HSSFSheetsheet=wookbook.getSheet("Sheet1");//获取到Excel文件中的所有行数introws=sheet.getPhysicalNumberOfRows();//遍历行for(inti=0;i5、row.getPhysicalNumberOfCells();Stringvalue="";//遍历列for(intj=0;j6、";break;caseHSSFCell.CELL_TYPE_STRING:value+=cell.getStringCellValue()+",";break;default:value+="0";break;}}}//将数据插入到mysql数据库中String[]val=value.split(",");TestEntityentity=newTestEntity();entity.setNum1(val[0]);entity.setNum2(val[1]);entity.setNum3(val[2]);7、entity.setNum4(val[3]);entity.setNum5(val[4]);entity.setNum6(val[5]);TestMethodmethod=newTestMethod();method.Add(entity);}}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}
5、row.getPhysicalNumberOfCells();Stringvalue="";//遍历列for(intj=0;j6、";break;caseHSSFCell.CELL_TYPE_STRING:value+=cell.getStringCellValue()+",";break;default:value+="0";break;}}}//将数据插入到mysql数据库中String[]val=value.split(",");TestEntityentity=newTestEntity();entity.setNum1(val[0]);entity.setNum2(val[1]);entity.setNum3(val[2]);7、entity.setNum4(val[3]);entity.setNum5(val[4]);entity.setNum6(val[5]);TestMethodmethod=newTestMethod();method.Add(entity);}}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}
6、";break;caseHSSFCell.CELL_TYPE_STRING:value+=cell.getStringCellValue()+",";break;default:value+="0";break;}}}//将数据插入到mysql数据库中String[]val=value.split(",");TestEntityentity=newTestEntity();entity.setNum1(val[0]);entity.setNum2(val[1]);entity.setNum3(val[2]);
7、entity.setNum4(val[3]);entity.setNum5(val[4]);entity.setNum6(val[5]);TestMethodmethod=newTestMethod();method.Add(entity);}}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}
此文档下载收益归作者所有