如何在jsp中实现数据的导入,比如将excel中的数据直接导入到mysql数据库中
展开全部
用poi读取excel中的数据然后写sql语句导入不就Ok了啊
追问
能举例说明一下嘛?这方面我从来没试过,请多多指教,如果能成功的话,追加100分都行
追答
引入一个poi的jar包
public class ImportExcel {
// 将excel数据导入到内存中
public static List importExcel() {
List stuList = new ArrayList();
try {
InputStream inp = new FileInputStream("E:\\student.xls");
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
HSSFSheet sheet = wb.getSheet("student");
if (sheet == null) {
return null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
Student student = new Student();
HSSFRow row = rit.next();
if (row.getRowNum() != 0) {
HSSFCell numCell = row.getCell((short) 0);
HSSFCell nameCell = row.getCell((short) 1);
HSSFCell ageCell = row.getCell((short) 2);
HSSFCell sexCell = row.getCell((short) 3);
HSSFCell bornCell = row.getCell((short) 4);
// 给javabean设置值
student.setId((long) numCell.getNumericCellValue());
student.setName(nameCell.getStringCellValue());
student.setAge((int) ageCell.getNumericCellValue());
student.setSex(("男"
.equals(sexCell.getStringCellValue()) ? true
: false));
student.setBirthday(sdf.parse(bornCell
.getStringCellValue()));
stuList.add(student);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return stuList;
}
public static void main(String[] args) {
List list = ImportExcel.importExcel();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t"
+ stu.getAge() + "\t\t" + stu.getSex() + "\t\t"
+ sdf.format(stu.getBirthday()));
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询