Excel upload/download

Source: Internet
Author: User
: This article mainly introduces how to upload and download excel files. For more information about PHP tutorials, see. Html code

Document







Upload:

Php code Upload

'Set NAMES \ 'utf8 \ ''); // connect to the database and SET the data transmitted to the UTF-8 encoding format. // introduce the excel class include '. /PHPExcel. php '; // reference class file include '. /PHPExcel/IOFactory. php '; // reference the corresponding class file/* echo $ _ FILES ["file"] ["name"]."
"; Echo $ _ FILES [" file "] [" type "]."
"; Echo ($ _ FILES [" file "] [" size "]/1024)." Kb
"; Echo $ _ FILES [" file "] [" tmp_name "]."
"; */Foreach ($ _ FILES as $ key => $ val) {$ str = substr ($ val ['name'], strrpos ($ val ['name'], '. ') + 1); if ($ val ["error"]> 0) {echo $ val ['name']. "upload error "."
"; Continue;} if ($ str! = 'XLS ') {echo' must be an xls file '."
"; Continue;} if (file_exists (iconv (" UTF-8 "," GB2312 ",'. /xls /'. $ val ["name"]) {echo $ val ['name']. "already exists "."
"; Continue;} else {move_uploaded_file ($ val [" tmp_name "], iconv (" UTF-8 "," GB2312 ",'. /xls /'. $ val ["name"]); echo $ val ["name"], 'Upload successful! ';} $ XlsPath = iconv ("UTF-8", "GB2312 ",'. /xls /'. $ val ["name"]); // specify the exls path to be read // $ type = 'excel2007 '; // Set the Excel type to be parsed: Excel5 (2003 or earlier) or Excel2007 $ type = 'excel5'; $ xlsReader = PHPExcel_IOFactory: createReader ($ type ); $ Sheets = $ xlsReader-> load ($ xlsPath); // start to read $ Sheet = $ Sheets-> getSheet (0)-> toArray (); // read the first worksheet (note that the number starts from 0). If you want to read multiple worksheets, you can create a loop of 0, 1, 2, 3 .... // Obtain a two-dimensional array. each small array is the data echo of each column containing this row in a row of the excel table content'
'; // Print_r ($ Sheet); die; foreach ($ Sheet as $ k => $ v) {$ res = $ db-> exec ("insert into a (data) values ('". $ v [1]. "')"); // write the database if ($ res) {// print_r ($ Sheet); // output data after successful execution }}?>

Php code download

'Set NAMES \ 'utf8 \ ''); // instantiate the pdo object $ res = $ db-> query (" select * from "); // query data from the database $ str = "id \ t Data \ t \ n"; $ str = iconv ('utf-8', 'gb2312', $ str ); // iconv () is the encoding conversion function $ row = $ res-> fetchAll (); foreach ($ row as $ key => $ val) {$ id = iconv ('utf-8', 'gb2312', $ val ['id']); $ data = iconv ('utf-8', 'gb2312 ', $ val ['data']); $ str. = $ id. "\ t ". $ data. "\ t \ n" ;}$ filename = date('Ymd').'.xls '; exportExcel ($ filename, $ str); // Generate an excel table

The above introduces the excel upload and download, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.