如何使用 php 将 excel 数据合二为一 excel?
How to customize the excel data's with two sheets in one excel with php?
我正在使用 codeigniter 框架,在此我正在创建 excel 包含 MySQL 数据的文件。我需要将 header table(即第一个循环)数据创建到第一个 sheet,详细 table(即第二个循环)数据到第二个 sheet。下面我给出了我的代码,这个代码与下一个下一个数据在相同的 sheet 中生成。任何人都可以提供一些想法来解决这个问题。
$out = '"S.no","HeaderID","InvoiceID","InvoiceNo","doc_no","InvoiceDate","PartyCode","doc_type","CurrencyID","Remarks","loc_amt","doc_amt"'."\r\n";
$i=1;
foreach($export_list as $d)
{
$out .= $i.',"'.$d->slsid.'","'.'0'.'","'.$d->reference_no.'","'.' '.'","'.$d->date.'","'.$d->customer_code.'","'.' '.'","'.' '.'","'.$d->internal_note.'","'.'0'.'","'.$d->total.'"'."\r\n";
$i++;
}
$out .= '"S.no","HeaderID","DetailID","ProductID","Description","Qty","loc_amt","doc_amt"'."\r\n";
$i=1;
foreach($export_detail as $d)
{
$out .= $i.',"'.$d->sale_id.'","'.$d->id.'","'.$d->product_code.'","'.' '.'","'.$d->quantity.'","'.'0'.'","'.$d->gross_total.'"'."\r\n";
$i++;
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=Users.xls');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
exit;
提前致谢。
是的,必须为 codeigniter 下载 PHPExcel 库。
我有一些示例代码可以帮助您使用 PHPExcel。
function test_excel()
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('test worksheet');
$this->excel->getActiveSheet()->setCellValue('A1', 'User id');
$this->excel->getActiveSheet()->setCellValue('B1', 'User name');
$this->excel->getActiveSheet()->setCellValue('C1', 'Email');
$this->excel->getActiveSheet()->setCellValue('D1', 'Status');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM users");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['USER_ID']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['FIRST_NAME']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['USER_EMAIL']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['USER_TYPE_ID']);
$k++;
}
$filename='just_some_random_name.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}
希望对您有所帮助。
嗨@Jagan Akash 检查这个,
public function test_excel()
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('test worksheet');
$this->excel->getActiveSheet()->setCellValue('A1', 'User id');
$this->excel->getActiveSheet()->setCellValue('B1', 'User name');
$this->excel->getActiveSheet()->setCellValue('C1', 'Email');
$this->excel->getActiveSheet()->setCellValue('D1', 'Status');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM user limit 2");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);
$k++;
}
$init_cnt = $this->excel->getSheetCount();
$this->excel->createSheet($init_cnt);
$this->excel->setActiveSheetIndex($init_cnt);
$this->excel->getActiveSheet()->setTitle('test 1');
$this->excel->getActiveSheet()->setCellValue('A1', '1');
$this->excel->getActiveSheet()->setCellValue('B1', '2');
$this->excel->getActiveSheet()->setCellValue('C1', '3');
$this->excel->getActiveSheet()->setCellValue('D1', '4');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM user limit 2,2");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);
$k++;
}
$init_cnt = $this->excel->getSheetCount();
$this->excel->createSheet($init_cnt);
$this->excel->setActiveSheetIndex($init_cnt);
$this->excel->getActiveSheet()->setTitle('test 2');
$this->excel->getActiveSheet()->setCellValue('A1', '12');
$this->excel->getActiveSheet()->setCellValue('B1', '22');
$this->excel->getActiveSheet()->setCellValue('C1', '32');
$this->excel->getActiveSheet()->setCellValue('D1', '42');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM user limit 4,2");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);
$k++;
}
$this->excel->setActiveSheetIndex(0);
$filename='just_some_random_name.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}
这将回答所有问题。
我正在使用 codeigniter 框架,在此我正在创建 excel 包含 MySQL 数据的文件。我需要将 header table(即第一个循环)数据创建到第一个 sheet,详细 table(即第二个循环)数据到第二个 sheet。下面我给出了我的代码,这个代码与下一个下一个数据在相同的 sheet 中生成。任何人都可以提供一些想法来解决这个问题。
$out = '"S.no","HeaderID","InvoiceID","InvoiceNo","doc_no","InvoiceDate","PartyCode","doc_type","CurrencyID","Remarks","loc_amt","doc_amt"'."\r\n";
$i=1;
foreach($export_list as $d)
{
$out .= $i.',"'.$d->slsid.'","'.'0'.'","'.$d->reference_no.'","'.' '.'","'.$d->date.'","'.$d->customer_code.'","'.' '.'","'.' '.'","'.$d->internal_note.'","'.'0'.'","'.$d->total.'"'."\r\n";
$i++;
}
$out .= '"S.no","HeaderID","DetailID","ProductID","Description","Qty","loc_amt","doc_amt"'."\r\n";
$i=1;
foreach($export_detail as $d)
{
$out .= $i.',"'.$d->sale_id.'","'.$d->id.'","'.$d->product_code.'","'.' '.'","'.$d->quantity.'","'.'0'.'","'.$d->gross_total.'"'."\r\n";
$i++;
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=Users.xls');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
exit;
提前致谢。
是的,必须为 codeigniter 下载 PHPExcel 库。
我有一些示例代码可以帮助您使用 PHPExcel。
function test_excel()
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('test worksheet');
$this->excel->getActiveSheet()->setCellValue('A1', 'User id');
$this->excel->getActiveSheet()->setCellValue('B1', 'User name');
$this->excel->getActiveSheet()->setCellValue('C1', 'Email');
$this->excel->getActiveSheet()->setCellValue('D1', 'Status');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM users");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['USER_ID']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['FIRST_NAME']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['USER_EMAIL']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['USER_TYPE_ID']);
$k++;
}
$filename='just_some_random_name.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}
希望对您有所帮助。
嗨@Jagan Akash 检查这个,
public function test_excel()
{
$this->load->library('excel');
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->setTitle('test worksheet');
$this->excel->getActiveSheet()->setCellValue('A1', 'User id');
$this->excel->getActiveSheet()->setCellValue('B1', 'User name');
$this->excel->getActiveSheet()->setCellValue('C1', 'Email');
$this->excel->getActiveSheet()->setCellValue('D1', 'Status');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM user limit 2");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);
$k++;
}
$init_cnt = $this->excel->getSheetCount();
$this->excel->createSheet($init_cnt);
$this->excel->setActiveSheetIndex($init_cnt);
$this->excel->getActiveSheet()->setTitle('test 1');
$this->excel->getActiveSheet()->setCellValue('A1', '1');
$this->excel->getActiveSheet()->setCellValue('B1', '2');
$this->excel->getActiveSheet()->setCellValue('C1', '3');
$this->excel->getActiveSheet()->setCellValue('D1', '4');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM user limit 2,2");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);
$k++;
}
$init_cnt = $this->excel->getSheetCount();
$this->excel->createSheet($init_cnt);
$this->excel->setActiveSheetIndex($init_cnt);
$this->excel->getActiveSheet()->setTitle('test 2');
$this->excel->getActiveSheet()->setCellValue('A1', '12');
$this->excel->getActiveSheet()->setCellValue('B1', '22');
$this->excel->getActiveSheet()->setCellValue('C1', '32');
$this->excel->getActiveSheet()->setCellValue('D1', '42');
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$this->excel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$query = $this->db->query("SELECT * FROM user limit 4,2");
$k=2;
foreach($query->result_array() as $row)
{
$this->excel->getActiveSheet()->setCellValue("A".$k, $row['user_id']);
$this->excel->getActiveSheet()->setCellValue("B".$k, $row['first_name']);
$this->excel->getActiveSheet()->setCellValue("C".$k, $row['last_name']);
$this->excel->getActiveSheet()->setCellValue("D".$k, $row['user_email']);
$k++;
}
$this->excel->setActiveSheetIndex(0);
$filename='just_some_random_name.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}
这将回答所有问题。