所以我使用PHPExcel
库来从我的PSQL
数据库创建动态报告。
我将首先展示我在做什么:
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Koordynator")
->setLastModifiedBy("SystemTest")
->setTitle("GeneratedTest")
->setKeywords("Test")
->setCategory("TestCategory");
$db = ConnectionFactory::CreateConnection();
$systemArray= array (
"systems which are processed"=>"system",
"Persons responsible for systems"=>"person",
"Software used inside of implementation"=>"software",
"Hardware used inside of solution"=>"hardware");
$system= array ("id"=>"system_id","System name"=>"name","System price"=>"price");
$person= array("Firstname of admin"=>"firstname","Lastname of admin"=>"lastname");
$software=array("Software id"=>"id, "System Name"=>"name", "System License"=>"license");
$hardware=array("id"=>"id","Graphic"=>"gpu","Procesor"=>"cpu","Memory"=>"ram");
在这个地方,我将描述这些数组的用途。第一个$systemArray
包含我的数据库中的表列表,其中"key"=>"value"
代表key
- 工作表名称和A1单元格,value
是表名。
数组$system, $person $software and $hardware
是包含每个数组中列的定义的数组,其中"key"=>"value"
代表key
- 表中列的名称,value
表中列的名称。
这就是我创建XLS
foreach ($systemArray as $tableKey=>$tableName) {
//$q = "select * from $tableName where date >= '2013-11-01' and date <= '2013-11-30' order by date asc;";
$i=0;
foreach($$tableName as $key=>$value){
if ($i==0)
$selectValue = $value ;
else
$selectValue = $selectValue." , ".$value ;
$i++;
}
$q= "select $selectValue, date from $tableName where date >= '2013-11-01' and date <= '2013-11-30' order by date asc";
$i=3;
$s=0;
$aa=preg_replace('/[^a-zA-Z0-9_ %\[\]\[\]\.\(\)%&-]/s', '', $tableKey);
//echo "klucz: $tableKey <br> a teraz $aa<br>";
$objPHPExcel->createSheet($s);
$objPHPExcel->getActiveSheet()->setTitle(substr($aa,0,30));
$letters = range('A','Z');
foreach ($db->query($q)->fetchall(PDO::FETCH_ASSOC) as $row){
$k=1;
//$objPHPExcel->getActiveSheet()->setTitle('1');
$objPHPExcel->setActiveSheetIndex($s)->setCellValue('A1', $tableKey);
foreach($$tableName as $columnkey=>$columnValue){
if ($i == 3){
$objPHPExcel->setActiveSheetIndex($s)->setCellValue('A'.($i-1), 'Data');
if ( $k <25)
$objPHPExcel->setActiveSheetIndex($s)->setCellValue($letters[$k].($i-1), $columnkey);
else
$objPHPExcel->setActiveSheetIndex($s)->setCellValue('A'.$letters[($k-25)].($i-1), $columnkey);
}
if ($k<25)
$objPHPExcel->setActiveSheetIndex($s)->setCellValue($letters[$k].$i, $row[$columnValue]);
else
$objPHPExcel->setActiveSheetIndex($s)->setCellValue('A'.$letters[($k-25)].$i, $row[$columnValue]);
$objPHPExcel->setActiveSheetIndex($s)->setCellValue('A'.$i, $row['date']);
//echo $letters[$k].$i." $columnName<br>";
$k++;
}
//$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.$i, $row['date']);
$i++;
}
$s++;
}
$objPHPExcel->setActiveSheetIndex(0);
我遇到的问题是,sheets
中XLS
的某种方式显示的顺序错误,而我的意思是它们不符合我将它们放入$systemArray
的顺序另一件事是他们以奇怪的方式命名。第一个名为Worksheet 1
,其余的都可以,但在1
等名称的末尾添加了systems which are processed 1
。
有人能指出我在做错的地方吗?