简体   繁体   English

如何通过php上传一个Excel文件并在网页上显示其所有行和列

[英]How to upload one excel file and display all its rows and columns on the web page via php

I am writing a code for uploading one excel file.And displaying its full content on a webpage.But whenever user clicks on the submit button it shows the error- "Your File Type is:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet File type must be text(.txt) or msword(.doc)." 我正在编写用于上传一个excel文件的代码。并将其完整内容显示在网页上。但是,每当用户单击“提交”按钮时,它都会显示错误-“您的文件类型为:application / vnd.openxmlformats-officedocument.spreadsheetml.sheet文件类型必须为text(.txt)或msword(.doc)。”

Below is my code. 下面是我的代码。

 <?php
 if( isset($_POST['submit1'])) {
 // $_FILES is the array auto filled when you upload a file and submit a form.
 $userfile_name = $_FILES['file1']['name']; // file name
 $userfile_tmp  = $_FILES['file1']['tmp_name']; // actual location
 $userfile_size  = $_FILES['file1']['size']; // file size
 $userfile_type  = $_FILES['file1']['type']; 
 $userfile_error  = $_FILES['file1']['error']; // any error!. get from here

 // Content uploading.
  $file_data = '';
 if ( !empty($userfile_tmp))
 {
    $file_data=base64_encode(@fread(fopen($userfile_tmp,'r'),  filesize($userfile_tmp)));

 }

 switch (true)
         {
       // Check error if any
           case ($userfile_error == UPLOAD_ERR_NO_FILE):
        case empty($file_data):
       echo 'You must select a document to upload before you can save this page.';
        exit;
        break;
       case ($userfile_error == UPLOAD_ERR_INI_SIZE):
      case ($userfile_error == UPLOAD_ERR_FORM_SIZE):
     echo 'The document you have attempted to upload is too large.';
   break;

   case ($userfile_error == UPLOAD_ERR_PARTIAL):
  echo 'An error occured while trying to recieve the file. Please try again.';
       break;

        }

     if( !empty($userfile_tmp))
       {
     // only MS office and text file is accepted.
       if( !(($userfile_type=="application/msword") || ($userfile_type=="text/plain") ||       ($userfile_type=="application/vnd.ms-excel")) )
       {echo 'Your File Type is:'. $userfile_type;
      echo '<br>File type must be text(.txt) or msword(.doc).';

       exit;
       }
     }
     echo filesize($userfile_tmp);
    } 
   ?>
     <HTML>
     <HEAD>
    <TITLE> PHP File Upload Script </TITLE>

    </HEAD>
   <BODY>
   <form name="profile" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>"   target="_self" enctype="multipart/form-data" >
  <P align ="center"><input type="hidden" name="MAX_FILE_SIZE" value="1000000">

 <input name="file1" type="file" accept="application/vnd.openxmlformats-     officedocument.spreadsheetml.sheet" />

<input type="submit" name="submit1" value="Submit" />
</P>

</form>

</BODY>
</HTML>

Please help . 请帮忙 。 Thank you in advance. 先感谢您。

You need to add following in your if condition as well because your are checking for application/vnd.ms-excel and application/msword but file is having its header different as 您还需要在if条件中添加以下内容,因为您正在检查application/vnd.ms-excelapplication/msword但文件的标头与

"application/vnd.openxmlformats-officedocument.spreadsheetml.sheetyour" So if you add this inside if like "application/vnd.openxmlformats-officedocument.spreadsheetml.sheetyour"因此,如果您将其添加到内部,例如

if(($userfile_type=="application/vnd.openxmlformats-officedocument.spreadsheetml.sheetyour")  || ($userfile_type=="application/msword") || ($userfile_type=="text/plain") ||   
 ($userfile_type=="application/vnd.ms-excel")){
 // code
 }

Because server will interpret the file type based on headers it receives. 因为服务器将根据接收到的标头解释文件类型。

If you want to show Excel file contents on your page then you should think of using PHPExcel. 如果要在页面上显示Excel文件内容,则应考虑使用PHPExcel。 It is very easy to use. 这是非常容易使用。

Link: https://phpexcel.codeplex.com/ 链接: https//phpexcel.codeplex.com/

Examples: https://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home 范例: https//phpexcel.codeplex.com/wikipage?title = Examples &referringTitle = Home

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM