【问题标题】:Excel Not Saved as Numeric Format (MySQL to Excel)Excel 未保存为数字格式(MySQL 到 Excel)
【发布时间】:2014-03-20 02:16:39
【问题描述】:

我有一个脚本可以让我将数据库从 MySQL 转换为 Excel .xls 格式,很成功,

这里是代码

<?php

// DB TABLE Exporter
//
// How to use:
//
// Place this file in a safe place, edit the info just below here
// browse to the file, enjoy!

// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO
     $cdate = date("Y-m-d");
     $dbhost  = "localhost";
     $dbuser  = "-";
     $dbpass  = "-";
     $dbname  = "-";
     $dbtable = "-";
     $filename = "C:\xxx";


// END CHANGING STUFF


// first thing that we are going to do is make some functions for writing out
// and excel file. These functions do some hex writing and to be honest I got 
// them from some where else but hey it works so I am not going to question it 
// just reuse


// This one makes the beginning of the xls file
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
    return;
}

// This one makes the end of the xls file
function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

// this will write text in the cell you specify
function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
    return;
}



// make the connection an DB query
$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname );
$q = "SELECT * FROM ".$dbtable." ";
$qr = mysql_query( $q ) or die( mysql_error() );

//start the object
 ob_start();

// start the file
xlsBOF();

// these will be used for keeping things in order.
$col = 0;
$row = 0;

// This tells us that we are on the first row
$first = true;

while( $qrow = mysql_fetch_assoc( $qr ) )
{
    // Ok we are on the first row
    // lets make some headers of sorts
    if( $first )
    {
        // di comment karena ini ngasih label tabelnya, sepertinya nggak butuh
        //foreach( $qrow as $k => $v )
        //{
        //   // take the key and make label
        //   // make it uppper case and replace _ with ' '
        //   xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
        //   $col++;
        //}

        // prepare for the first real data row
        $col = 0;
        $row = 0;//$row++; // nyoba
        $first = false;
    }

    // go through the data
    foreach( $qrow as $k => $v )
    {

        // write it out
        xlsWriteLabel( $row, $col, $v );
        $col++;
    }

    // reset col and goto next row
    $col = 0;
    $row++;

}

xlsEOF();

//write the contents of the object to a file
file_put_contents($filename, ob_get_clean());

?>

此代码生成了一个 .xls 文件。我使用 Matlab 通过 readxls 函数读取我的 .xls 文件,但它没有将 .xls 文件中的值识别为数字数据,因此我在 matlab 上的脚本无法通过读取我的 xls 文件来生成矩阵。我必须在 excel 中手动转换它,值附近有一些弹出窗口可以让我将其转换为数字

【问题讨论】:

    标签: mysql excel matlab matrix


    【解决方案1】:

    如果您的目标是从 MySQL 获取数据并将其放入 Matlab,那么从 Matlab 直接连接到 MySQL 可能比通过 xls 格式发送数据更容易。

    http://www.mathworks.com/help/database/ug/database.fetch.html

    【讨论】:

    • 谢谢!,这是一个更好的选择,我稍后会尝试,我已经解决了这个问题,但使用不同的方法,将其转换为 .csv 文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多