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?