how can i save compare.xls to my directory(specified folder) and also i want to convert these convert.cls to csv format

Topics: Developer Forum, User Forum
Sep 30, 2015 at 9:03 AM
<html>
<head>
    <title>select some columns</title>
</head>
<body>
<table border="3" align="center">
<?php
$dir=mkdir(date('Y-M-D'));

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-Type: text/csv/xls');
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=compare.xls" );
//header("Content-type:application/json");
$objWriter->save("/opt/lampp/htdocs/workplace/1stwork/2015-Sep-Wed");
$handle1 = fopen("new.csv", "r");
$data1=fgetcsv($handle1,1000);
$dat= array('0' =>$data1[0],'1'=>$data1[1],'2'=>$data1[2]);
//print_r($dat);
$handle = fopen("others.csv", "r");
$data=fgetcsv($handle,1000);
$dat1 = array('0' =>$data[0],'1'=>$data[1],'2'=>$data[2],'3' =>$data[3],'4'=>$data[4],'5'=>$data[5]);
//print_r($dat1);

$result = array_intersect($dat1, $dat);

//print_r($result);
$mykey=array();
foreach($result as $key => $value)
{
$mykey[] = $key;
}
//print_r($mykey);
//exit;
$row = 1;


$wantedColumns =$mykey;


//print_r($wantedColumns);

if (($handle = fopen("others.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

$num = count($data);
if ($row == 1) {
    echo '<tr>';
}else{
    echo '<tr>';
}

for ($c=0; $c < $num; $c++) {
if (!in_array($c,$wantedColumns)) continue;
    if(empty($data[$c])) {
       $value = "&nbsp;";
    }else{
       $value = $data[$c];
    }
    if ($row == 1) {

         echo '<td style="border-top: 1px solid rgb(111,180,224); border-left: 1px solid rgb(111,180,224); border-bottom: 1px solid rgb(111,180,224);"  align="left" bgcolor="#0066cc" height="36" valign="middle" ><b><font color="#ffffff" size="2">&nbsp;&nbsp;'.$value.'&nbsp;&nbsp;</font></b></td>';
    }else{


        echo '<td>&nbsp;&nbsp;'.$value.'&nbsp;&nbsp;</td>';
    }
}

if ($row == 1) {
    echo '</tr>';
}else{
    echo '</tr>';
}
$row++;
}

echo '</tbody></table>';
echo '</center>';
fclose($handle);
}


//json_encode($mykey);
//echo json_encode($value);
?>

</table>
</body>
</html>
Coordinator
Sep 30, 2015 at 9:24 AM
Are you sure you want PHPExcel for any of this?
how can i save compare.xls to my directory(specified folder)
and also i want to convert these convert.cls to csv format
You're reading csv files (not xls files) using fgetcsv, and you don't seem to be doing anything to write them to any file format at all, just generating HTML markup.

You're also sending a mass of contradictory content-type headers to the browser, so that it will have no idea what to do with that generated markup.
text/csv/xls is completely invalid, and you're also sending application/vnd.ms-excel and application/json (which has nothing to do with any spreadsheet format).... only one content type header will actually be sent, and the last you're setting is application/json, which will be the content type header that's actually sent.


If all you're doing is working with csv files, then stick to fgetcsv() and fputcsv().

If you do want to work with spreadsheets, and use PHPExcel, then I suggest you start by reading the documentation, and looking through the examples
Sep 30, 2015 at 10:59 AM
Sir,
I want to save that file to my desired location please help me

--
Deena Babu
Software Developer@axeltechnologies pvt ltd
Coordinator
Sep 30, 2015 at 12:05 PM
Are you using PHPExcel in any way? This is a site to support the PHPExcel library, not a general PHP tutorial site
Sep 30, 2015 at 12:17 PM
Sir,
I try to use phpexcel but i got following error,
require_once(Excel/reader.php): failed to open stream: Permission denied in ,how can i solve it?

Sep 30, 2015 at 12:18 PM

error_reporting(E_ALL | E_STRICT);
require_once 'Excel/reader.php';
include 'Excel/Writer.php';
// ExcelFile($filename, $encoding);
$data = new Spreadsheet_Excel_Reader();
// Set output Encoding.
$data->setOutputEncoding('CP1251');

$file_1 = 'Excel/new.xls';
$data->read($file_1);


$data2 = new Spreadsheet_Excel_Reader();
$data2->setOutputEncoding('CP1251');

//move_uploaded_file($_FILES['file1']['tmp_name'],'Excel/'.$_FILES['file1']['name']);

$file_2 = 'Excel/others.xls';
$data2->read($file_2);

$excel = new Spreadsheet_Excel_Writer('Excel/res.xls');
$sheet =& $excel->addWorksheet('Class I');

//looping with the data of upload file
$data_res = array();

for ($i = 2; $i <= $data2->sheets[0]['numRows']; $i++)
{
$sku = isset($data2->sheets[0]['cells'][$i][1])?$data2->sheets[0]['cells'][$i][1]:'';
$qnt = isset($data2->sheets[0]['cells'][$i][2])?$data2->sheets[0]['cells'][$i][2]:'';
if($sku != ''){
for ($j = 2; $j <= $data->sheets[0]['numRows']; $j++)
{
$sku_primary = isset($data->sheets[0]['cells'][$j][1])?$data->sheets[0]['cells'][$j][1]:'';
$sku1 = isset($data->sheets[0]['cells'][$j][6])?$data->sheets[0]['cells'][$j][6]:'';
if($sku == $sku1){
$data_res[] = array($sku_primary,$qnt);
$sheet->write($i - 2, 0, $sku_primary);
$sheet->write($i - 2, 1, $qnt);
}
}
}
}
// save file to disk
if ($excel->close() === true) {
echo 'Spreadsheet successfully saved!';
} else {
echo 'ERROR: Could not save spreadsheet.';
}


Coordinator
Sep 30, 2015 at 3:10 PM
The code you've posted here doesn't use PHPExcel, it uses Spreadsheet_Excel_Reader
Oct 1, 2015 at 6:04 AM
ok fine,then you should tell how to compare two excel files and take common values by using phpexcel ?
thank you

Coordinator
Oct 1, 2015 at 8:09 AM
A good starting point is the PHPExcel Documentation