通过jsp向mysql批量导入数据_如何在jsp中实现数据的导入,比如将excel中的数据直接导入到mysql数据库中...

引入一个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()));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值