jxl在操作Excel文件时,要求文件需要保存在服务器端
可以先用SmartUpload将文件上传至服务器,再进行操作
下面是个例子
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*,jxl.*,ConnDatabase.SDatabase"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ include file="../public_js/yonghuhuodong.jsp" %>
<script language="javascript" src="../public_js/public.js"></script>
<LINK href="../class/<%=session.getAttribute("FactColorC") %>" rel=stylesheet>
<script language=javascript src="../sale/js/mad.js"> </script>
<jsp:useBean id="ViewBean" scope="page" class="ConnDatabase.SDatabase"/>
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String trace = "";
String fileNames = request.getParameter("fname");
//out.println(fileNames);
//String fsections = request.getParameter("fsection");
if(fileNames!=null && !fileNames.equals("")){
try{
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
// 建立上传目录
//java.io.File fileMkDir = new java.io.File(strSavePath);
//fileMkDir.mkdirs();
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
String myFileName=myFile.getFileName();
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath("/")+"jsp\\upload\\";
trace=aa+myFileName;
//System.out.println(trace);
//取得别的参数
String explain=(String)mySmartUpload.getRequest().getParameter("files");
String send=(String)mySmartUpload.getRequest().getParameter("SaveEPrice");
//将文件保存在服务器端
myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL);
}
}
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
//System.out.println("-----------------");
java.io.File f= new java.io.File(trace);
f=f.getCanonicalFile();
InputStream is = new FileInputStream(f);
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0);
int rsrows=rs.getRows();
int allRec=rs.getRows();///得到该sheet的行数
int row = rs.getColumns(); //得到列数
// out.print("行:"+allRec+"--列:"+row);
// Cell c1=rs.getCell(0,1);
// String s=c1.getContents();
// out.println(s);
//List list=new ArrayList();
String s1="";
String s2="";
String s3="";
String s4="";
String s5="";
String s6="";
String s7="";
String s8="";
String s9="";
String custNo="";
try{
for(int i=2;i<allRec;i++){
for(int j=1;j<row;j++){
// c.setSection(fsections); /
//s1=fsections;
Cell c1=rs.getCell(1,i);
Cell c2=rs.getCell(2,i);
Cell c3=rs.getCell(3,i);
Cell c4=rs.getCell(4,i);
Cell c5=rs.getCell(5,i);
Cell c6=rs.getCell(6,i);
Cell c7=rs.getCell(7,i);
Cell c8=rs.getCell(8,i);
//custNo=getMaxSequrence(fsections);
s2=c1.getContents();
s3=c2.getContents();
s4=c3.getContents();
s5=c4.getContents();
s6=c5.getContents();
s7=c6.getContents();
s8=c7.getContents();
s9=c8.getContents();
/*c.setCustName(c1.getContents());
c.setCus_Sex(c2.getContents());
c.setRoomNo(c3.getContents());
c.setRoomNo(c4.getContents());
c.setAddress(c5.getContents());
c.setCardNo(c6.getContents());
c.setPhone(c7.getContents());
c.setRemark(c8.getContents());*/
// String fff=c1.getContents();
}
}
}
out.println("<script language='javascript'>alert('成功添加');window.opener.location.reload();window.close();</script>");
}catch(Exception e){
out.println("<script language='javascript'>alert('您提供的文件不正确!!!');window.close();</script>");
}
%>
posted on 2006-10-11 18:29
robbin163 阅读(576)
评论(0) 编辑 收藏