Tuz*_*zki 6 php mysql phpexcel
我正在尝试从下面的代码中更改导出到xlsx的所有工作表的名称.导出工作正常,但所有工作表的标题只是工作表1,2,3等
我想从我的php数据库中的"asset_name"列中获取名称.
例如,worksheet1-> aaa
我希望它成为:asset1-> aaa
干杯,
/** Query 1.0 */
$query = "SELECT * FROM asset_register";
$query2 = "SELECT asset_name FROM asset_register";
if ($result = mysql_query($query) or die(mysql_error())) {
/** Create a new PHPExcel object 1.0 */
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Data');
}
/** Loop through the result set */
$rowNumber = 1; //start in row 1
$objPHPExcel->removeSheetByIndex(0);
while ($row = mysql_fetch_row($result)) {
$newsheet = $objPHPExcel->createSheet();
$col = 'A'; // start at column A
$objPHPExcel->getActiveSheet()->setTitle(mysql_query($query2));
foreach($row as $cell) {
$newsheet->setCellValue($col.$rowNumber,$cell);
$col++;
}
Run Code Online (Sandbox Code Playgroud)
mysql_query($query2)
Run Code Online (Sandbox Code Playgroud)
返回结果集,因此您尝试将工作表标签设置为资源,而不是字符串。您需要从结果集中获取行以读取字符串值,asset_column如所有 PHP 的 MySQL 文档中所示
编辑
要为每个资产名称提供一个新表,请使用以下内容:
$prevAssetName = NULL;
$objPHPExcel->removeSheetByIndex(0);
while ($row = mysql_fetch_row($result)) {
if ($row['asset_name'] !== $prevAssetName) {
$newsheet = $objPHPExcel->createSheet();
$newsheet->setTitle($row['asset_name']);
$prevAssetName = $row['asset_name'];
}
$col = 'A'; // start at column A
foreach($row as $cell) {
$newsheet->setCellValue($col.$rowNumber,$cell);
$col++;
}
}
Run Code Online (Sandbox Code Playgroud)