我有这个代码清单,它将数据导出到显示为here的excel文件!,我想使用一个循环来做同样的事情,而不是硬编码,建议!
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("workbooks/" . $labref . "/" . $labref . ".xlsx");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()
->setCellValue('I3', $heading)
->setCellValue('I5', 'Standard Preparation For Assay')
->setCellValue('B7', 'Weight')
…………
//Assay Standard Preparation desired
->setCellValue('A8', 'Desired Weight')
->setCellValue('B8', $weight)
->setCellValue('C8', $vf1)
…………………..
//Other values used
->setCellValue('D22', 'Label Claim')
->setCellValue('D23', 'Tabs or Caps Average')
……..
$objPHPExcel->getActiveSheet()->setTitle($heading);
$dir = "workbooks";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("workbooks/" . $labref . "/" . $labref . ".xlsx");
}
}
答案 0 :(得分:0)
尝试这样的事情
$data = R::getAll('SELECT * FROM form'); //Im using redbean
$url = // Your url from the root dir -> the folder you wanna save to
$header = array();
$index = 0;
foreach ($data[0] as $key => $value) {
$header[$index] = $key;
$index += 1;
}
try {
$sheet = new PHPExcel();
$sheet->getProperties()->setCreator('Username')
->setLastModifiedBy('Username')
->setTitle('Title');
$sheet->getDefaultStyle()->getAlignment()->setVertical(
PHPExcel_Style_Alignment::VERTICAL_TOP);
$sheet->getDefaultStyle()->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$sheet->getDefaultStyle()->getFont()->setName('Calibri');
$sheet->getDefaultStyle()->getFont()->setSize(12);
$sheet->setActiveSheetIndex(0);
$activeSheet = $sheet->getActiveSheet();
$colHeaders = $header;
$col = 'B';
$rownum = 2;
foreach ($colHeaders as $h) {
$activeSheet->setCellValue($col . $rownum, $h);
$activeSheet->getStyle($col . $rownum)->getFont()->setBold(true);
$activeSheet->getColumnDimension($col)->setAutoSize(true);
$col++;
}
foreach ($data as $k => $v) {
$col = 'B';
$rownum++;
if ($k != count($data)) {
foreach ($v as $value) {
$activeSheet->setCellValue($col++ . $rownum, $value);
}
} else {
foreach ($v as $value) {
$rownum++;
$activeSheet->setCellValue($col++ . $rownum, $value);
}
}
}
$writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');
// The URL needs to be a absolute path from the root folder to the save folder
// time() makes sure that the file has a uniq name and allso makes it easy to
// see the leatest verson of it.
$saveObj = $url . time() . '';
$writer->save($saveObj);
exit();
} catch (Exception $e) {
$error = $e->getMessage();
// print("<pre>".print_r($error,true)."</pre>");
}
print("<pre>".print_r($error,true)."</pre>");
exit();