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
    <title>select some columns</title>
<table border="3" align="center">

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/" );
header( "Content-disposition: attachment; filename=compare.xls" );
$handle1 = fopen("new.csv", "r");
$dat= array('0' =>$data1[0],'1'=>$data1[1],'2'=>$data1[2]);
$handle = fopen("others.csv", "r");
$dat1 = array('0' =>$data[0],'1'=>$data[1],'2'=>$data[2],'3' =>$data[3],'4'=>$data[4],'5'=>$data[5]);

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

foreach($result as $key => $value)
$mykey[] = $key;
$row = 1;

$wantedColumns =$mykey;


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

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

for ($c=0; $c < $num; $c++) {
if (!in_array($c,$wantedColumns)) continue;
    if(empty($data[$c])) {
       $value = "&nbsp;";
       $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>';

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

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

echo '</tbody></table>';
echo '</center>';

//echo json_encode($value);

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/ 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
I want to save that file to my desired location please help me

Deena Babu
Software Developer@axeltechnologies pvt ltd
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
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.

$file_1 = 'Excel/new.xls';

$data2 = new Spreadsheet_Excel_Reader();


$file_2 = 'Excel/others.xls';

$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.';

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

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