Home >Backend Development >PHP Tutorial >Data import and export using PHP to develop small programs
Use PHP to develop data import and export of small programs
Importing and exporting data are operations often involved in the development of any small program, and PHP, as a powerful back-end language, can handle it well Data processing and file manipulation needs. This article will introduce how to use PHP to develop the data import and export functions of small programs, and provide corresponding code examples.
1. Data import
In the development of small programs, it is often necessary to import data from Excel files. PHP provides a commonly used library PHPExcel, which can easily import Excel data.
<?php require_once 'PHPExcel/PHPExcel.php'; $excel = PHPExcel_IOFactory::load("data.xlsx"); $sheet = $excel->getActiveSheet(); $data = array(); foreach ($sheet->getRowIterator() as $row) { $rowData = array(); $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(FALSE); foreach ($cellIterator as $cell) { $rowData[] = $cell->getValue(); } $data[] = $rowData; } print_r($data); ?>
In the above code, first introduce the PHPExcel library, then load the Excel file through the PHPExcel_IOFactory::load()
method, and then traverse the rows and columns to load the data of each cell Stored in array.
In addition to Excel data, sometimes you also need to import data in CSV format. PHP provides the fgetcsv function to easily process CSV files.
<?php $file = fopen("data.csv", "r"); $data = array(); while (($row = fgetcsv($file)) !== false) { $data[] = $row; } fclose($file); print_r($data); ?>
In the above code, the CSV file is first opened through the fopen function, and then the data is read line by line through the fgetcsv function and stored in the array. Finally close the file.
2. Data export
Exporting data to Excel files also depends on the PHPExcel library. You can use the classes and classes provided by the PHPExcel library. Method to implement data export.
<?php require_once 'PHPExcel/PHPExcel.php'; $data = array( array('Name', 'Age', 'City'), array('John', 25, 'New York'), array('Alice', 30, 'Los Angeles'), array('Bob', 35, 'Chicago') ); $excel = new PHPExcel(); $sheet = $excel->getActiveSheet(); $sheet->fromArray($data, NULL, 'A1'); $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007'); $writer->save('data.xlsx'); ?>
In the above code, first create a PHPExcel object, and use the getActiveSheet()
method to get the current active sheet, and then write the data through the fromArray()
method in the corresponding cell of the sheet. Finally, use the createWriter()
method to create an Excel writer, and use the save()
method to save the Excel file in the specified path.
Exporting CSV data is relatively simple. You only need to output the data in the format of a CSV file.
<?php $data = array( array('Name', 'Age', 'City'), array('John', 25, 'New York'), array('Alice', 30, 'Los Angeles'), array('Bob', 35, 'Chicago') ); $file = fopen('data.csv', 'w'); foreach ($data as $row) { fputcsv($file, $row); } fclose($file); ?>
In the above code, first use the fopen function to create a CSV file, then use the fputcsv function to write each line of data into the file, and finally close the file.
Summary:
This article introduces the data import and export functions of developing small programs using PHP, and provides corresponding code examples. Through these code examples, we can easily implement data import and export operations in mini programs and improve development efficiency. At the same time, developers can also make appropriate modifications and optimizations to the code according to specific needs. I hope this article will be helpful to small program developers!
The above is the detailed content of Data import and export using PHP to develop small programs. For more information, please follow other related articles on the PHP Chinese website!