2014-12-27 1 views
0

오라클 쿼리를 실행하는 응용 프로그램을 작성하고 Excel 형식으로 데이터를 내 보냅니다. 내가 직면하고있는 문제는 Excel 파일의 Tran_code 열이 00입니다. 내 코드가 실행되고 Excel을 생성, 그것은 하나의 0 대신 인쇄 할 때 배열의 셀 값을 설정 00PHP를 사용하여 Excel에서 열 속성 설정

:

$_SESSION['report_values'][$sno][19]= "'00"; //Setting cell value in an array 

Excel로 헤더와 값을 설정하면 파일

include_once("class.export_excel.php"); 
$excel_obj->setHeadersAndValues($_SESSION['report_header'],$_SESSION['report_values']); // 
$excel_obj->GenerateExcelFile(); 

class.export_excel.php

<?php 
class ExportExcel 
{ 
//variable of the class 
var $titles=array(); 
var $all_values=array(); 
var $filename; 

//functions of the class 
function ExportExcel($f_name) //constructor 
{ 
    $this->filename=$f_name; 
} 
function setHeadersAndValues($hdrs,$all_vals) //set headers and query 
{ 

//$this->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT); 
    $this->titles=$hdrs; 
    $this->all_values=$all_vals; 
} 
function GenerateExcelFile() //function to generate excel file 
{ 

    foreach ($this->titles as $title_val) 
    { 
     $header .= $title_val."\t"; 

    } 
    for($i=0;$i<sizeof($this->all_values);$i++) 
    { 
     $line = ''; 
     foreach($this->all_values[$i] as $value) 
     { 
      if ((!isset($value)) OR ($value == "")) 
      { 
       $value = "\t"; 
      } //end of if 
      else 
      { 

       $value = str_replace('"', '""', $value); 
       $value = '"' . $value . '"' . "\t"; 
      } //end of else 
      $line .= $value; 
     } //end of foreach 
     $data .= trim($line)."\n"; 
    }//end of the while 
    $data = str_replace("\r", "", $data); 
    if ($data == "") 
    { 
     $data = "\n(0) Records Found!\n"; 
    } 
    //echo $data; 
    header("Content-type: application/vnd.ms-excel"); 
    header("Content-Disposition: attachment; filename=$this->filename"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    print "$header\n$data"; 


} 

} 
?> 

답변

0

엑셀이 TSV 파일을 읽을 때 jsut 파일을 출력하기 때문에 PHPExcel과 같은 librayr를 사용해야 할 것입니다. 그것은 그래서 PHPExcel 사용하는 단일 0

로 포맷합니다 :

그래서이 setCellValueExplicit* 방법을 사용하는 것이 아니라 당신이 세포의 특정 그룹에있는 데이터의 형식을 설정할 수 있음에 유의해야한다
class ExportExcel 
{ 
    //variable of the class 
    protected $titles=array(); 
    protected $all_values=array(); 
    protected $filename; 
    protected $dataTypes; 

    //functions of the class 
    function ExportExcel($f_name) //constructor 
    { 
     $this->filename=$f_name; 
    } 

    function setHeadersAndValues($hdrs,$all_vals, $dataTypes = array()) 
    { 

     $this->titles=$hdrs; 
     $this->all_values=$all_vals; 
     $this->dataTypes = $dataTypes; 
    } 

    function getDataType($columnIndex) { 
     // the data types tou provide should match up with 
     // the constants on PHPExcel_Cell_DataType 
     // https://github.com/PHPOffice/PHPExcel/blob/develop/Classes/PHPExcel/Cell/DataType.php 
     if (isset($this->dataTypes[$columnIndex]) && !empty($this->dataTypes[$columnIndex])) { 
      return $this->dataTypes[$columnIndex] 
     } 

     return null; 
    } 

    function GenerateExcelFile() //function to generate excel file 
    { 


     $excel = new PHPExcel(); 
     $worksheet = $excel->createSheet(); 

     // we will check for records here 
     if (count($this->all_values)) { 

      foreach (array_values($this->titles) as $col => $title_val) 
      { 
       // note values will be converted here so if you have you 00 
       // in a header youll need to use the Explicit version like i do 
       // below 
       $worksheet->setCellValueByColumnAndRow($col, 1, $title_val); 
      } 

      $nbValues = count($this->all_values); 
      for($i=0; $i < $nbValues; $i++) 
      { 
       // for some reason rows are 1 indexed instead of 0 
       // and we already used one row for the headers so 
       // we add 2 to the increment to get the proper row index 
       $row = $i+2; 

       foreach(array_values($this->all_values[$i]) as $col => $value) 
       { 
        // note the use of the new method getDataType on your class 
        if (null !== ($type = $this->getDataType($col))) { 
         $worksheet->setCellValueExplicitByColumnAndRow($col, $row, $value, $type); 
        } else { 
         // type was null so let excel figure it out 
         $worksheet->setCellValueByColumnAndRow($col, $row, $value); 
        } 
       } //end of foreach 

      } //end of the for 

      // we need to save it first so we can read the file data out to the user 
      $writer = PHPExcel_IOFactory::createWriter($excel, "Excel5"); 
      $tmpfile = tempnam(sys_get_temp_dir(), 'report'); 
      $writer->save($tmpfile); 

      $data = file_get_contents($tmpfile); 
     } else { 
      $data = "\n(0) Records Found!\n"; 
     } 


     header("Content-type: application/vnd.ms-excel"); 
     header("Content-Disposition: attachment; filename=$this->filename"); 
     header("Pragma: no-cache"); 
     header("Expires: 0"); 
     print "$data"; 
    } 
} 

, 그러나 나는 그것을 어떻게하는지 손에서 기억하지 못했고, 처음에는 그것을 문서에서 찾지 못했습니다. 그것은 갈 수있는 더 좋은 방법일지도 모르지만, 어느 쪽이든은 일해야합니다.

+0

작동하지 않습니다! – user3202144

+0

특정 오류/경고 ??? 아니면 파일을 열었을 때 여전히 '0'으로 채닝하고 있다고 말하는 것입니까? 그리고 어떤 데이터 유형을 사용 했습니까? – prodigitalson

+0

오류가 발생하지 않습니다. 파일이 생성되지 않습니다. – user3202144