zoukankan      html  css  js  c++  java
  • php 利用ExcelReader实现EXCEL转换成缓存

    如果不清楚ExcelReader的话,请查看 php读取excel类——PHP-ExcelReader


    大概的步骤:

    1、有个上传文件的php文件

    2、有个处理上传文件的php文件

    3、一些需要处理的excel 。

    第一步:上传的页面文件

    效果:

    相关代码:

    <html>
    <body>

    <form action="example.php" method="post"
    enctype
    ="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    </body>
    </html>

    第二部  需要的处理文件(完成读取并转换成php数据,最后序列化写入缓存)

    相关代码:
    <a href = 'test.php' >返回</a>
    <?php
    // Test CVS
    if($_FILES['file']['type'] == 'application/vnd.ms-excel')
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
    }
    }
    else{
    echo 'Error: ' . 'FILE TYPE IS NOT TXT DOCUMENT!' . "<br />";
    }

    $tArray = explode('.', $_FILES["file"]["name"]);
    $cachedir = 'c:\testWeb\public\phpExcelReader\cache\\';
    $cacheFileName = 'c:\testWeb\public\phpExcelReader\cache\\'. $tArray[0].'.txt';

    error_reporting(0);
    require_once 'Excel/reader.php';

    // ExcelFile($filename, $encoding);
    $data = new Spreadsheet_Excel_Reader();

    // Set output Encoding.
    $data->setOutputEncoding('CP936');

    $data->read('xls\\'. $_FILES["file"]["name"]);
    $sheetArray['tbody'] = array();
    $sheetArray['thead'] = array();
    $tempArray = array();

    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
    $tempArray = array();
    for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
    if($i == 1){
    $sheetArray['thead'][0][]['val'] = $data->sheets[0]['cells'][$i][$j] == '--' ? '-' : $data->sheets[0]['cells'][$i][$j];
    }
    else{
    $tempArray[] = $data->sheets[0]['cells'][$i][$j] == '--' ? '-' : $data->sheets[0]['cells'][$i][$j];
    }
    }
    $i == 1 ? '' : $sheetArray['tbody'][] = $tempArray;
    }
    var_dump($sheetArray);

    if(!is_dir($cachedir))
    mkdir($cachedir);
    $fp = fopen($cacheFileName, 'w');
    if($fp){
    echo $cacheFileName . '<br/>';
    fwrite($fp, serialize($sheetArray));
    fclose($fp);
    }
    ?>
    最后在运行文件的cache下就会有缓存文件了
  • 相关阅读:
    6.11 考试修改+总结
    6.10 考试修改+总结+颓废记
    我们都一样
    【HDU 5730】Shell Necklace
    【SPOJ 8093】Sevenk Love Oimaster
    【BZOJ 3238】【AHOI 2013】差异
    【UOJ #131】【NOI 2015】品酒大会
    【SPOJ 220】Relevant Phrases of Annihilation
    【POJ 3177】Redundant Paths
    【POJ 2186】Popular Cows
  • 原文地址:https://www.cnblogs.com/zcy_soft/p/2077904.html
Copyright © 2011-2022 走看看