I'm trying to import a Excel file to my PHP application with PHPExcel but It cannot work correctly with UTF-8 data.
This is my data
Độc quyền
and this is what I retrieve:
Äá»™c quyá»n
This is my code:
$cell = $objWorksheet->getCellByColumnAndRow($col, $row);
$value = $cell->getCalculatedValue();
$arraydata[$row-2][$col]=$value;
Thanks for your help. :)
Related
I am trying to import csv. And, here is my csv file for test.
https://drive.google.com/file/d/1LU4SHG_EbSj9OTRXRakNHgllZD45iaS3/view?usp=sharing
I am providing exact file so that the file structure / encoding may not changed if I paste the csv text here.
I am using CSV League https://csv.thephpleague.com/ and I have code for import as follows:
$header_row = 0;
$filename = 'test_extra.csv'
$csv_file_path = storage_path('app/files/').$filename;
if (!ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", TRUE);
}
$csv = Reader::createFromPath($csv_file_path, 'r');
//trying to conver encode to utf-8
$csv->addStreamFilter('convert.iconv.ISO-8859-15/UTF-8');
$csv->setHeaderOffset($header_row);
$sample_data = $csv->fetchOne($header_row);
print_r($sample_data);
But, It is printing data as:
This is totally weird because the first row possess data something like:
0858,0858-A1,One Bedroom All.a1,1,1,702,2,1X1,0
Is there something I might be missing?
Normal ASCII data is reading fine with this code in xlsx!
for ($row = 2; $row <= $highestRow; $row++){
$rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,TRUE);
}
But not able to read unicode data with this.
Is there any way to read unicode with this from xlsx?
Note : unicode working fine with xls! i am not getting it from xlsx only!
I've tried to import csv file into mysql table and everything is Ok but I get a problem with Encoding the content of the file, the content is in "arabic Content."
The following image explains my problem:
1- this is my CSV FILE
2-My PHP code:
$c=1;
while($data=fgetcsv($file,1000,","))
{
if($c==1)
{
$c++;
continue;
}
$row=explode(";",$data[0]);
foreach($row as $val)
{
echo $val." ";
}
echo "<br>";
$c++;
}
3- The Result:
I try to insert data in mysql table
4- If I insert these data into Mysql Table, it will be :
I'm thankful for any help,
Thanks
You need to convert your CSV file in UTF-8 or process your string using utf8_encode.
Make sure your MySQL table is also in UTF-8
/open file pointer to standard output
$fp = fopen('php://output', 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
Thanks for all answers but the solution is:
open CVS file in any editor
re-save it with UTF-8 encoding and CVS extension
and upload it by an script
I want to add excel file data into an existing XML file and a particular tag.
I want to add excel data of upc code into the upc tag of the XML file using PHP.
My excel file format is:
I want the following XML format data.
$xml = simplexml_load_file("yourxmlfilename.xml");
$prod = $xml->Products;
print_r($prod->Product->count());
for($i=1; $i<=$prod->Product->count();$i++){
$str = (string) $prod->Product[$i]->ExternalId;
if($str == 'mobilesn1'){
$c = $prod->Product[$i]->UPCs;
print_r($prod->Product[$i]->UPCs->UPC);
if((string)$prod->Product[$i]->UPCs->UPC !="895623" ){
//unset($prod->Product[$i]->UPCs->UPC);
$c->addChild('UPC', '895623');
$xml->asXML("yourxmlfilename.xml");
}
}
}
This question already has answers here:
How to convert Excel XLS to CSV using PHP
(5 answers)
Closed 7 years ago.
I want to convert Excel files (.xls) into CSV file (.csv) with a script PHP ?
I tried many codes but it didn't work like this one !
No errors appear but it wont work Any idea or any other lines of codes that I can try ?
<?php
echo("it works");
require_once '../batchs/Classes/PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = '../public/aixstream/stock.xls';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save($loadedSheetName.'.csv');
}
?>
Thank you
As already stated in this answer, you can use the PHP-ExcelReader function to read the xls file. After that, you may easily convert it into CSV (or any other other format) using the following code, also available here.
Reading the xls file
//You will obviously need to import the function
//by downloading the file from the link above.
$reader=new Spreadsheet_Excel_Reader(); //Instantiate the function
$reader->setUTFEncoder('iconv'); // Set Encoder
$reader->setOutputEncoding('UTF-8'); // Set Output Encoding Type
$reader->read($filename); // Read the xls file
Data Output
/***
* Information about sheets is stored in boundsheets variable.
* This code displays each sheet's name.
***/
foreach ($reader->boundsheets as $k=>$sheet) //Run loop for all sheets in the file
{
echo "\n$k: $sheet";
}
//Now just save the data in the array as csv
/***
* Data of the sheets is stored in sheets variable.
* For every sheet, a two dimensional array holding table is created.
* This code saves all data to CSV file.
***/
foreach($reader->sheets as $k=>$data) // Run loop for all items.
{
echo "\n\n ".$reader->boundsheets[$k]."\n\n"; //Print Title
foreach($data['cells'] as $row) // Loop for all items
{
foreach($row as $cell) // Loop for every cell
{
echo "$cell".","; //Add a comma after each value
}
}
}
//It works! :D