jsp和excel的数据交换-----我的项目实践

类别:Java 点击:0 评论:0 推荐:

前段时间,客户要求我们的系统可以实现与ms excel的数据交换,可以把系统中的数据导入导出到excel.我们的系统是一个用java做的b/s结构的MIS系统.为了解决这个
问题,我找到了一个专门操作excel的开源项目jxl,并找到了一篇介绍如何使用jxl开发包的好文章:
--http://www-900.cn.ibm.com/developerWorks/cn/java/l-javaExcel/#2
根据客户的要求,用jxl实现了如下功能:
1:在客户端读取excel文件的数据;
2:把系统数据导出到excel模板文件中.
对于第一个问题,我开始考虑的是写一个客户端运行的applet小程序.后来才明白,java为了安全,applet是不能随便访问客户端的文件系统的(:(低级错误!不要鄙视我).
于是就想到了先把excel文件上传到服务器,然后用jxl来解析数据.怎么上传文件我就不说了,只要做过jsp的人应该都知道吧.主要讲一下如何解析.由于上传的数据都是
有固定格式的,所以我们做了一个excel模板,客户必须按照模板的格式填写数据,程序才可以正常的解析.
解析其实也是很简单的,但在做的过程中发现,excel中的时间格式数据需要特别注意:如果按照文本格式读取时间格式数据,读取的时间是错误的(大家可以试验一下).
为了解决读取时间错误的问题,我仔细研究了一下jxl的api,呵呵,居然发现了一个好东东:DateCell类,用它的getDate方法可以得到一个cell的date对象.不过由于
国际化的需要,这个date对象得到的是格林尼治时间,你需要处理一下.
至于导出,其实和导入差不多,无非就是先访问一下数据库,把得到的信息写入excel文件然后下载.
下面是我写的导入导出周生产计划的java类,其中开始的几行注释掉的部分分别是下载和上传的试例.
(其实模板也很关键,下面的程序是基于模板而写的,不过相信大家看了下面的程序,也可以知道模板的样子了,需要重点说明的是,在模板中,填写时间的字段必须设置成时
间格式-----在excel中点击右键菜单轻松搞定)

/*
 * Create Date: 2004-11-9  10:20
 * Create By: 李春雷
 * purpose:和周计划相关的导入导出操作
 */
   
    /**********************周生产下载示例*****************
    ExcelZJHXM myZJH =  new ExcelZJHXM();
    myZJH.download("mydataZjh","20041018003");
    ****************************************************/ 
   
    /**********************周计划上传示例*****************         
        ExcelZJHXM test = new ExcelZJHXM();       
        test.upload("zscjhtest");
        if(test.getFlag()){
        System.out.println("begin test--------------");
        ArrayList a = test.getData();
        for(int i = 0;i<a.size();i++){
        ExcelZJHXM.dataZJHXM mydata =(ExcelZJHXM.dataZJHXM)a.get(i);
        System.out.println(mydata.XH+" "+mydata.XMMC+" "+mydata.GZNR +" "+mydata.SFTD +" "+mydata.TDFW +" "+mydata.JHLRFLBH +" "+mydata.JHLRBH +" "+mydata.ZXDWMC +" "+mydata.PHDWMC +" "+mydata.DBRMC +" "+mydata.JHKSSJ+" "+mydata.JHWCSJ +" "+mydata.BZ);
        
    } 
        }else{
            System.out.println("模板错误");
            }
    *****************************************************/

package com.infoearth.excel;

import com.infoearth.common.DataAccess;  //我们项目自己的访问数据库的东东
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.SQLException;
import java.sql.*;
import java.io.*;
import java.io.Serializable;
import java.util.*;
import javax.sql.RowSet;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.*;
import java.text.DateFormat;
import jxl.DateCell;
import java.text.*;

public class ExcelZJHXM
{
    private boolean flag=false;
   
    private ArrayList subdata = new ArrayList();
       
    public boolean download(String filename,String jhbh){
        Connection conn = null;
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        DataAccess dBean = new DataAccess();
        String mysql;
        mysql = "select XH,XMMC,GZNR,SFTD,TDFW,JHLRFLBH,JHLRBH,ZXDWMC,PHDWMC,DBRMC,JHKSSJ,JHWCSJ,BZ from JH_ZJHXM where JHBH='"+jhbh+"' order by jhxmbh asc";
        try {
            conn = dBean.getConnection();
            pStatement = conn.prepareStatement(mysql);
            rs = pStatement.executeQuery();
            Workbook wb=Workbook.getWorkbook(new File("pengyue\\webmis\\template\\zscjh.xls"));
            WritableWorkbook book= Workbook.createWorkbook(new File("pengyue\\webmis\\download\\"+filename+".xls"),wb);
            WritableSheet sheet = book.getSheet("zscjh");
            jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat();
         wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);           
            int i=4;
                jxl.write.DateFormat df = new jxl.write.DateFormat("yyyy-mm-dd hh:mm");
                jxl.write.WritableCellFormat wcfDF = new jxl.write.WritableCellFormat(df);
                wcfDF.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);                           
            while(rs.next()) {
                sheet.addCell(new Label(2,i,rs.getString("XH"),wcfFC));     //序号
                sheet.addCell(new Label(3,i,rs.getString("XMMC"),wcfFC));   //项目名称
                sheet.addCell(new Label(4,i,rs.getString("GZNR"),wcfFC));   //工作内容
                sheet.addCell(new Label(5,i,rs.getString("SFTD"),wcfFC));   //是否停电
                sheet.addCell(new Label(6,i,rs.getString("TDFW"),wcfFC));   //停电范围
                sheet.addCell(new Label(7,i,rs.getString("JHLRFLBH"),wcfFC));  //任务来源
                sheet.addCell(new Label(8,i,rs.getString("JHLRBH"),wcfFC));  //来源编号
                sheet.addCell(new Label(9,i,rs.getString("ZXDWMC"),wcfFC));  //执行单位
                sheet.addCell(new Label(10,i,rs.getString("PHDWMC"),wcfFC));  //配合单位
                sheet.addCell(new Label(11,i,rs.getString("DBRMC"),wcfFC));  //督办人
//              sheet.addCell(new Label(12,i,rs.getString("JHKSSJ"),wcfFC));  //开始时间
//              sheet.addCell(new Label(13,i,rs.getString("JHWCSJ"),wcfFC));  //结束时间
                if(rs.getString("JHKSSJ")!=null)sheet.addCell(new jxl.write.DateTime(12, i,DateFormat.getDateTimeInstance().parse(rs.getString("JHKSSJ")), wcfDF));
                if(rs.getString("JHWCSJ")!=null)sheet.addCell(new jxl.write.DateTime(13, i,DateFormat.getDateTimeInstance().parse(rs.getString("JHWCSJ")), wcfDF));
                sheet.addCell(new Label(14,i,rs.getString("BZ"),wcfFC));      //备注
                i++;
                }
            book.write();
            book.close();           
            rs.close();
            pStatement.close();
            return true;
        }
     
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }
        finally{
            try{
               
                if (rs!=null){
                    rs.close();
                }
               
                if (pStatement!=null){
                    pStatement.close();
                }
                if (conn!=null){
                    conn.close();
                }
            }
            catch(SQLException sqle){
                conn = null;
            }
        }
    }

    public void upload(String filename){
        String flagstr;
        try{            
            jxl.Workbook rwb = Workbook.getWorkbook(new File("pengyue\\webmis\\upload\\"+filename+".xls"));
            Sheet st = rwb.getSheet("zscjh");
            int i = 4;           //其始列-1
            flagstr = st.getCell(0,0).getContents();
            if(flagstr.equals("zscjhbegin"))flag=true;
              while(st.getCell(2,i).getContents().length()!=0&&flag==true){
                dataZJHXM mydata = new dataZJHXM();
                for(int j=2;j<st.getColumns();j++){              
                    if(st.getCell(j,0).getContents().equals("XH"))mydata.XH = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("XMMC"))mydata.XMMC = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("GZNR"))mydata.GZNR = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("SFTD"))mydata.SFTD = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("TDFW"))mydata.TDFW = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("JHLRFLBH"))mydata.JHLRFLBH = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("JHLRBH"))mydata.JHLRBH = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("ZXDWMC"))mydata.ZXDWMC = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("PHDWMC"))mydata.PHDWMC = st.getCell(j,i).getContents();
                    if(st.getCell(j,0).getContents().equals("DBRMC"))mydata.DBRMC = st.getCell(j,i).getContents();
                    //时间处理
                    if(st.getCell(j,0).getContents().equals("JHKSSJ"))mydata.JHKSSJ = FormateData(st.getCell(j,i));
                    if(st.getCell(j,0).getContents().equals("JHWCSJ"))mydata.JHWCSJ = FormateData(st.getCell(j,i));
                    if(st.getCell(j,0).getContents().equals("BZ"))mydata.BZ = st.getCell(j,i).getContents();
                    }
                    subdata.add(mydata);              
                    i++;
                }
            rwb.close();          
        }catch(Exception e){
            e.printStackTrace();
            flag = false;           
            }
       }


    //处理日期格式数据  
    public String FormateData(Cell formatecell){
        try{
            java.util.Date mydate=null;
            DateCell datecll = (DateCell)formatecell;
            mydate =datecll.getDate();
            long time=(mydate.getTime()/1000)-60*60*8;
            mydate.setTime(time*1000);
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return  formatter.format(mydate);          
            }
        catch(Exception e){
            e.printStackTrace();
            return null;
            }      
        }
  
    public boolean getFlag(){
        return flag;
        }
       
    public ArrayList getData(){
        return subdata;
        } 
         
    public static class dataZJHXM implements Serializable
    {
        public String XH;
        public String XMMC;
        public String GZNR;
        public String SFTD;
        public String TDFW;
        public String JHLRFLBH;
        public String JHLRBH;
        public String ZXDWMC;
        public String PHDWMC;
        public String DBRMC;
        public String JHKSSJ;
        public String JHWCSJ;
        public String BZ;
   
        public dataZJHXM(){
            XH = "";
            XMMC = "";
            GZNR = "";
            SFTD = "";
            TDFW = "";
            JHLRFLBH = "";
            JHLRBH = "";
            ZXDWMC = "";
            PHDWMC = "";
            DBRMC = "";
            JHKSSJ = "";
            JHWCSJ = "";
            BZ = "";                       
            }     

    }
}

本文地址:http://com.8s8s.com/it/it11945.htm