发布网友 发布时间:2022-04-09 23:50
共2个回答
热心网友 时间:2022-04-10 01:19
用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()));
}
}
}
热心网友 时间:2022-04-10 02:37
给一个按钮选择excel 文件,将这个文件上传到服务器上去...(fileup)网上找找文件上传下载