Search
 
SCRIPT & CODE EXAMPLE
 

PHP

export to excel in php

$queryexport = ("
SELECT username,password,fullname FROM ecustomer_users
WHERE fk_customer='".$fk_customer."'
");

$row = mysql_fetch_assoc($queryexport);

$result = mysql_query($queryexport);
$header = '';

for ($i = 0; $i < $count; $i++){
   $header .= mysql_field_name($result, $i)."	";
   }

while($row = mysql_fetch_row($result)){
   $line = '';
   foreach($row as $value){
          if(!isset($value) || $value == ""){
                 $value = "	";
          }else{
                 $value = str_replace('"', '""', $value);
                 $value = '"' . $value . '"' . "	";
                 }
          $line .= $value;
          }
   $data .= trim($line)."
";
   $data = str_replace("
", "", $data);

if ($data == "") {
   $data = "
no matching records found
";
   }
}
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

// output data
echo $header."
".$data;

mysql_close($conn);`
Comment

create excel file using php]

$books = [
    ['ISBN', 'title', 'author', 'publisher', 'ctry' ],
    [618260307, 'The Hobbit', 'J. R. R. Tolkien', 'Houghton Mifflin', 'USA'],
    [908606664, 'Slinky Malinki', 'Lynley Dodd', 'Mallinson Rendel', 'NZ']
];
$xlsx = SimpleXLSXGen::fromArray( $books );
$xlsx->saveAs('books.xlsx');
//  $xlsx->downloadAs('books.xlsx');
//git repo given below 
Comment

Using the PHPExcel library to read an Excel file and transfer the data into a database

//  Include PHPExcel_IOFactory
include 'PHPExcel/IOFactory.php';

$inputFileName = './sampleData/example1.xls';

//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                    NULL,
                                    TRUE,
                                    FALSE);
    //  Insert row data array into your database of choice here
}
Comment

how do i use php read excel file

Mark Baker was extremely helpful in guiding me to the right answer. I don't use Composer with PHP (I should probably learn), but given that, in order to get this to work I went to the GitHub page for PHPExcel (https://github.com/PHPOffice/PHPExcel), clicked the green Clone and download button, and then the Download ZIP link.

After unzipping the file, I got a folder called PHPExcel-1.8. I moved that folder to the same folder as both the Excel file I wanted to read (in my code below test.xlsx) and the PHP file that has the code below.

The key to getting it to work was inputting the correct path to the IOFactory.php file. It may seem simple to some, but it was tripping me up.
Comment

php read excel file

I use PHP-ExcelReader to read xls files, and works great.
Comment

PREVIOUS NEXT
Code Example
Php :: Remove White Space At Sides 
Php :: laravel route namespace 
Php :: laravel seeder update 
Php :: packagist carbon 
Php :: global variable in laravel 
Php :: wordpress basic auth 
Php :: whats the difference between using date function and DATETime in php 
Php :: add data to the collection laravel 
Php :: input if not null laravel 
Php :: how to update dropdown value in laravel 
Php :: how to add drop a table in phpmyadmin 
Php :: laravel env in js 
Php :: how to create object in php 
Php :: how pass optional route parameter in laravel 
Php :: php mvc example 
Php :: PHP Custom Time Ago Function 
Php :: optional route parameter in laravel 
Php :: Eloquent orWhere clousure 
Php :: laravel send request on tor request 
Php :: enset laravel session 
Php :: aravel cache store does not support tagging 
Php :: count vs sizeof php 
Php :: php header accept post request from same domain 
Php :: SQLSTATE[42S02]: Base table or view not found: 1146 Table many to many in laravel 
Php :: exe:/usr/local/bin/php 
Php :: deploy php composer with vercel.com 
Php :: php import 
Php :: simple php round When a parameter is passed with a specific precision value 
Php :: laravel read csv 
Php :: Define Events in Model 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =