将数据库导出到 xls,价格格式为美元符号和逗号

发布于 2024-11-15 01:09:44 字数 1568 浏览 3 评论 0原文

我将我的 mysql 数据库导出到 Excel,一切正常,但我希望价格字段在 Excel 电子表格中显示为美元符号和逗号。

这是我的代码:

$pubtable = $_GET["publication"];
$addate = $_GET["adDateHidden"];

$export = mysql_query("SELECT * FROM $pubtable WHERE addate = '$addate' ORDER BY price DESC") or die ("Sql error : " . mysql_error());

$fields = mysql_num_fields($export);

for($i = 0; $i < $fields; $i++){
    $header .= mysql_field_name($export , $i). "\t";
}

while($row = mysql_fetch_row($export)){
    $line = ''; 
    foreach($row as $value)    {   
        if(!isset($value) || trim($value) == "")        {
            $value = "\t";
        } else {
            $value = str_replace('"' , '""' , $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line). "\n";
}
$data = str_replace("\r" , "" , $data);

if(trim($data) == ""){
    $data = "\n(0)Records Found!\n";                        
}

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$pubtable."_".$addate.".xls");
header("Pragma: no-cache");
header("Expires: 0");
header ('Content-Transfer-Encoding: binary');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
print "$header\n$data";

我尝试这样做,

$export = mysql_query("SELECT  CONCAT('$', FORMAT(price, 2)) as fieldalias * FROM $pubtable WHERE addate = '$addate' ORDER BY fieldalias DESC") or die ("Sql error : " . mysql_error());

正确格式化它,但它只输出价格字段而没有其他内容。

Im exporting my mysql database to excel, and everything is working, but I want the price field to display with a dollar sign and commas within the excel spreadsheet.

Here is my code:

$pubtable = $_GET["publication"];
$addate = $_GET["adDateHidden"];

$export = mysql_query("SELECT * FROM $pubtable WHERE addate = '$addate' ORDER BY price DESC") or die ("Sql error : " . mysql_error());

$fields = mysql_num_fields($export);

for($i = 0; $i < $fields; $i++){
    $header .= mysql_field_name($export , $i). "\t";
}

while($row = mysql_fetch_row($export)){
    $line = ''; 
    foreach($row as $value)    {   
        if(!isset($value) || trim($value) == "")        {
            $value = "\t";
        } else {
            $value = str_replace('"' , '""' , $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line). "\n";
}
$data = str_replace("\r" , "" , $data);

if(trim($data) == ""){
    $data = "\n(0)Records Found!\n";                        
}

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$pubtable."_".$addate.".xls");
header("Pragma: no-cache");
header("Expires: 0");
header ('Content-Transfer-Encoding: binary');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
print "$header\n$data";

I tried doing this

$export = mysql_query("SELECT  CONCAT('

this formats it correctly but it only outputs the price field and nothing else.

, FORMAT(price, 2)) as fieldalias * FROM $pubtable WHERE addate = '$addate' ORDER BY fieldalias DESC") or die ("Sql error : " . mysql_error());

this formats it correctly but it only outputs the price field and nothing else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2024-11-22 01:09:44

从技术上讲,您并不是在生成 Excel 电子表格。您正在生成一个扩展名为 .xls 的 CSV 文件。 CSV 没有添加格式的机制,因为它只是纯文本。您可以让 MySQL 和/或 PHP 将数字格式化为看起来不错的货币值,但随后您就会破坏它作为数字的存在。它将是一个曾经是数字的字符串。

您应该使用 PHPExcel 生成一个实际的 Excel 文件,您可以在其中添加 Excel 支持的所有常用功能,包括颜色和公式。

Technically, you're not producing an Excel spreadsheet. You're producing a CSV file with a .xls extension. CSV has no mechanism for adding formatting, because it's just plain text. You can have MySQL and/or PHP format a number into what looks like a nice currency value, but then you're destroying its existence as a number. It'll be a string-that-used-to-be-a-number.

You should use PHPExcel to produce an ACTUAL Excel file, into which you can add all the usual goodies that Excel supports, including colors and formulae.

一枫情书 2024-11-22 01:09:44

您的代码正在生成一个带有 XLS 扩展名的文本文件(CSV 格式)。然后,在安装了Excel的Windows下,尽管内容只是文本文件,但Excel可能会自动打开它。
这种方法不能产生任何有风格的数据。

我建议您使用 OpenTBS 轻松构建您的 Excel 文件。 OpenTBS 是一个 PHP 类,可以使用模板构建真正的 XLSX、DOCX、ODT、ODS 等。您只需使用 Excel 设计模板,然后将其与 PHP 下的数据合并,您就可以直接下载新的 XSLS,或者作为文件保存在服务器上。

Your code is generating a text file (CSV formated) with a XLS extension. Then, under Windows with an Excel installed, it may be automatically opened by Excel despite the content is only text file.
This method cannot produce any data with style.

I suggest that you use OpenTBS to easily build your Excel file. OpenTBS is a PHP class which can build real XLSX, DOCX, ODT, ODS and more... using templates. You just design the template with Excel and then merge it with the data under PHP and you have a new XSLS directly for download, or as a files saved on the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文