Php: The easiest way to read an excel table

Source: Internet
Author: User
Today, a colleague asked me if I could import an excel table in CRM. I said yes. although I have never done this before, it is nothing new to use php to operate excel documents.

Today, a colleague asked me if I could import an excel table in CRM. I said yes. although I have never done this before, it is nothing new to use php to operate excel documents, so, I thought about it for a moment, so let him prepare the table ~~

After that, I went online for a while and found that most of them were implemented using the ExcelReader class. Although this stuff is very powerful and has many functions, but in some cases it is not necessarily the best. So here we will not discuss this excelreader class library.

I don't know if you have used excel, but there is a format of example. CSV in the table format "~ What is this CSV? you can study it in private. here, we will only take a look at the popular science. CSV is short for comma-separated values. to put it bluntly, it stores data with commas (,) and line breaks. You can try to save a simple table file in the. csv format, and then open it in notepad to see what it looks like ~~

It's easy to do with CSV. there is the fgetcsv () function in PHP, reading a row from the file pointer and parsing the CSV field. is it so cheerful? For a short piece of code:

$ Handle = fopen ("demo.csv", "r ");
$ I = 0;
$ Result = array ();
While ($ row = fgetcsv ($ handle, 1000, ",") {// The second parameter must be longer than the longest row in the file... for detailed parameters, see the manual.
If (empty ($ row [0]) | empty ($ row [1]) | empty ($ row [2]) continue; // skip a column with a null value.
$ Result [$ I] = $ row;
$ I ++;
}
Echo'
';
Print_r ($ result );
?>

How is this code compared to the ExcelReader class library ?? This method can easily read some relatively simple excel table data. However, you must note that the first column of the file to be read using the fgetcsv () method cannot contain Chinese characters; otherwise, garbled characters may occur. Although the setlocale () function is used for transcoding on the Internet, but I haven't figured out how to use this function in a loop... I want to add a number column at the beginning of the table, such as the serial number. should it be simpler?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.