大家好,请问怎么把一个二进制的文件转换成excel文件,把一个excel文件转换成二进制文件。

解决方案 »

  1.   

    可用一般的fread,fwrite,fopen或FileCreate,FileRead,FileWrite即可。   
      先读出Excel中的内容,然后写文件。
      

  2.   

    excel一般是一个字段记录位置,一个字段记录长度,一个字段记录内容,把这些转换成二进制,最后一起生成一个文件即可.
      

  3.   

    private void insertIt() throws FileNotFoundException,java.sql.SQLException {

    java.sql.Connection con = null;
    java.sql.PreparedStatement pstmt = null;
    String SQL = null;
            FileInputStream fin=null;
            SQL="INSERT INTO dbo.CASE_ATTACHMENT (HELPDESK_NO, SEQ_NO, FILE_NAME,FILE_CONTENT, CREATED_BY, CREATED_DATE ) VALUES (?,  ?, ?, ?, ?, ? )";
    try {
                File f = new File(ContentFileName);
                fin = new FileInputStream(f);
                int len = (int)f.length();            con = getCon();

    int id=1;
    pstmt = con.prepareStatement(SQL);

    pstmt.setString(id++, HelpdeskNO);
                SeqNO=getNextSeqNO();
                pstmt.setInt(id++, SeqNO);
    pstmt.setString(id++, f.getName());
                
                pstmt.setBinaryStream(id++, fin,len);            pstmt.setString(id++, CreatedBy);

    if(CreatedDate!=null)
    pstmt.setTimestamp(id++, CreatedDate.getSQLTimestamp());
    else
    pstmt.setTimestamp(id++, null)
    ;


    int iAffected = pstmt.executeUpdate();
    //to do
    commit();
    setIsNew(false);
    }
    catch (SQLException sqle) {
    try {
    //if you need..
    rollback();
    }
    catch (Exception e) {}
    System.err.println("Error : " + sqle);
    sqle.printStackTrace();
    throw sqle;
    }
    finally {
    try {pstmt.close();} catch (Exception e) {}
    try {closeCon();} catch (Exception e) {}
                try{     fin.close();}catch(Exception e){}
            }
    }此Function 是将文件"ContentFileName" 以二进制存入数据库
      

  4.   

    将二进制数据从数据库读出 产生excel文件                //masterObject=home.getCase(helpDeskNo);
                    int seq=hr.getInt("seq");
                    String fileName=hr.getString("filename");//                CaseAttachmentHome attachHome= HomeFactory.getCaseAttachmentHome(con);
    //                CaseAttachment attachment=attachHome.getCaseAttachment(helpDeskNo,seq);
    //                if(!attachment.getFileName().equals(fileName))return;                    java.sql.PreparedStatement pstmt = null;
                        OutputStream   fileOut=null;
                        String SQL = null;
                        Pageable rs = null;                    SQL="SELECT FILE_CONTENT FROM dbo.CASE_ATTACHMENT WHERE HELPDESK_NO=? and SEQ_NO=?  ";
                        try {
                            int id=1;
                            pstmt = con.prepareStatement(SQL);                        pstmt.setString(id++, helpDeskNo);
                            pstmt.setInt(id++, seq);                        rs = (Pageable) pstmt.executeQuery();                        if(rs.getRowsCount()==0) throw new CaseAttachmentNotFoundException(this);                        rs.first();
                            InputStream   fc   =   rs.getBinaryStream("FILE_CONTENT");
                            log.info("size="+fc.available());
                            response.reset();
                            response.setContentType("application/x-download");
                            response.addHeader(
                                "Content-Disposition",
                                "attachment; filename=" + fileName);
                            byte[] b = new byte[fc.available()];
                            int   rec   =0;
    //                        OutputStream sout   =   new   FileOutputStream(fileName);
                            ServletOutputStream   sout   =   response.getOutputStream();
    //                       w fc.read(b,0,b.length);
    //                       w sout.write(b);
                            while ((rec =  fc.read(b)) > 0) {
                                sout.write(b,0,rec);
                            }
                            
    //                       w while(fc.read(b)!=-1){
    //                       w     fileOut.write(b);
    //                       w }
    //                        fileOut.close();
                            sout.close();
                            fc.close();
                            rs.close();//                        fileOut   =   new   FileOutputStream(fileName);
    //                        byte[]   idata   =   new   byte[8000];
    //
    //                        response.setCharacterEncoding("utf-8");
    //                        response.setContentType("application/x-download");
    //                        response.addHeader("Accept-Ranges", "bytes");
    //                        response.addHeader("Accept-Length", "" + fc.available());
    //                        response.addHeader(
    //                            "Content-Disposition",
    //                            "attachment; filename=" + fileName);
    //
    //                        int   rec   =0;
    //                        while ((rec =  fc.read(idata)) > 0) {
    //                                fileOut.write(idata,0,rec);
    //                            }
    ////                        while(fc.read(idata)!=-1){
    ////                        fileOut.write(idata);
    ////                        }
    //
    //                        idata   =   null;
    //                        fileOut.flush();
    //                        fileOut.close();
    //                        fc.close();
                        }                    catch (SQLException sqle) {
                            //to do
                            System.err.println("Error : " + sqle);
                            sqle.printStackTrace();
                            throw sqle;
                        }
                        finally {
                            try {rs.close();} catch (Exception e) {}
                            try {pstmt.close();} catch (Exception e) {}
                            try {fileOut.close();} catch (Exception e) {}
                }