Home Backend Development PHP Tutorial excel upload download

excel upload download

Aug 08, 2016 am 09:25 AM
application content header quot

html code

<!doctype html>
<<span>html </span>lang=<span>"en"</span>>
 <<span>head</span>>
  <<span>meta </span>charset=<span>"UTF-8"</span>>
  <<span>meta </span>name=<span>"Generator" </span>content=<span>"EditPlus?"</span>>
  <<span>meta </span>name=<span>"Author" </span>content=<span>""</span>>
  <<span>meta </span>name=<span>"Keywords" </span>content=<span>""</span>>
  <<span>meta </span>name=<span>"Description" </span>content=<span>""</span>>
  <<span>title</span>>Document</<span>title</span>>
 </<span>head</span>>
 <<span>body</span>>
 <<span>center</span>>
     <<span>br</span>><<span>br</span>><<span>br</span>>

     <<span>br</span>><<span>br</span>><<span>br</span>>
     <<span>form </span>method=<span>"post" </span>action=<span>"a.php" </span>enctype=<span>"multipart/form-data"</span>>
         <<span>input </span>type=<span>"button" </span>value=<span>"下载" </span>onclick=<span>"</span><span>download</span>()<span>"</span>>
         <<span>input </span>type=<span>'button' </span>value=<span>"加一个" </span>onclick=<span>"</span><span>jia</span>()<span>"</span>><<span>br</span>><<span>br </span>id=<span>"j"</span>>
         上传:<<span>input </span>type=<span>"file" </span>name=<span>'file' </span>id=<span>'file'</span>><<span>br</span>><<span>br</span>>
        <<span>input </span>type=<span>'submit' </span>value=<span>"OK"</span>>
     </<span>form</span>>
 </<span>center</span>>
 </<span>body</span>>
 <<span>script </span>src=<span>"./jq.js"</span>></<span>script</span>>
<<span>script</span>>
    <span>var </span><span>i</span><span>=</span><span>1</span>;
    <span>function </span><span>jia</span>(){
        $(<span>"#j"</span>).after(<span>'上传:<input type="file" name="file'</span><span>&#43;</span><span>i</span><span>&#43;</span><span>'" id="file'</span><span>&#43;</span><span>i</span><span>&#43;</span><span>'"><br><br>'</span>);
        <span>i</span><span>++</span>;
    }
    <span>function </span><span>download</span>(){
        <span>location</span>.<span>href</span><span>=</span><span>'b.php'</span>;
    }
</<span>script</span>>
</<span>html</span>>
Copy after login

php code upload

<span><?php
</span><span>header(</span><span>"Content-type:text/html;charset=utf-8"</span><span>);</span><span>//设置编码
</span><span>$dsn=</span><span>"mysql:dbname=book;host=localhost"</span><span>;
</span><span>$db=new </span><span>PDO(</span><span>$dsn</span><span>,</span><span>'root'</span><span>,</span><span>'root'</span><span>,</span><span>array</span><span>(PDO</span><span>::</span><span>MYSQL_ATTR_INIT_COMMAND</span><span>=></span><span>'SET NAMES </span><span>\'</span><span>UTF8</span><span>\'</span><span>'</span><span>));</span><span>//连接数据库,设置传递数据库的数据为utf8的编码格式
</span><span>//引入excel类
</span><span>include </span><span>'./PHPExcel.php'</span><span>;   </span><span>//引用类文件
</span><span>include </span><span>'./PHPExcel/IOFactory.php'</span><span>;</span><span>//引用相应的类文件
</span><span>   /*echo $_FILES["file"]["name"] . "<br />";
</span><span>    echo $_FILES["file"]["type"] . "<br />";
</span><span>    echo ($_FILES["file"]["size"] / 1024) . " Kb<br />";
</span><span>    echo $_FILES["file"]["tmp_name"]. "<br />";*/
</span><span></span><span>foreach</span><span>(</span><span>$_FILES as $key </span><span>=> </span><span>$val</span><span>){
</span><span></span><span>$str = </span><span>substr(</span><span>$val</span><span>[</span><span>'name'</span><span>],strrpos(</span><span>$val</span><span>[</span><span>'name'</span><span>],</span><span>'.'</span><span>)</span><span>+</span><span>1</span><span>);
</span><span></span><span>if </span><span>(</span><span>$val</span><span>[</span><span>"error"</span><span>] </span><span>> </span><span>0</span><span>)
</span><span>        {
</span><span></span><span>echo $val</span><span>[</span><span>'name'</span><span>]</span><span>.</span><span>"上传发生错误"</span><span>. </span><span>"<br />"</span><span>;
</span><span></span><span>continue</span><span>;
</span><span>        }
</span><span></span><span>if</span><span>(</span><span>$str != </span><span>'xls'</span><span>){
</span><span></span><span>echo </span><span>'必须是xls文件'</span><span>. </span><span>"<br />"</span><span>;
</span><span></span><span>continue</span><span>;
</span><span>        }
</span><span></span><span>if </span><span>(file_exists(iconv(</span><span>"UTF-8"</span><span>,</span><span>"GB2312"</span><span>,</span><span>'./xls/'</span><span>.$val</span><span>[</span><span>"name"</span><span>])))
</span><span>        {
</span><span></span><span>echo $val</span><span>[</span><span>'name'</span><span>] </span><span>. </span><span>"已存在"</span><span>. </span><span>"<br />"</span><span>;
</span><span></span><span>continue</span><span>;
</span><span>        }
</span><span></span><span>else
</span><span></span><span>{
</span><span>            move_uploaded_file(</span><span>$val</span><span>[</span><span>"tmp_name"</span><span>],iconv(</span><span>"UTF-8"</span><span>,</span><span>"GB2312"</span><span>,</span><span>'./xls/'</span><span>.$val</span><span>[</span><span>"name"</span><span>]));
</span><span></span><span>echo $val</span><span>[</span><span>"name"</span><span>],</span><span>'以上传成功!'</span><span>;
</span><span>        }
</span><span></span><span>$xlsPath = </span><span>iconv(</span><span>"UTF-8"</span><span>,</span><span>"GB2312"</span><span>,</span><span>'./xls/'</span><span>.$val</span><span>[</span><span>"name"</span><span>]); </span><span>//指定要读取的exls路径
</span><span>        //$type = 'Excel2007'; //设置要解析的Excel类型 Excel5(2003或以下版本)或Excel2007
</span><span></span><span>$type = </span><span>'Excel5'</span><span>;
</span><span></span><span>$xlsReader = </span><span>PHPExcel_IOFactory</span><span>::</span><span>createReader(</span><span>$type</span><span>);
</span><span></span><span>$Sheets = $xlsReader-></span><span>load(</span><span>$xlsPath</span><span>);
</span><span></span><span>//开始读取
</span><span></span><span>$Sheet = $Sheets-></span><span>getSheet(</span><span>0</span><span>)</span><span>-></span><span>toArray(); </span><span>//读取第一个工作表(注意编号从0开始) 如果读取多个可以做一个循环0,1,2,3....
</span><span></span><span>        //得到二维数组,每个小数组是excel表格内容的一行 里面包含此行的每列的数据
</span><span></span><span>echo </span><span>'<pre class="brush:php;toolbar:false">'</span><span>;
</span><span></span><span>//print_r($Sheet);die;
</span><span></span><span>foreach</span><span>(</span><span>$Sheet as $k</span><span>=></span><span>$v</span><span>){
</span><span></span><span>$res=$db-></span><span>exec(</span><span>"INSERT INTO a (data) values('"</span><span>.$v</span><span>[</span><span>1</span><span>]</span><span>.</span><span>"')"</span><span>);</span><span>//写入数据库
</span><span></span><span>if</span><span>(</span><span>$res</span><span>){
</span><span></span><span>//print_r($Sheet); //执行成功后,输出数据
</span><span></span><span>}
</span><span>        }
</span><span>    }
</span><span>?></span>
Copy after login

php code download

<span><?php
</span><span>header(</span><span>"Content-type:text/html;charset=utf-8"</span><span>);</span><span>//设置编码
</span><span>function </span><span>exportExcel(</span><span>$filename</span><span>,</span><span>$content</span><span>){
</span><span>    header(</span><span>"Cache-Control: must-revalidate, post-check=0, pre-check=0"</span><span>);
</span><span>    header(</span><span>"Content-Type: application/vnd.ms-execl"</span><span>);
</span><span>    header(</span><span>"Content-Type: application/force-download"</span><span>);
</span><span>    header(</span><span>"Content-Type: application/download"</span><span>);
</span><span>    header(</span><span>"Content-Disposition: attachment; filename="</span><span>.$filename</span><span>);
</span><span>    header(</span><span>"Content-Transfer-Encoding: binary"</span><span>);
</span><span>    header(</span><span>"Pragma: no-cache"</span><span>);
</span><span>    header(</span><span>"Expires: 0"</span><span>);
</span><span></span><span>echo $content</span><span>;
</span><span></span><span>}
</span><span></span><span></span><span>$dsn=</span><span>"mysql:dbname=book;host=localhost"</span><span>;
</span><span>$db=new </span><span>PDO(</span><span>$dsn</span><span>,</span><span>'root'</span><span>,</span><span>'root'</span><span>,</span><span>array</span><span>(PDO</span><span>::</span><span>MYSQL_ATTR_INIT_COMMAND</span><span>=></span><span>'SET NAMES </span><span>\'</span><span>UTF8</span><span>\'</span><span>'</span><span>));</span><span>//实例化pdo对象
</span><span>$res=$db-></span><span>query(</span><span>"select * from a"</span><span>);</span><span>//从数据库查询数据
</span><span>$str = </span><span>"id</span><span>\t</span><span>数据</span><span>\t\n</span><span>"</span><span>;
</span><span>$str = </span><span>iconv(</span><span>'utf-8'</span><span>,</span><span>'gb2312'</span><span>,</span><span>$str</span><span>);</span><span>//iconv() 是编码转换函数
</span><span>$row=$res-></span><span>fetchAll();
</span><span>foreach</span><span>(</span><span>$row as $key </span><span>=> </span><span>$val</span><span>){
</span><span></span><span>$id = </span><span>iconv(</span><span>'utf-8'</span><span>,</span><span>'gb2312'</span><span>,</span><span>$val</span><span>[</span><span>'id'</span><span>]);
</span><span></span><span>$data = </span><span>iconv(</span><span>'utf-8'</span><span>,</span><span>'gb2312'</span><span>,</span><span>$val</span><span>[</span><span>'data'</span><span>]);
</span><span></span><span>$str .= $id.</span><span>"</span><span>\t</span><span>"</span><span>.$data.</span><span>"</span><span>\t\n</span><span>"</span><span>;
</span><span>}
</span><span>$filename = </span><span>date(</span><span>'Ymd'</span><span>)</span><span>.</span><span>'.xls'</span><span>;
</span><span>exportExcel(</span><span>$filename</span><span>,</span><span>$str</span><span>); </span><span>//生成excel表格</span>
Copy after login

The above introduces excel uploading and downloading, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
How does SpringBoot pass parameters in the Header through Feign calls? How does SpringBoot pass parameters in the Header through Feign calls? May 16, 2023 pm 08:38 PM

[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

What is the principle of python WSGI Application? What is the principle of python WSGI Application? May 19, 2023 pm 01:25 PM

The python environment this article relies on is: What is WSGI? WSGI is also called the web server universal gateway interface, and its full name is webservergatewayinterface. It defines a standard for how web servers and web applications should communicate and handle http requests and responses in Python. Note that it is just a protocol, or a specification or standard. You don’t have to follow this standard. Just like the web server we wrote in the previous article. WSGI is also divided into applications and server gateways. Among them, the well-known Flask belongs to applications, and uWSGI and wsgiref belong to server gateways. Personal feeling, WSG

What does linux header mean? What does linux header mean? Jul 18, 2023 pm 03:34 PM

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

How to jump in php header How to jump in php header Dec 02, 2022 am 09:14 AM

How to implement jump in php header: 1. Use "Header("Location:$url");" syntax to implement jump; 2. Use if judgment to implement jump, with jump statements such as "if($_COOKIE[" u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');".

How to use PHP header() method to adjust web pages How to use PHP header() method to adjust web pages Mar 28, 2023 pm 01:54 PM

PHP is a powerful programming language that can be used to create dynamic websites and web applications. One of the most powerful features is PHP’s header() method. In this article, we will explore how to use PHP’s header() method to adjust web pages.

What is the difference between html5 tag head and header? What is the difference between html5 tag head and header? Jan 17, 2022 am 11:10 AM

Differences: 1. The head tag is used to define the head of the document, which is a container for all head elements, and the header tag is used to define the header (introduction information) of the document; 2. All browsers support the head tag, and older versions of browsers None of the browsers support the header tag, and browsers such as IE9+ and above are required to support the header tag.

Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions Complete list of PHP file download functions: analysis of file download examples of readfile, header, Content-Disposition and other functions Nov 18, 2023 pm 03:26 PM

Complete list of PHP file download functions: File download example analysis of readfile, header, Content-Disposition and other functions. File download is one of the essential functions in Web applications, and PHP, as a widely used Web development language, provides many A function and method to implement file downloading. This article will introduce commonly used file download functions in PHP, including readfile, header, Content-Dispo

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

See all articles