乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>struts2</em> <em>导入excel</em>到数据

<em>struts2</em> <em>导入excel</em>到数据

作者:乔山办公网日期:

返回目录:excel表格制作


String path_ = ServletActionContext.getRequest().getSession()
zd.getServletContext().getRealPath("/")+"相对路";

这是一个小例子,使用的jexcel插件,你需要下载相应的jar


用apache的poi.jar,非常简单。先下载这个包,我最近也在做这个功能。上传的话可以用jspsmartupload.jar包,这个比那个common-fileupload包更好用,封装多了。

/**
 * 通过filePath参数,找到对应excel文件e799bee5baa6e59b9ee7ad94338,读取表中数据放入List<String[]>集合中
 * @param filePath
 * @return
 */
private List<String[]> getExcel(String filePath) {
List<String[]> list = new ArrayList<String[]>();
String[] arr = null;
try {
// 把一张xls的数据表读到wb里
//HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File("F:/test.xls")));
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(filePath)));
// 读取第一页,这里获取第一个工作表来进行操作
HSSFSheet sheet = wb.getSheetAt(0);
// 循环遍历表,sheet.getLastRowNum()是获取一个表最后一条记录的记录号
for (int j = 1; j <= sheet.getLastRowNum(); j++) {
// 创建一个行对象
HSSFRow row = sheet.getRow(j);
int l = row.getLastCellNum();
arr = new String[l + 1];
// 把一行里的每一个字段遍历出来
for (int i = 0; i < l; i++) {
if(i == 5 || i == 6){
arr [i] = String.valueOf((long)row.getCell((short) i ).getNumericCellValue());
}else{
// 创建一个行里的一个字段的对象,也就是获取到的一个单元格中的值
HSSFCell cell = row.getCell((short) i);
arr[i] = cell.getRichStringCellValue().toString();
}
}
list.add(arr);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}

/**
 * 从getExcel()方法中得到要插入的各行,将其插入到数据库学生基本信息表中
 * 一旦出现错误,则跳出插入循环体,返回插入的List<StudentBean>集合
 * @param filePath
 * @return
 */
public List<StudentBean> initStuInfo(String filePath) {
boolean sign = false;
String[] arr = null;
String sql = "";
StudentBean sb = null;
List<String[]> list = new ArrayList<String[]>();
List<StudentBean> insertList = new ArrayList<StudentBean>();
filePath = ""/*----------Excel文件路径---------------*/;
list = this.getExcel(filePath);
for (int i = 0; i < list.size(); i++) {
arr = list.get(i);
sb = new StudentBean();
sb.setStuID(arr[0]);
sb.setStuName(arr[1]);
sb.setSex(arr[2]);
sql = "insert into dormitory.`stu_info`(stu_id,stu_name,sex,college,major,grade,class_id,birthday,nation,idnumber,addr,status)values("
+ "'"+ sb.getStuID() + "','" + sb.getStuName( )+ "','"+ sb.getSex() + "')";
sign = conn.getExecuteUpdate(sql);
if(!sign){
break;
}
insertList.add(sb);
}
return insertList;
}

相关阅读

  • <em>Java</em>利用<em>POI导入excel</em>表格

  • 乔山办公网excel表格制作
  • 参考答案   4.如果现在这个时代能出全才,那便是应试教育的幸运和这个时代的不幸。如果有,他便是人中之王,可惜没有,所以我们只好把“全”字人下的“王”给拿掉。时代需要
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网