Discussione: leggere excel con php
View Single Post
Old 05-05-2005, 11:41   #4
RaouL_BennetH
Senior Member
 
L'Avatar di RaouL_BennetH
 
Iscritto dal: Sep 2004
Messaggi: 3967
Quote:
Originariamente inviato da Braseolo
ci avevo pensato anche io.. mi sono perņ bloccato in quanto non so come esportare in txt (o altro) il mio foglio di lavoro in ambiente linux.

purtroppo il file .xls da cui devo attingere cambia tutti i giorni, quindi la procedura deve essere completamente automatica...

tu come avevi fatto?
Quindi, se non ho capito male, non sei tu che gestisci il foglio di lavoro, dato che per esportare i dati in formato testo delimitato, c'č una comoda funzione nel menu file di excel, "esporta". Per quanto riguarda quello che feci io, se hai pazienza potrei postartelo verso sera, non ho il cd con lo script a portata di mano e sinceramente non ricordo bene.

Ho comunque trovato quest'esempio su google:

Codice:
<?php

// The example of print out the result
$result = array();
searchEXL("C:/test.xls", "test", $result);

foreach($result as $sheet => $rs){
echo "Found at $sheet";

echo "<table width=\"100%\" border=\"1\"><tr>";

for($i = 0; $i < count($rs["FIELD"]); $i++)
echo "<th>" . $rs["FIELD"][$i] . "</th>";

echo "</tr>";

for($i = 0; $i < count($rs["TEXT"]); $i++) {
echo "<tr>";

for($j = 0; $j < count($rs["FIELD"]); $j++)
echo "<td>" . $rs["ROW"][$i][$j] . "</td>";

echo "</tr>";
}
echo "</table>";

}

/**
* @param $file string The excel file path
* @param $keyword string The keyword
* @param $result array The search result
*/

function searchEXL($file, $keyword, &$result) {
$exlObj = new COM("Excel.Application") or Die ("Did not connect");
$exlObj->Workbooks->Open($file);
$exlBook = $exlObj->ActiveWorkBook;
$exlSheets = $exlBook->Sheets;

for($i = 1; $i <= $exlSheets->Count; $i++) {
$exlSheet = $exlBook->WorkSheets($i);

$sheetName = $exlSheet->Name;

if($exlRange = $exlSheet->Cells->Find($keyword)) {
$col = 1;
while($fields = $exlSheet->Cells(1, $col)) {
if($fields->Text == "")
break;

$result[$sheetName]["FIELD"][] = $fields->Text;
$col++;
}

$firstAddress = $exlRange->Address;
$finding = 1;
$result[$sheetName]["TEXT"][] = $exlRange->Text;

for($j = 1; $j <= count($result[$sheetName]["FIELD"]); $j++) {
$cell = $exlSheet->Cells($exlRange->Row ,$j);
$result[$sheetName]["ROW"][$finding - 1][$j - 1] = $cell->Text;
}

while($exlRange = $exlRange->Cells->Find($keyword)) {
if($exlRange->Address == $firstAddress)
break;

$finding++;
$result[$sheetName]["TEXT"][] = $exlRange->Text;

for($j = 1; $j <= count($result[$sheetName]["FIELD"]); $j++) {
$cell = $exlSheet->Cells($exlRange->Row ,$j);
$result[$sheetName]["ROW"][$finding - 1][$j - 1] = $cell->Text;
}

}

}

}

$exlBook->Close(false);
unset($exlSheets);
$exlObj->Workbooks->Close();
unset($exlBook);
$exlObj->Quit;
unset($exlObj);
}
?>
Ovviamente devi sostituire il nome Test con il nome del tuo file.
__________________
Dai wafer di silicio nasce: LoHacker... il primo biscotto Geek
RaouL_BennetH č offline   Rispondi citando il messaggio o parte di esso