1

我正在将数据从 db 写入 excel,并且有一个问题,如果数字有第一个符号“0”excel 不显示。例如数字0777excel 显示777,有编程问题或 excel 吗?

4

2 回答 2

2

这是一个基本示例,如何使用 PHPExcel 设置数字样式。在 Excel 中,这些值将被威胁为数字777,但将使用前导零进行格式化0770

// create a new document
$xls = new PHPExcel();

// get the first worksheet
$sheet = $xls->getSheet(0);

// write example values and set number styles of the cells
// 0000 means that values lower than 1000 will be prepended by a 0
$sheet->getCell('A1')->setValue('0777');
$sheet->getStyle('A1')->getNumberFormat()
   ->setFormatCode('0000');

$sheet->getCell('A2')->setValue('0440');
$sheet->getStyle('A2')->getNumberFormat()
   ->setFormatCode('0000');

// write file in Excel2007 format
$writer = new PHPExcel_Writer_Excel2007($xls);
$writer->save('test.xlsx');
于 2013-04-30T13:51:30.807 回答
1

我的导出干净数据的代码有一些字符串规则

ob_end_clean();
  function cleanData(&$str) { 
     if($str == 't') 
       $str = 'TRUE'; 
     if($str == 'f') 
       $str = 'FALSE'; 
     if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) { 
       $str = "'$str"; 
     } 
     if(strstr($str, '"')) 
     $str = '"' . str_replace('"', '""', $str) . '"'; 
  } 
  // filename for download 
  $filename = date("Y-m-d").".csv";
  header('Content-Type: text/csv');
  header("Content-Disposition: attachment; filename=\"$filename\""); 
  $out = fopen("php://output", 'w'); 
  $flag = false;
   //$result = $orderDetails; 
   foreach($details as $row) {
     if(!$flag) { 
       // display field/column names as first row 
       fputcsv($out, array_keys($row), ',', '"');
       $flag = true; 
     } 
     array_walk($row, 'cleanData');
     fputcsv($out, array_values($row), ',', '"'); 
   } 
    fclose($out); 
    exit();
于 2013-04-30T13:24:52.310 回答