使用 PHP 在 Excel 中设置列属性

问题描述 投票:0回答:1

我正在编写一个运行 Oracle 查询并以 Excel 格式导出数据的应用程序。我面临的问题是Excel文件中名为Tran_code的列是

00
。当我的代码运行并生成 Excel 时,它打印单个 0 而不是 00。

设置数组中的单元格值:

$_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";  


}

}
?>
php excel oracle php-5.3
1个回答
0
投票

我很确定您需要使用 PHPExcel 这样的库,因为您当前只是输出 TSV 文件。因此,当 Excel 读取它时,它会尝试根据内容格式化列,并且由于它都是数字,因此它会将其格式化为单个 0。

所以使用 PHPExcel:

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 you 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 you'll 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";  
    }
}

因此应该注意,您可以在给定的一组单元格上设置数据类型,而不是使用

setCellValueExplicit*
方法,但我不记得如何做到这一点,并且乍一看没有找到它文档。这可能是一个更好的方法,但两者都应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.