PHP Import and Export

Source: Internet
Author: User

First: Download the Phpexcel class library file.

View Layer:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title></title>

<body>
<form action= "<?php echo site_url (' Admin/excel/import_pro ')?>" method= ' Post ' enctype= "Multipart/form-data" >
<input type= "file" name= ' File_stu '/>
<input type= "Submit" value= ' Import '/>
</form>
</body>

Controller:

<?php
Defined (' BasePath ') or exit (' No Direct script access allowed ');
Class Excel extends ci_controller{
function __construct ()
{
Parent::__construct ();
$this->load->library (' Phpexcel ');
$this->load->library (' phpexcel/iofactory ');
}
Exporting from a data table to Excel
Public function Export ($table _name) {
$query = $this, db, Get ($table _name);
Print_r ($query);
if (! $query) return false;
Startingthephpexcellibrary
Load Phpexcel class
$this->load->library (' Phpexcel ');
$this->load->library (' phpexcel/iofactory ');
$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties (), Settitle ("Export"), SetDescription ("none");
Setactivesheetindex, $objPHPExcel (0);
Fieldnamesinthefirstrow
$fields = $query-List_fields ();
$col = 0;
foreach ($fields as $field) {
Setcellvaluebycolumnandrow ($col, 1, $field), Getactivesheet, $objPHPExcel
$col + +;
}
Fetchingthetabledata
$row = 2;
foreach ($query->result () as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getactivesheet ()->setcellvaluebycolumnandrow ($col, $row, $data-$field);
$col + +;
}
$row + +;
}
Setactivesheetindex, $objPHPExcel (0);
$objWriter = iofactory:: Createwriter ($objPHPExcel, ' Excel5 ');
Sendingheaderstoforcetheusertodownloadthefile
Header (' Content-type:application/vnd.ms-excel ');
Header (' content-disposition:attachment;filename= ' products_ '. Date (' DMy '). '. xls ');
Header (' content-disposition:attachment;filename= ' brand_ '. Date (' y-m-d '). '. xls ');
Header (' cache-control:max-age=0 ');
$objWriter, Save (' php://output ');
}
Import from Excel to datasheet
Function Import () {
$this->load->view (' excel_import.html ');
}
Import from Excel to datasheet
function Import_pro () {
Excel file to work with
$filename = './sampledata/example2.xls ';//Specify File
Use the Select Excel file
Print_r ($_files); exit;
$tmp _file = $_files [' File_stu '] [' tmp_name '];
$file _types = Explode (".", $_files [' File_stu '] [' name ']);
$file _type = $file _types [count ($file _types)-1];
/* discriminant is not an. xls file, discriminant is not an Excel file */
if (Strtolower ($file _type)! = "xls") {
$this->error (' Not Excel file, re-upload ');
}
$savePath = "public/uploads/excel/";
/* Name the uploaded file in time */
$str = Date (' Ymdhis ');
$file _name = $str. "." . $file _type;
/* Upload Successful */
if (!copy ($tmp _file, $savePath. $file _name)) {
$this->error (' upload failed ');
}
To get a new file path + name
$fullpath = $savePath. $file _name;
Echo $fullpath;
$re = $this->read ($fullpath, ' utf-8 ');
Var_dump ($re);
return $re;
}

Public function Read ($filename, $encode = ' utf-8 ') {
$this->load->library (' phpexcel/iofactory ');
$objReader = Iofactory::createreader (' Excel5 ');
$objReader->setreaddataonly (TRUE);
$objPHPExcel = $objReader->load ($filename);
$objWorksheet = $objPHPExcel->getactivesheet ();
$highestRow = $objWorksheet->gethighestrow ();
Echo $highestRow;
$highestColumn = $objWorksheet->gethighestcolumn ();
$highestColumnIndex = phpexcel_cell::columnindexfromstring ($highestColumn);
$excelData = Array ();
for ($row = 1; $row <= $highestRow; $row + +) {
for ($col = 0; $col < $highestColumnIndex; $col + +) {
$excelData [$row][]= (String) $objWorksheet->getcellbycolumnandrow ($col, $row)->getvalue ();
}
}
return $excelData;
}
}

Specific content, their own operation will know!

PHP Import and Export

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.