home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / bb98.exe / SOexcel2.php < prev    next >
PHP Script  |  2002-12-08  |  4KB  |  146 lines

  1. <?php if (!defined("SOexcel")) { define("SOExcel", 1);
  2.  
  3. include("SOhttp.php");
  4.  
  5. //    Convert Excel coordinate (e.g., "C6") into numeric X,Y (e.g., 2,5)
  6. //    Inputs:
  7. //        $sCell: cell desigation (e.g., "C6")
  8. //        $nX: X-coordinate (column, e.g., 2) using zero-based counting
  9. //        $nY: Y-coordinate (row, e.g., 5) using zero-based counting
  10. //    Outputs:
  11. //        None
  12. //
  13. function SOExcelCell2RowCol($sCell, &$nX, &$nY) {
  14.     $nX = $nY = 0;
  15.     $sCell = strtoupper($sCell);
  16.     $nX = ord($sCell) - 65;
  17.     $sCell = substr($sCell, 1);
  18.     if (ord($sCell) > 64) {
  19.         $nX *= 26;
  20.         $nX += (ord($sCell) - 65);
  21.         $sCell = substr($sCell, 1);
  22.     }
  23.     $nY = $sCell;
  24.     settype($nY, "integer");
  25.     $nY--;
  26.     return ("");
  27. }
  28.  
  29. //    Retrieve Excel data over specified range and fill in associative
  30. //        array with values.
  31. //    Inputs:
  32. //        $sAddr: address of BadBlue server (e.g., "127.0.0.1:8080")
  33. //        $sPath: path of shared file in EXT.INI file (e.g., "path3")
  34. //        $sFile: name of Excel file to examine (e.g., "invoice.xls")
  35. //        $nSheet: sheet number (e.g., 1)
  36. //        $aData: associative array returned with data
  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. //        $errmsg: empty if no error occurred, otherwise error message
  43. //
  44. function SOExcel($sAddr, $sPath, $sFile, $nSheet, &$aData,
  45.                  $sCellStart, $sCellEnd, $sUser = "", $sPassword = "") {
  46.     $aData = array();
  47.     $nDX = 25;
  48.     $nDY = 50;
  49.     $errmsg = "";
  50.     do {
  51.  
  52.         //    Convert cell addressing to numeric row/columns...
  53.         //
  54.         SOExcelCell2RowCol($sCellStart, $nXS, $nYS);
  55.         SOExcelCell2RowCol($sCellEnd,   $nXE, $nYE);
  56.         // echo(" nXS= $nXS nYS= $nYS nXE= $nXE NYE= $nYE <BR>");
  57.  
  58.         //    Loop through by chunks until done.
  59.         //
  60.         for ($nY = $nYS; $nY <= $nYE; $nY += $nDY) {
  61.             for ($nX = $nXS; $nX <= $nXE; $nX += $nDX) {
  62.     
  63.                 //    Construct URL and grab page...
  64.                 //
  65.                 $sURL = "http://".$sAddr."/ext.dll?MfcISAPICommand=LoadPage&".
  66.                         "page=xls.htx&a0=/get/".$sPath."/".rawurlencode($sFile)."&a1=".$nSheet."&a2=_&".
  67.                         "a3=".$nX."&a4=".$nY."&a5=".$nDX."&a6=".$nDY."&a7=2&a8=100%25";
  68.                 $errmsg = SOHTTPGet($sURL, &$sPage, $sUser, $sPassword);
  69.                 if (strlen($errmsg)) {
  70.                     break;
  71.                 }
  72.                 $aColumns = array();
  73.  
  74.                 //    Rip through first row of matrix to grab column labels.
  75.                 //
  76.                 for ($i = 0; $i < $nDX + 1; $i++) {
  77.                     $sTemp = "<TD class=fXH align=center>";
  78.                     $nCursor = strpos($sPage, $sTemp);
  79.                     if ($nCursor === false) {
  80.                         $errmsg = "Invalid template file (1)";
  81.                         break;
  82.                     }
  83.                     $nCursor += strlen($sTemp);
  84.                     if (!$i) {
  85.                         $sPage = substr($sPage, $nCursor + 2);
  86.                         continue;
  87.                     }
  88.                     $sColumn = substr($sPage, $nCursor, 2);
  89.                     if (($nCursor2 = strpos($sColumn, '<')) > 0) {
  90.                         $sColumn = substr($sColumn, 0, $nCursor2);
  91.                     }
  92.                     $aColumns[$i - 1] = $sColumn;
  93.                     $sPage = substr($sPage, $nCursor + 2);
  94.                 }
  95.                 if (strlen($errmsg)) {
  96.                     break;
  97.                 }
  98.                 // echo("Columns=".implode(", ", $aColumns)."<BR>");
  99.  
  100.                 //    Rip through multiple rows of data...
  101.                 //
  102.                 for ($i = 0; $i < $nDY; $i++) {
  103.                     for ($j = 0; $j < $nDX; $j++) {
  104.                         $sTemp = "<TD class=fXL";
  105.                         $nCursor = strpos($sPage, $sTemp);
  106.                         if ($nCursor === false) {
  107.                             $errmsg = "Invalid template file (2)";
  108.                             break;
  109.                         }
  110.                         $nCursor += strlen($sTemp);
  111.                         $nCursor = strpos($sPage, ">", $nCursor);
  112.                         if ($nCursor === false) {
  113.                             $errmsg = "Invalid template file (3)";
  114.                             break;
  115.                         }
  116.                         $nCursor++;
  117.                         //
  118.                         $sVal = substr($sPage, $nCursor, 255);
  119.                         if (($nCursor2 = strpos($sVal, "</")) > 0) {
  120.                             $sVal = substr($sVal, 0, $nCursor2);
  121.                         }
  122.                         //
  123.                         $sPage = substr($sPage, $nCursor + $nCursor2);
  124.                         $nTemp = $nY + $i + 1;
  125.                         settype($nTemp, "string");
  126.                         $aData[$aColumns[$j].$nTemp] = $sVal;
  127.                         // echo("Cell ".$aColumns[$j].$nTemp."= ".$sVal."<BR>");
  128.                     }
  129.                     if (strlen($errmsg)) {
  130.                         break;
  131.                     }
  132.                 }
  133.             //    ...end loop through chunks.
  134.             //
  135.             }
  136.             if (strlen($errmsg)) {
  137.                 break;
  138.             }
  139.         }
  140.  
  141.     } while (0);
  142.     return ($errmsg);
  143. }
  144.  
  145. } ?>
  146.