简体   繁体   English

导出为 CSV 百分比格式

[英]export to CSV percentage formatting

Having an issue with the formatting of the percentages in an exported CSV file.导出的 CSV 文件中的百分比格式存在问题。

Here is the process that grabs the data and sends it back to JQuery to be exported:这是抓取数据并将其发送回 JQuery 以进行导出的过程:

<?php
  include('include/sessions.php');

  $date_begin = date("Y-m-d", strtotime($_POST['date_begin']));
  $date_end = date("Y-m-d", strtotime($_POST['date_end']));

  $sql = "SELECT table1.services, table1.port
          sum(case when TIMESTAMPDIFF(HOUR,table2.berth,table1.performance) <= -12 then 0 else 1 end) ON_TIME,
          count(table1.port) TTL_CALLS,
          round((sum(case when TIMESTAMPDIFF(HOUR,table2.berth,table1.performance) <= -12 then 0 else 1 end)/count(table1.port))*100,1) as ON_TIME_PCT
          FROM
            table1
          Left Outer Join table2 ON table1.voyage = table2.voyage AND table1.port = table2.port
          WHERE
            date( table1.performance ) >= '$date_begin' AND
            date( table1.performance ) <= '$date_end'";

  $query = mysqli_query($dbc, $sql) or die(mysqli_error($dbc));

  $out = array();
  while($row = $query->fetch_assoc()){
    $out[] = $row;
  }
  echo json_encode($out);
  
?>

The field I am referring to is ON_TIME_PCT.我指的字段是 ON_TIME_PCT。

Using the above, when running the query in the dB itself, the results of the query for the field in reference can vary between 100.0 or 50.0.使用上述方法,在 dB 本身中运行查询时,对引用字段的查询结果可以在 100.0 或 50.0 之间变化。

When the data is exported, the cells initially look like 100 or 50.导出数据时,单元格最初看起来像 100 或 50。

When changing the format in Excel to show the full percentage, the numbers look like 10000% or 5000%.当更改 Excel 中的格式以显示完整百分比时,数字看起来像 10000% 或 5000%。

I am not sure why that is happening.我不确定为什么会这样。

Here is the code that I am using to export to CSV:这是我用来导出到 CSV 的代码:

$('#byPortService').on('click', function(){

  var date_begin = $('#date_begin').val();
  var date_end = $('#date_end').val();

  $.post('api/byPortService.php', {date_begin:date_begin,date_end:date_end}, function(data){
    JSONToCSVConvertor(data, "BY PORT SERVICE REPORT - DATE RANGE: "+date_begin+" - "+date_end+"", true);
  });
});

Here is the JSONToCSVConvertor function:这是 JSONToCSVConvertor function:

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel){
  var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
  var CSV = '';
  CSV += ReportTitle + '\r\n\n';
  if (ShowLabel) {
    var row = "";
    for (var index in arrData[0]) {
      row += index + ',';
    }
    row = row.slice(0, -1);
    CSV += row + '\r\n';
  }
  for (var i = 0; i < arrData.length; i++) {
    var row = "";
    for (var index in arrData[i]) {
      row += '"' + arrData[i][index] + '",';
    }
    row.slice(0, row.length - 1);
    CSV += row + '\r\n';
   }
   if (CSV == '') {
     alert("Invalid data");
     retirn;
   }
   var fileName = ReportTitle.replace(/ /g,"_");
   var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
   var link = document.createElement("a");
   link.href = uri;
   link.style = "visibility:hidden";
   link.download = fileName + ".csv";
   document.body.appendChild(link);
   link.click();
   document.body.removeChild(link);
}

Is there any way that I can export the percentages so that they retain the decimal?有什么方法可以导出百分比以保留小数点?

I don't want the user to have to manually do it on the Excel side.我不希望用户必须在 Excel 端手动执行此操作。

Like mentionned in comments, make sure the values are between 0 and 1 .就像评论中提到的那样,确保值在01之间。

To keep a certain amount of decimal in PHP, use number_format()要在 PHP 中保留一定数量的小数,请使用number_format()

<?php
$number = 0.5;
formatted number = number_format($number, 2);  // 0.50
?>

I was able to solve the problem by changing this line:我能够通过更改此行来解决问题:

round((sum(case when TIMESTAMPDIFF(HOUR,table2.berth,table1.performance) <= -12 then 0 else 1 end)/count(table1.port))*100,1) as ON_TIME_PCT

To this:对此:

concat(round((sum(case when TIMESTAMPDIFF(HOUR,table2.berth,table1.performance) <= -12 then 0 else 1 end)/count(table1.port))*100,0),'%') as ON_TIME_PCT

And now the fields are showing 100% or 50% even after formatting Excel.现在,即使在格式化 Excel 之后,这些字段也显示 100% 或 50%。

Thank you all for your help.谢谢大家的帮助。 Upvotes for everyone.为大家点赞。

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

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