Jump to content

[SOLVED] PHP working with Excel


jschofield

Recommended Posts

Hello php'ers,
I have built a script that imports a xls and exports a csv. The xls file has the first 4 rows as a header that i am trying to remove usingmy php script. I know I can use the continue command but not to sure on how to use it. Below is my script. Any help from anyone would be a big help and really apprciated. Thanks, Johnnie.

<?php

$inputfile = "C:/SMDIM/Data/Data dump Holy Spirit School.xls";
$outputfile = "C:/SMDIM/Data/All_Student_Data.csv";
$logfilename = "C:/SMDIM/Logs/All_Student_Log.txt";
include'C:/SMDIM/Translators/importlibrary.inc.php';
//The query
$query = "SELECT * FROM [Sheet1$]";
//The excel driver we need
$connection = odbc_connect('Driver={Microsoft Excel Driver (*.xls)};
DriverId=790;Dbq='.$inputfile.';','','');
$count = 0;
//Execute the query
if(!$dbexec = odbc_exec($connection, $query)) {
wlogdie("Failed to execute the query");
}else{
wlog("Successfully executed the query");
}

//open the file
if(!$outfilefp = fopen($outputfile, "w")) {
wlogdie("Failed to open $outputfile");
}else{
wlog("Opened $outputfile for writing");
}

//add English language codes to the end of each line
while  (odbc_fetch_into($dbexec, $row)) {

$row[12] = "English";
$count++;
writeLine($outfilefp, $row);
}
if($count !== '0') {
wlog("finished the import with a total of $count records");
}else{
wlog("Failed to import data");
}

?>
Link to comment
https://forums.phpfreaks.com/topic/35865-solved-php-working-with-excel/
Share on other sites

This should prevent the first row from being added to the output file
[code]<?php
$header = true;
//add English language codes to the end of each line
while  (odbc_fetch_into($dbexec, $row)) {
  if($header) { $header = false; }
  else {
    $row[12] = "English";
    $count++;
    writeLine($outfilefp, $row);
  }
}
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.