Struts2实现Excel导入

除Struts2必须的jar包外,还需要jar包:poi-3.7-beta-20100620.jar

首先是jsp代码实现上传:
<body>
<s:form id="form1" name="form1" action="roleFileUpload.action" method="post" enctype="multipart/form-data" theme="simple">
<table width="80%" border="1" align="center" class="DB_table">
<tr>
<td colspan="99" align="left">文件上传</td>
</tr>
<tr>
<td colspan="99" id="more">
<s:file name="uploadFile" label="文件位置" size="80"/>
</td>
</tr>
<tr>
<td colspan="99" align="right">
<s:submit value="上传"></s:submit>
<s:reset value="重置"></s:reset>
</td>
</tr>
</table>
[/align]
</s:form>
</body>

其次是Struts.xml配置文件:
<action name="roleFileUpload" class="roleFileAction" method="loadRoleFile">
<result name="success">/page/role/roleallinput.jsp</result>
<result name="input">/page/role/roleallinput.jsp</result>
</action>

然后是Action类实现:
public class RoleFileUploadAction extends ActionSupport{

private File uploadFile;
private String uploadFileFileName;
private FileLoadDao fileLoadDao;
private RoleDao roleDao;

public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}

public FileLoadDao getFileLoadDao() {
return fileLoadDao;
}
public void setFileLoadDao(FileLoadDao fileLoadDao) {
this.fileLoadDao = fileLoadDao;
}

public RoleDao getRoleDao() {
return roleDao;
}
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
@Override
public void validate() {
super.validate();
}

public String loadRoleFile(){

String directory = "/upload";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
//生成上传的文件对象
File target = new File(targetDirectory,uploadFileFileName);
//如果文件已经存在,则删除原有文件
if(target.exists()){
target.delete();
}
//复制file对象,实现上传
try {
FileUtils.copyFile(uploadFile, target);

} catch (IOException e) {
e.printStackTrace();
}
loadRoleInfo();
return SUCCESS;
}

/**
* 把Excele表读出的数据,组装成一个List,统一导入数据库
* @param uploadFileFileName
*/
public void loadRoleInfo(){

String directory = "/upload";
String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);
File target = new File(targetDirectory,uploadFileFileName);
List roleList = new ArrayList();
try{
FileInputStream fi = new FileInputStream(target);
Workbook wb = new HSSFWorkbook(fi);
Sheet sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum()+1;
for(int i=1;i<rowNum;i++){
PtRoleInfo ptRoleInfo = new PtRoleInfo();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for(int j=0;j<cellNum;j++){
Cell cell = row.getCell(j);
String cellValue = null;
switch(cell.getCellType()){ //判断excel单元格内容的格式,并对其进行转换,以便插入数据库
case 0 : cellValue = String.valueOf((int)cell.getNumericCellValue()); break;
case 1 : cellValue = cell.getStringCellValue(); break;
case 2 : cellValue = String.valueOf(cell.getDateCellValue()); break;
case 3 : cellValue = ""; break;
case 4 : cellValue = String.valueOf(cell.getBooleanCellValue()); break;
case 5 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
}

switch(j){//通过列数来判断对应插如的字段
case 0 : ptRoleInfo.setRoleId(cellValue);break;
case 1 : ptRoleInfo.setRoleName(cellValue);break;
case 2 : ptRoleInfo.setDeil(cellValue);break;
}
}
roleList.add(ptRoleInfo);
}
for(PtRoleInfo pt:roleList){
fileLoadDao.roleInfotoDB(pt);

}
}catch(IOException e){
e.printStackTrace();
}
}

}
最后在roleInfoToDB方法中需要使用‘事务Transaction’来进行批量插入,确保插入失败的能完全回滚。这样Excel文件的读取以及数据库的写入就实现了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值