我要把客户端的一个Excel文件导入到数据库(Sql server 2000)中,是不是一定要把她先上传到服务器端然后才能导入呢?那怎样把这个Excel文件上传到服务器端呢?请各位大虾指点小弟一下~最好有实例JavaScript。谢谢~`

解决方案 »

  1.   

    利用JAVA EXCEL APIimport java.io.*;
    import java.util.*;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionForm;
    import org.apache.struts.upload.FormFile;
    import org.apache.struts.upload.MultipartRequestHandler;import com.neusoft.idc.common.Constants;
    import com.roger.util.StringUtil;import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;/** *//**
     * <p>Title: </p>
     * <pre>Description: Excel数据导入到oracle数据库类.
     * </pre>
     * <p>Company: </p>
     * @author 
     * @version 1.0
     */
    public class ExcelImportOracle { /** *//**
      * 用于返回三维数组的ArrayList.
      */
     private static ArrayList subdata = new ArrayList();
     
     /** *//**
      * Excel中的表名.
      */
     private static String tablename;
     
     /** *//**
      * 文件的路径
      */
     private static String filePath;
     
     public static ArrayList uploadFile(ActionForm form,
             HttpServletRequest request, String filePath)
     {
         ArrayList fileInfos = new ArrayList();
         String xwlwPath = Constants.ATTATCHMENT_ROOTPATH + filePath
                 + File.separator;
         // 取得存放文件的物理路径
         File xwlwDir = new File(xwlwPath);
         if (!xwlwDir.exists()) // 判断是否存在目录,不存在则创建目录
         {
             xwlwDir.mkdirs();
         }     MultipartRequestHandler multipartRequestHandler = form
                 .getMultipartRequestHandler();
         // 取得所有上传文件的对象集合
         Hashtable elements = multipartRequestHandler.getFileElements();     // 循环遍历每一个文件
         Collection values = elements.values();
         int k = 0;
         for (java.util.Iterator i = values.iterator(); i.hasNext();)
         {
             FormFile file = (org.apache.struts.upload.FormFile) i.next();
             // 取得上传的文件
             // 判断文件是不是空,如果是空文件则忽略
             if (file == null || file.getFileSize() == 0
                     || file.getFileName().equals(""))
             {
                 continue;
             }
             k++;
             // 构造文件的全路径包括文件名称
             String fileName = file.getFileName();
             int index = fileName.lastIndexOf(".");
             String suffix = "";
             if (index != -1)
             {
                 suffix = fileName.substring(index);
             }
             String rename = "111111111" + k + suffix;
             String fileFullPath = xwlwPath + rename;         // 调用保存文件的方法
             saveFile(file, fileFullPath);         String[] fileInfo = new String[] { fileFullPath, fileName };
             fileInfos.add(fileInfo);
         }     return fileInfos;
     } /**
      * <p>
      * Discription:文件上传
      * </p>
      */
     public static void saveFile(FormFile file, String fileFullPath)
     {
         try
         {
             InputStream stream = file.getInputStream(); // 把文件读入
             OutputStream bos = new FileOutputStream(fileFullPath);
             // 建立一个上传文件的输出流         // 开始读取文件
             int bytesRead = 0;
             byte[] buffer = new byte[8192];
             while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
             {
                 bos.write(buffer, 0, bytesRead); // 将文件写入服务器
             }
             bos.close();
             stream.close();
         }
         catch (IOException e)
         {
             
         }
     }
     
     /** *//**
      * 该方法为完成读取Excel中的数据并将数据插入到对应的数据库表中的操作(在调用前需要先调用setFilePath(String)这个方法.).
      * @author Administrator
      * @param data:读取Excel中的数据的数组.    
      * @deprecated:将读取Excel中的数据插入到对应的数据库表中.  * 
      */
     public static void ExcelDataImportOracle(String filePath) throws Exception {
      try {
       ArrayList al = readExcel(filePath);
       InsertData(al);   
      } catch (Exception e) {
       e.printStackTrace();
      }
     } /** *//**
      * 读取Excel中的数据.将这些数据放入到一个三维数组中.
      * @author Administrator
      * @param filePath 文件路径.
      * @deprecated:读取Excel中的数据将它放入到ArrayList数组中(此为三维数组).
      */
     public static ArrayList readExcel(String filePath) ...{
      try {
       subdata.clear();//将静态ArrayList数组清空.(如果不清空原数据会不断累加)
       InputStream is = new FileInputStream(filePath);
       Workbook rwb = Workbook.getWorkbook(is);
       // Sheet st = rwb.getSheet(0);//这里有两种方法获取sheet表,1为名字,而为下标,从0开始
       // Sheet st = rwb.getSheet("Book1");// Excel中第一页的页名称.
       Sheet st[] = rwb.getSheets();// 得到所有Excel中页的列表.
       for (int a = 0; a < st.length; a++) {
        ArrayList alList = new ArrayList();
        ArrayList tablenames = new ArrayList();
        ArrayList tableAndContents = new ArrayList();
        tablename = st[a].getName().trim();
        int b = 0;
        for (int i = 1; i < st[a].getRows(); i++) {
         ArrayList al = new ArrayList();
         for (int j = 0; j < st[a].getColumns(); j++) {
          Cell c00 = st[a].getCell(j, i);
          // 通用的获取cell值的方式,返回字符串
          String strc00 = StringUtil.toISO(c00.getContents().trim());
          // 获得cell具体类型值的方式得到内容.
          al.add(j, strc00);
         }
         alList.add(b, al);
         b++;
        }
        tablenames.add(tablename);
        tableAndContents.add(0, tablenames);
        tableAndContents.add(1, alList);
        subdata.add(a, tableAndContents);
       }
       rwb.close();
       // 关闭
       //System.out.println(subdata);// 输出
      } catch (Exception e) {
       e.printStackTrace();
      }
      finally
      {
       //删除上传文件
       File file = new File(filePath);
       boolean result = false;
       
       if(file.exists())
       {
        result = file.delete();
       }
       else
       {
        System.out.println("文件没有找到,无法删除!");
       }
       
       if(result)
       {
        System.out.println("删除成功!");
       }
       else
       {
        System.out.println("删除失败!");
       }
      }
      return subdata;
     } /** *//**
      * 将读取的Excel的三维数组数据进行对应的数据库表插入操作.
      * @author Administrator
      * @param data:读取Excel中的数据的数组.
      * @deprecated:将读取Excel中的数据插入到对应的数据库表中.
      */
     private static void InsertData(ArrayList data) {
      try {
       String tablename;
       ArrayList Contents = new ArrayList();
       for (int i=0; i<data.size(); i++){
        tablename = ((ArrayList)((ArrayList)data.get(i)).get(0)).get(0).toString();
        Contents = (ArrayList)((ArrayList)data.get(i)).get(1);
        WfdefineDateOperate.currecorde(tablename,Contents);
       }
      } catch (Exception e) {
       e.printStackTrace();
      }
     } /** *//**
      * 得到文件路径;
      * @return filePath
      */
     public String getFilePath() {
      return filePath;
     }
     /** *//**
      * 设置文件路径的位置;
      * @param filePath
      */
     public void setFilePath(String filePath) {
      ExcelImportOracle.filePath = filePath;
     }
     
     /** *//**
      * 将readExcel方法读出来的三维数组数据转换成二维数组数据.
      * @param al
      * @return 返回二维数组数据.
      */
     public static ArrayList getDisplayData(ArrayList al)
     {
      ArrayList result = new ArrayList();
      for(int i = 0;i < al.size();i ++)
      {
       ArrayList tmp = (ArrayList)((ArrayList)al.get(i)).get(1);
       
       for(int j = 0;j < tmp.size();j ++)
       {
        result.add(tmp.get(j));
       }
      }
      
      return result;
     } /** *//**
      * @param args
      */
     public static void main(String[] args) {  
     }}
      

  2.   

    需要jxl.jar,可以自己去网上下载,上面的例子包括了上传和插入数据库。
    在界面上用<input type="file" name="excelfile">然后POST提交就行。
      

  3.   

    com.neusoft.idc.common.Constants
    这个类从那儿下载
      

  4.   

    我有个ASP的处理实例,去年冬天写的。
    ASP的是最麻烦的,还得自己弄上传功能,服务器端要用DCOM里面的excel处理程序。
    需要的可发站短。