简体   繁体   English

JavaScript xlsx文件上传

[英]JavaScript xlsx file upload

How can i do xlsx file upload from web page to server ? 如何将xlsx文件从网页上传到服务器? I have issue about read Excel file. 我有关于读取Excel文件的问题。 I have a php code but i need upload file from web page to my server . 我有一个php代码,但我需要将文件从网页上传到服务器。 Because if i dont upload it, I cannot read file. 因为如果我不上传它,我将无法读取文件。

this is my php code. 这是我的PHP代码。 In this code i need upload the File. 在此代码中,我需要上传文件。

header('Content-Type: text/plain');

    if (isset($argv[1]))
    {
        $Filepath = $argv[1];
    }
    elseif (isset($_POST['File']))
    {
        $Filepath = $_POST['File'];
    }
    else
    {
        if (php_sapi_name() == 'cli')
        {
            echo 'Please specify filename as the first argument'.PHP_EOL;
        }
        else
        {
            echo 'Please specify filename as a HTTP GET parameter "File", e.g., "/test.php?File=test.xlsx"';
        }
        exit;
    }

    // Excel reader from http://code.google.com/p/php-excel-reader/
    require('php-excel-reader/excel_reader2.php');
    require('SpreadsheetReader.php');

    date_default_timezone_set('UTC');

    $StartMem = memory_get_usage();


    try
    {
        $Spreadsheet = new SpreadsheetReader($Filepath);
        $BaseMem = memory_get_usage();

        $Sheets = $Spreadsheet -> Sheets();


        //print_r($Sheets);
        $TabloArray=array();
        $Satir=array();

        foreach ($Sheets as $Index => $Name)
        {


            $Time = microtime(true);

            $Spreadsheet -> ChangeSheet($Index);

            foreach ($Spreadsheet as $Key => $Row)
            {
                //echo $Key.': ';
                if($Key==0)
                {


                    continue;
                }
                if ($Row)
                {
                    $Satir['Isim']=$Row[0];
                    $Satir['SoyIsim']=$Row[1];
                    $Satir['Yas']=$Row[2];
                    $TabloArray[]=$Satir;
                }
                else
                {
                    var_dump($Row);
                }
                $CurrentMem = memory_get_usage();
            }



        }
        print_r(json_encode($TabloArray));

    }
    catch (Exception $E)
    {
        echo $E -> getMessage();
    }

And JavaScript code for read 和JavaScript代码可供读取

json_obj = $.parseJSON(veri);//parse JSON
for(var i in json_obj)
    {
        Isimler[i]=json_obj.Isim;
        SoyIsim[i]=json_obj.SoyIsim;
        Yas[i]=json_obj.Yas;
        var Table=document.getElementById("tablo_icin2");
        var td = document.createElement("td");
        var tr=document.createElement("tr");
td.appendChild(document.createTextNode(json_obj[i].Isim));
                tr.setAttribute("id","element"+i);
                tr.appendChild(td);
                td = document.createElement("td");
td.appendChild(document.createTextNode(json_obj[i].SoyIsim));
                tr.appendChild(td);
                td = document.createElement("td");
td.appendChild(document.createTextNode(json_obj[i].Yas));
                tr.appendChild(td);
                Table.appendChild(tr);
}

Use parser: If you want read file by JavaScript you can use https://github.com/SheetJS/js-xlsx for it. 使用解析器:如果要通过JavaScript读取文件,则可以使用https://github.com/SheetJS/js-xlsx It you want read file by php you can use https://github.com/PHPOffice/PHPExcel/tree/develop/Documentation after upload you file as simple file. 如果您想通过php读取文件,则可以在将文件上传为简单文件后使用https://github.com/PHPOffice/PHPExcel/tree/develop/Documentation

Why not just upload the file to the server and save its path to the database. 为什么不将文件上传到服务器并将其路径保存到数据库。 And then when you want to read it, fetch the path from database relative to the upload folder of the excel file. 然后,当您要读取它时,请从数据库中获取相对于excel文件的上载文件夹的路径。

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

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