我使用下面的代码使用phpexcel将两个mysql表中的列导出到excel文件。
我通过运行两次查询来实现它。有人可以通过只运行一个查询并使用LIKE和UNION来帮助我完成这项工作。
谢谢!
编辑:根据要求,表格的数量会增加,但会有相同的名称...所以必须找到一个解决方案,首先使用LIKE选择数据库中的所有表格,然后查询所有表中的列数据。然后将这些数据并排输出到Excel工作表。类似于col A1-AJ上的表1数据,选项卡;来自AL-AR的e 2数据等等。这是我的代码:
<?php
// connection with the database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "redhat";
$dbname = "was";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
// require the PHPExcel file
require 'phpexcel/Classes/PHPExcel.php';
$query = "SELECT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos1report";
$query1 = "SELECT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos2report";
$headings = array('Service Date','Rec. Date','DOS to Rec. Lag','Cases Rec.','scan Date','Cases Entered','Cases Pending','Cases Count Diff','Entry Date','Rec. to Entry Lag');
if ($result = mysql_query($query) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Report');
$rowNumber = 8;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
// Loop through the result set
$rowNumber = 9;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
} else {
echo 'a problem has occurred... no data retrieved from the database';
}
if ($result1 = mysql_query($query1) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet();
$rowNumber = 8;
$col = 'L';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
// Loop through the result set
$rowNumber = 9;
while ($row = mysql_fetch_row($result)) {
$col = 'L';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
} else {
echo 'a problem has occurred... no data retrieved from the database';
}
// Freeze pane so that the heading line won't scroll
$objPHPExcel->getActiveSheet()->freezePane('A2');
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
?>
所有表格都有相同的列,但数据不同
答案 0 :(得分:3)
使用联合(阅读https://dev.mysql.com/doc/refman/5.0/en/union.html)
尝试以下操作$query = "
SELECT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos1report
union
SELECT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos2report
";
编辑:鉴于您的更新声明“将这些数据并排输出到Excel工作表。如表A1中关于col A1-AJ的数据,选项卡;来自AL-AR的e 2数据,等等”一个“SELECT DISTINCT ... LEFT JOIN”而不是一个联合...你可以改变名称来反映这个表,但除非你在WHERE子句中添加条件,否则这不是必需的......
$query = "
SELECT DISTINCT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos1report
left join hos2report";
性能将是可怕的,如果你可以显示col A1到AJ中的所有数据它会更简单....你甚至可以添加一个列来指定哪个表是源如此
$query = "
SELECT 'hos1report' as tablename, servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos1report
union
SELECT 'hos2report' as tablename, servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos2report
";
答案 1 :(得分:1)
你真的应该提供一个数据模式(即使它只是一个样本),以便人们可以将它放入SQLFIDDLE并使用它。此外,拥有我们可以使用的数据的复制/粘贴版本也很有帮助。
我为您创建了一个示例架构并为您设置了小提琴。转到SQLFIDDLE SAMPLE OF YOUR DATA,您可以看到如何在那里构建架构,在右侧,您可以查询架构数据并查看网站的工作方式。
我现在将尝试解决您的新问题,即使我已经解决了您的原始问题,即使是一个upvote。
注意:您应该真正组合这两个表,只需添加一个额外的列来确定数据是来自table1还是table2。喜欢......
ALTER TABLE `hos1report` ADD `flag` tinyint(2) NOT NULL DEFAULT 0;
然后将flag
设置为1表示来自table2的任何数据,2表示来自table3的任何数据,3表示来自table4的任何数据。和table1,现在表中的原始数据,将标志列设置为默认值0。
无论如何......
如果第一个表的结果为0行,则只从第2个表中拉出。
SELECT SQL_CALC_FOUND_ROWS
`servicedate`,`recdate`,`dostoreclag`,`casesrec`,`scandate`,
`casesentered`,`casespending`,`casecountdiff`,`entrydate`,`rcvdtolag`
FROM
`hos1report`
UNION ALL
SELECT
`servicedate`,`recdate`,`dostoreclag`,`casesrec`,`scandate`,
`casesentered`,`casespending`,`casecountdiff`,`entrydate`,`rcvdtolag`
FROM
`hos2report`
WHERE
FOUND_ROWS() = 0
答案 2 :(得分:1)
SELECT t1.servicedate,t1.recdate,t2.servicedate ,t2.recdate ... FROM hos1report t1 INNER JOIN hos2report t2 ON(t1.YOUR_PRIMARY_KEY = t2.YOUR_PRIMARY_KEY)