日期:2014-05-16  浏览次数:20458 次

上传excel文件并在服务器端读取存入数据库
页面jsp文件
<form name="add" action="/gxtWeb/lvjcontacts/manyContacts.action" method="post" enctype="multipart/form-data" onsubmit="return checkNull();"> 
  		 <s:file name="upload"></s:file>
  		<input type="submit" value="导入信息" >
  	</form> 
action处理:
public class ManyContactsAction extends ActionSupport {
private File upload;
	private String uploadContentType;// 要上传的文件的类型
	private String uploadFileName;// 要上传的文件
	private ContactsManager manager;
	private Contacts cbean;
	private Persons pbean;
	private Long gid;
	private String result;
public String execute() throws Exception {
		// 检查后缀名是否符合条件,同时更改上传文件的文件名
		int filesize = this.getUploadFileName().length();
		String fileEx = this.getUploadFileName().substring(
				this.getUploadFileName().indexOf("."), filesize);
		//获取文件名
		String fileName=uploadFileName.substring(0,uploadFileName.indexOf("."));
		// 获得上传路径
		String realPath = ServletActionContext.getServletContext().getRealPath(
				"/UploadFile/");
		File saveFile=null;
		if (upload != null) {
			// 修改文件名,使上传后不至于重复替代
			// this.uploadFileName = new Date().getTime() + fileEx;
			saveFile = new File(new File(realPath), uploadFileName);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}

			FileUtils.copyFile(upload, saveFile);// 到这里,文件已上传成功
			// 下面进行判断文件是否是rar文件,是就需要解压
			if (fileEx.equals(".rar")) {
				System.out.println("saveFile:" + saveFile);//rar文件所在保存路径
				System.out.println("realPath:" + realPath);//解压后保存路径
				// 定义解压字符串,用于解压上传的rar文件,注意此处需要一个unrar.exe文件
				String rarpath = ServletActionContext.getServletContext()
						.getRealPath("/rarFile/UNRAR.exe x -t -o+ -p- \"");
				String jieya = rarpath + saveFile + "\" \"" + realPath + "\"";
				Process p1 = Runtime.getRuntime().exec(jieya);// 将传输的rar文件解压
				p1.waitFor();
				p1.destroy();
				FileUtils.deleteQuietly(saveFile);// 删除rar文件
				saveFile=new File(new File(realPath),fileName+".xls");
				System.out.println("解压后:"+saveFile);
				ServletActionContext.getResponse().getWriter().println(
						"success!!");
			}
			if (fileEx.equals(".xls") || fileEx.equals(".xlsx")) {
				// 开始读取文件了,获得第一列手机号码
				Workbook persons = Workbook.getWorkbook(saveFile);// 获得xls文件
				Sheet sheet = persons.getSheet(0);// 获得第一个工作簿
				System.out.println("列数:" + sheet.getColumns());
				int count = sheet.getRows();// 取得记录数,count行
				String cphone;
				// 遍历行,获得列数据
				for (int i = 0; i < count; i++) {
					cphone = sheet.getCell(0, i).getContents();// 第一列的所有行
					pbean = manager.getPerson(cphone);// 获得该用户,查询别的信息
					cbean = new Contacts();
					Long contactsid = pbean.getId();
					Long pid = 10002L;
					cbean.setContactsid(contactsid);// 联系人id
					cbean.setPid(pid);// 用户本身的PID
					cbean.setCid("cid");// 关系的学校ID
					cbean.setGid(gid);// 分组id
					manager.addPerson(cbean);
					this.contactsLog.writeLog("10002", "批量添加联系人", "批量添加联系人操作", "");
					System.out.println("添加成功!");
				}
			}
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
}

xml配置:
<action name="manyContacts" class="manyContactAction">
			<result name="success" type="redirectAction">personInfo.action
			</result>
			<result name="input" type="redirectAction">showmangpersons.action
			</result>
</action>
注意:在文件解压时,需要一个unrar.exe文件,这个文件应保存在工程相应的目录下,我就放在webapps里面了,
按照上面的路径即可获得解压文件并进行解压。
  这里配置文件没写任何限制,拦截器之类,都在action里面进行判断了,这里只做了类型判断,没有别的。