文件的导入
jsp:
<s:form action="import.action" method="post" enctype="multipart/form-data" theme="simple"> <table border="1"> <tr> <td>文件上传</td> </tr> <tr> <td> <s:file name="uploadFile" label="文件位置" size="80"/> </td> </tr> <tr><td> <s:submit value="submit"></s:submit> <s:reset value="reset"></s:reset> </td></tr> </table> </s:form>
package excl;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.commons.io.FileUtils;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.struts2.ServletActionContext;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.Transaction;import test.BaseAction;import test.User;public class Import extends BaseAction{private static final String SUCCESS = "success";private File uploadFile=null;private String uploadFileFileName=null;public String loadRoleFile() {String directory="/upload/role";String targetDirectory=ServletActionContext.getServletContext().getRealPath(directory);String file=targetDirectory+"\"+uploadFileFileName;//生成上传的文件对象File target = new File(file);//如果文件已经存在,则删除源文件if(target.exists()){target.delete();}System.out.println(uploadFile);System.out.println(uploadFileFileName);//复制file对象,实现上传try {FileUtils.copyFile(uploadFile,target);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} 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++){User user = new User();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 : user.setId(Integer.parseInt(cellValue));break; case 1 : user.setPassword(cellValue);break; case 2 : user.setUsername(cellValue);break; }}roleList.add(user);}} catch (IOException e) {e.printStackTrace();}Session session = this.getHibernateTemplate().getSessionFactory().openSession();Transaction tx = null;try {tx=null;tx=session.beginTransaction();if(roleList.size()>0){int roleNum=roleList.size();for(int i=0;i<roleNum;i++){session.save(roleList.get(i));}tx.commit();}} catch (HibernateException e) {tx.rollback();e.printStackTrace();}finally{session.close();}return SUCCESS;}public String getUploadFileFileName() {return uploadFileFileName;}public void setUploadFileFileName(String uploadFileFileName) {this.uploadFileFileName = uploadFileFileName;}public File getUploadFile() {return uploadFile;}public void setUploadFile(File uploadFile) {this.uploadFile = uploadFile;}}