我是这样弄的,
1:提供一个浏览按钮,用于打开本地的excel文件。通过上传按钮把excel文件上传到该工程下的目录下2:然后读取工程目录下的excel文件,这样对不?InputStream IS = new FileInputStream("D:\\Tomcat 6.0\\webapps\\SomeFileUpLoad\\someImages\\policy.xls");
jxl.Workbook reb = Workbook.getWorkbook(IS);我这样读取的路径是一个绝对的,可以不?我没弄过,不知道大家都怎么弄的,说说思路就好了。谢谢
1:提供一个浏览按钮,用于打开本地的excel文件。通过上传按钮把excel文件上传到该工程下的目录下2:然后读取工程目录下的excel文件,这样对不?InputStream IS = new FileInputStream("D:\\Tomcat 6.0\\webapps\\SomeFileUpLoad\\someImages\\policy.xls");
jxl.Workbook reb = Workbook.getWorkbook(IS);我这样读取的路径是一个绝对的,可以不?我没弄过,不知道大家都怎么弄的,说说思路就好了。谢谢
//setter and getter ...然后方法里面是:
File file = new FileInputStream(theInFile);
Workbook book = Workbook.getWorkbook(file);
jsp页面对应的是:<input type="file" name="theInFile" />
Workbook wb = null;
try {
// 获取工作簿对象
wb = Workbook.getWorkbook(file);
if (wb != null) {
// 获取工作簿对象就可以获取工作簿内的工作表对象
Sheet[] sheets = wb.getSheets();
if (sheets != null && sheets.length != 0) {
// 遍历工作簿内所有工作表
for (int i=0;i<sheets.length;i++) {
// 获取该工作表内的行数
int rows = sheets[i].getRows();
// 遍历行
System.out.println("总行数是:"+rows);
// 获取当前行的所有单元格
for (int j=0;j<rows;j++) {
//读出表头
if(j==0){
//读出第一行记录
StringBuffer sbu=new StringBuffer();
Cell[] cells = sheets[i].getRow(j);
if (cells != null && cells.length != 0) {
// 遍历单元格
//存储每行
for (int k=0;k<cells.length;k++) {
// 获取当前单元格的值
String cell = cells[k].getContents();
// 缩进
sbu.append(cell+",");
}
Excel ec=new Excel();
//System.out.println("判断后的值为:"+ec.isNeedExcel(sbu.toString()));
if( ec.isNeedExcel(sbu.toString()) ==false){
// System.out.println("-----"+sbu.toString());
return "表格格式不匹配!";
}
}
}
if(j>0){
//读出每行记录
StringBuffer sbu=new StringBuffer();
Cell[] cells = sheets[i].getRow(j);
if (cells != null && cells.length != 0) {
// 遍历单元格
//存储每行
for (int k=0;k<cells.length;k++) {
// 获取当前单元格的值
String cell = cells[k].getContents();
// 缩进
sb.append(cell + "\t");
sbu.append(cell+","+"\t");
}
sb.append("\t\n");
sbu.append("\t\n");
Excel ec=new Excel();
ec.handleStu(sbu.toString(),rollinate); System.out.println("---------------5");
System.out.println("导入的内容为:"+sbu.toString());
}
}
}
sb.append("\t\n");
}
}
System.out.println("成功读取了:" + file + "\n");
String updoadFilePath=this.getInitParameter("updoadFilePath");
System.out.println("-------"+updoadFilePath);
//是否上传成功
boolean state = false;
// 用来引用上传成功的文件,以给客户端输出
java.util.List<File> upokFile=new java.util.ArrayList<File>();
try {
// 创建一个基于磁盘文件的工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(updoadFilePath));
factory.setSizeThreshold(10);
// 创建一个文件处理器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析请求
List items = upload.parseRequest(request);
// 处理文件上传项
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
// 普通请求,可以接收请求中其它参数
} else {
//文件上传请求
// 获得上传文件名
String fileName = item.getName();
System.out.println("文件全名为: "+fileName);
fileName=fileName.substring(fileName.lastIndexOf("\\"));
temf= new File(updoadFilePath+ fileName);
System.out.println("保存的路径为: "+temf.getAbsolutePath());
//将文件写入服务器上的目录
item.write(temf);
upokFile.add(temf);
state= true;
}
}
} catch (Exception ef) {
ef.printStackTrace();
}
response.setContentType("text/html; charset=GBK");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>欢迎光临netjava 文件上传</title>");
out.println("</head>");
if(state){
out.println("<h1><font color='blue' size='20'>感谢您的支持!</font></h1>");
// out.println("<h2><font color='red'>文件上传成功: </font></h2>");
// //将文件链结打印到客户端供其下载
// while(upokFile.size()>0){
// File upFile=upokFile.remove(0);
// String paht=upFile.getParentFile().getName() +"/"+upFile.getName();
// out.println("<br><a href=\""+paht+"\">"+upFile.getName()+"</a>");
// }
// out.println("</dir>");
// out.println("</body>");
// out.println("</html>");
// out.flush();
// out.close();
Excel ec=new Excel();
//入学年份
String rollinate=request.getParameter("rollinate");
System.out.println("roolinate is:"+rollinate.substring(rollinate.length()-2));
//读出数据 插入到数据库中
String msg=ec.importExcel(temf.getAbsolutePath(),rollinate);