dsq2015 2013-10-13 23:48
浏览 326
已采纳

PHPEXCEL为所有工作表设置标题

I'm trying to change the name of all the worksheets exported into xlsx from my code below. The export works fine, but the title of all the worksheets are just worksheet 1, 2, 3, etc

I want to grab the names from the column "asset_name" from my php database.

Eg, worksheet1->aaa

i want it to become: asset1-> aaa

Cheers,

/** 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++;
    }
  • 写回答

2条回答 默认 最新

  • doubaomao9304 2013-10-13 23:54
    关注
    mysql_query($query2)
    

    returns a resultset, so you're trying to set the worksheet label to a resource, not to a string. You need to fetch the row from the resultset to read the string value for asset_column as shown in all the MySQL documentation for PHP

    EDIT

    To give yourself a new sheet for each asset name, use something like:

    $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++;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入