home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / bb98.exe / SOexcel.java < prev    next >
Text File  |  2002-11-09  |  8KB  |  281 lines

  1. //
  2. // The contents of this file are subject to the BadBlue End User License
  3. // Agreement (the "EULA"); you may not use this file except in
  4. // compliance with the EULA.  You may obtain a copy of the EULA at
  5. // http://badblue.com/down.htm .
  6. //
  7. // Software distributed under the EULA is distributed on an "AS IS" basis,
  8. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the EULA
  9. // for the specific language governing rights and limitations under the
  10. // EULA.
  11. //
  12. // The Initial Developer of this code under the EULA is Working Resources,
  13. // Inc., Atlanta, GA  UNITED STATES.  All Rights Reserved.
  14. //
  15.  
  16. //
  17. package ShareOffice;
  18.  
  19. //
  20. import java.net.*;
  21. import java.io.*;
  22. import ShareOffice.HTTPGet;
  23. import ShareOffice.SOinit;
  24.  
  25. //
  26. public class SOexcel extends SOinit {
  27.     //
  28.     public SOexcel() {
  29.     }
  30.     //    Retrieve Excel data over specified range and fill in 
  31.     //        array with values.
  32.     //    Inputs:
  33.     //        sAddr: address of BadBlue server (e.g., "127.0.0.1:8080")
  34.     //        sPath: path of shared file in EXT.INI file (e.g., "path3")
  35.     //        sFile: name of Excel file to examine (e.g., "invoice.xls")
  36.     //        nSheet: sheet number (e.g., 1)
  37.     //        sCellStart: starting cell of area to retrieve (e.g., "A1")
  38.     //        sCellEnd: ending cell of area to retrieve (e.g., "G99")
  39.     //        sUser: (optional) user-name to get access to file
  40.     //        sPassword: (optional) password to get access to file
  41.     //    Outputs:
  42.     //        sData[][]: two-dimensional array returned with data
  43.     //            (if error occurs, array will be 1x1 and contain an
  44.     //             error message)
  45.     //
  46.     public String[][] GetExcelData(
  47.         String        sAddr,
  48.         String        sPath,
  49.         String        sFile,
  50.         int            nSheet,
  51.         String        sCellStart,
  52.         String        sCellEnd,
  53.         String        sUser,
  54.         String        sPassword
  55.     ) {
  56.         String[][]    aasSheet;
  57.         String        sError = "";
  58.         try {
  59.  
  60.             //    General setup.
  61.             //
  62.             int nX, nY;
  63.             int nDX = 25;
  64.             int nDY = 50;
  65.             int[] iaStart = Cell2RowCol(sCellStart);
  66.             int[] iaEnd   = Cell2RowCol(sCellEnd);
  67.             int i, j, nCursor, nCursor2, nTemp;
  68.             String sTemp, sVal;
  69.             aasSheet = new String[iaEnd[1]-iaStart[1]+nDY][iaEnd[0]-iaStart[0]+nDX];
  70.  
  71.             // Debugging info.
  72.             //
  73.             // System.out.println("DY: "+Integer.toString(iaEnd[1]-iaStart[1]+1) + " " +
  74.             //       "DX: "+Integer.toString(iaEnd[0]-iaStart[0]+1) + "\r\n");
  75.             // System.out.println("iaS" + Integer.toString(iaStart[0]) + " " + Integer.toString(iaStart[1]) + "\r\n");
  76.             // System.out.println("iaE" + Integer.toString(iaEnd[0]) + " " + Integer.toString(iaEnd[1]) + "\r\n");
  77.  
  78.             //    Loop through by chunks until done.
  79.             //
  80.             for (nY = iaStart[1]; nY <= iaEnd[1]; nY += nDY) {
  81.                 for (nX = iaStart[0]; nX <= iaEnd[0]; nX += nDX) {
  82.  
  83.                     //    Construct the URL and read it.
  84.                     //
  85.                     String sURL = 
  86.                         "http://"+sAddr+"/ext.dll?MfcISAPICommand=LoadPage&"+
  87.                         "page=xls.htx&a0=/get/"+sPath+"/"+URLEncoder.encode(sFile, "UTF-8")+
  88.                         "&a1="+Integer.toString(nSheet)+"&a2=_&"+
  89.                         "a3="+Integer.toString(nX)+"&a4="+Integer.toString(nY)+
  90.                         "&a5="+Integer.toString(nDX)+"&a6="+Integer.toString(nDY)+
  91.                         "&a7=2&a8=100%25";
  92.                     HTTPGet h = new HTTPGet();
  93.                     String sPage;
  94.                     if (sUser.length() > 0) {
  95.                         sPage = h.Read(sURL, sUser, sPassword);
  96.                     } else {
  97.                         sPage = h.Read(sURL);
  98.                     }
  99.                     // System.out.println("---\r\n"+sPage+"\r\n---\r\n");
  100.  
  101.                     //    Rip through multiple rows of data...
  102.                     //
  103.                     for (i = 0; i < nDY  &&  (nY + i) <= iaEnd[1]; i++) {
  104.                         for (j = 0; j < nDX  &&  (nX + j) <= iaEnd[0]; j++) {
  105.                             //
  106.                             sTemp = "<TD class=fXL";
  107.                             if ((nCursor = sPage.indexOf(sTemp)) < 0) {
  108.                                 sError = "Invalid template file (2)";
  109.                                 break;
  110.                             }
  111.                             nCursor += sTemp.length();
  112.                             if ((nCursor = sPage.indexOf(">", nCursor)) < 0) {
  113.                                 sError = "Invalid template file (3)";
  114.                                 break;
  115.                             }
  116.                             nCursor++;
  117.                             //
  118.                             if (nCursor + 255  >  (nCursor2 = sPage.length())) {
  119.                                 nCursor2--;
  120.                             } else {
  121.                                 nCursor2 = nCursor + 255;
  122.                             }
  123.                             sVal = sPage.substring(nCursor, nCursor2);
  124.                             if ((nCursor2 = sVal.indexOf("<")) >= 0) {
  125.                                 sVal = sVal.substring(0, nCursor2);
  126.                             }
  127.                             //
  128.                             sPage = sPage.substring(nCursor + nCursor2);
  129.                             nTemp = nY + i;
  130.                             aasSheet[nTemp][nX + j] = sVal;
  131.                             //
  132.                         }
  133.                         if (sError.length() > 0) {
  134.                             break;
  135.                         }
  136.                         sTemp = "</TR";
  137.                         if ((nCursor = sPage.indexOf(sTemp)) < 0) {
  138.                             sError = "Invalid template file (6)";
  139.                             break;
  140.                         }
  141.                         sPage = sPage.substring(nCursor + sTemp.length());
  142.                     }
  143.                     if (sError.length() > 0) {
  144.                         break;
  145.                     }
  146.                 }
  147.                 if (sError.length() > 0) {
  148.                     break;
  149.                 }
  150.             }
  151.             if (sError.length() > 0) {
  152.                 aasSheet = new String[1][1];
  153.                 aasSheet[0][0] = "Error: " + sError;
  154.             }
  155.             return (aasSheet);
  156.  
  157.         //    ...end SP.
  158.         //
  159.         } catch (StringIndexOutOfBoundsException e) {
  160.             aasSheet = new String[1][1];
  161.             aasSheet[0][0] = "Error: string out of bounds: " + e.getMessage();
  162.             return (aasSheet);
  163.  
  164.         //    ...end SP.
  165.         //
  166.         } catch (Exception e) {
  167.             aasSheet = new String[1][1];
  168.             aasSheet[0][0] = "Error: no data available: " + e.getMessage();
  169.             return (aasSheet);
  170.         }
  171.     }
  172.  
  173.     //
  174.     //    Update a cell in an Excel shared workbook
  175.     //    Inputs:
  176.     //        sAddr: address of BadBlue server (e.g., "127.0.0.1:8080")
  177.     //        sPath: path of shared file in EXT.INI file (e.g., "path3")
  178.     //        sFile: name of Excel file to examine (e.g., "invoice.xls")
  179.     //        nSheet: sheet number (e.g., 1)
  180.     //        sUpdateCell: cell to update (e.g., "A1")
  181.     //        sUpdateValue: new value for update cell
  182.     //        sUpdateType: data type of cell being updated (default is "S")
  183.     //            I2 = short-int   I4 = long int    R4 = real     R8 = float
  184.     //            C  = currency    D  = date        B  = boolean  E  = empty
  185.     //            S  = string 
  186.     //        sUser: (optional) user-name to get access to file
  187.     //        sPassword: (optional) password to get access to file
  188.     //    Outputs:
  189.     //        errmsg: empty if no error occurred, otherwise error message
  190.     //
  191.     public String UpdateExcelData(
  192.         String        sAddr,
  193.         String        sPath,
  194.         String        sFile,
  195.         int            nSheet,
  196.         String        sUpdateCell,
  197.         String        sUpdateValue,
  198.         String        sUpdateType,
  199.         String        sUser,
  200.         String        sPassword
  201.     ) {
  202.         String        sError = "";
  203.         try {
  204.  
  205.             //    Set defaults.
  206.             //
  207.             if (sUpdateType.length() == 0) {
  208.                 sUpdateType = "s";
  209.             }
  210.             int nX = 0; int nY = 0;
  211.             int nDX = 9; int nDY = 9;
  212.  
  213.             //    Construct the URL and read it.
  214.             //
  215.             String sURL = 
  216.                 "http://"+sAddr+"/ext.dll?MfcISAPICommand=LoadPage&"+
  217.                 "page=xls.htx&a0=/get/"+sPath+"/"+URLEncoder.encode(sFile, "UTF-8")+
  218.                 "&a1="+Integer.toString(nSheet)+"&a2=Update&"+
  219.                 "a3="+Integer.toString(nX)+"&a4="+Integer.toString(nY)+
  220.                 "&a5="+Integer.toString(nDX)+"&a6="+Integer.toString(nDY)+
  221.                 "&a7=2&a8=100%25&a10="+sUpdateCell+
  222.                 "&a9="+URLEncoder.encode(sUpdateValue, "UTF-8")+"/"+sUpdateType;
  223.             HTTPGet h = new HTTPGet();
  224.             String sPage;
  225.             // System.out.println(sURL+"\r\n.........\r\n\r\n");
  226.             if (sUser.length() > 0) {
  227.                 sPage = h.Read(sURL, sUser, sPassword);
  228.             } else {
  229.                 sPage = h.Read(sURL);
  230.             }
  231.             // System.out.println(sPage+"\r\n.........\r\n\r\n");
  232.             return (sError);
  233.  
  234.         //    ...end SP.
  235.         //
  236.         } catch (StringIndexOutOfBoundsException e) {
  237.             sError = "Error: string out of bounds: " + e.getMessage();
  238.             return (sError);
  239.  
  240.         //    ...end SP.
  241.         //
  242.         } catch (Exception e) {
  243.             sError = "Error: no data available: " + e.getMessage();
  244.             return (sError);
  245.         }
  246.     }
  247.  
  248.     //    Convert Excel coordinate (e.g., "C6") into numeric X,Y (e.g., 2,5)
  249.     //    Inputs:
  250.     //        sCell: cell desigation (e.g., "C6")
  251.     //    Outputs:
  252.     //        int[0]: X-coordinate (column, e.g., 2) using zero-based counting
  253.     //        int[1]: Y-coordinate (row, e.g., 5) using zero-based counting
  254.     //
  255.     public int[] Cell2RowCol(String sCell) {
  256.         int iCoords[] = new int[2];
  257.         int nX = 0;
  258.         int nY = 0;        
  259.         sCell.toUpperCase();
  260.         char c = sCell.charAt(0);
  261.         nX = (int) c - 65;
  262.         sCell = sCell.substring(1);
  263.         c = sCell.charAt(0);
  264.         if ((int) c > 64) {
  265.             nX *= 26;
  266.             nX += ((int) c - 65);
  267.             sCell = sCell.substring(1);
  268.         }
  269.         nY = Integer.parseInt(sCell) - 1;
  270.         iCoords[0] = nX;
  271.         iCoords[1] = nY;
  272.         return (iCoords);
  273.     }
  274.  
  275.     //    Private members.
  276.     //
  277. }
  278.  
  279. //    <EOF>
  280. //
  281.