$app = new Com("excel.application");
$app ->Workbooks ->add();$app ->WorkSheets[1] ->Activate();
$book = $app->Workbooks(1);
$sheet = $book->Worksheets(1);
 
for($i=1; $i<10; $i++){
$sheet ->Cells[$i][1] ->Value = $i+1;
}$sheet ->Rows[1] ->Font ->Name = '宋体';
$sheet ->Rows[1] ->Font ->Color = 'Blue';
$sheet ->Rows[1] ->Font ->Bold = True;
$sheet ->Rows[1] ->Font ->UnderLine = True;$book ->SaveAs("c:\\test.xls");
$book ->Close(true);$app ->Quit();

解决方案 »

  1.   

    先感谢lizhongbin。上面的代码能实现单元格的字体的颜色的设定。。我需要的是:一个单元格内某个字或词的字体颜色设定并不是整个单元格字体的颜色
      

  2.   

    大概思路是这样子
    1.先取得这个单元格的文字
    2.截取到目标文字
    3.然后利用Excel的方法,属性设定字体颜色不晓得对不啦??麻烦各位出来指点一下。。
      

  3.   

    我明白你的意思,我以前做的COM生成Excel只有一些简单的格式设置不过网上相关资料很少,我基本上都是通过VBA改造的晚上我回去帮你试试
      

  4.   

    你可以自己试试就是打开Excel,然后将你想要得操作录制成一段宏,在看看这段宏的代码,改造一下应该就行了
      

  5.   

    <?php$app = new Com("excel.application");
    $app ->Workbooks ->add();
    $app ->WorkSheets[1] ->Activate();
    $book = $app->Workbooks(1);
    $sheet = $book->Worksheets(1);
     
    $cell=$sheet->Cells(1,1);
    //$cell->Activate;
    $cell ->Value = "aaaabbaaaaaaa";//Characters(strat,length)
    $cell ->Characters(3,6)->Font->Name = '宋体';
    $cell ->Characters(3,6)->Font->ColorIndex = 3;  //3: red$book ->SaveAs("d:\\test.xls");
    $book ->Close(true);
     
    unset($sheet);
    unset($book);
     
    $app ->Quit();
    $app = null;echo "end";
    ?>
      

  6.   

    录取宏这个前些天试过,vba的代码也弄到了
    vba的代码不熟悉,能看懂,但移植时间有问题。老兄有没有移植后的代码呀,让兄弟参考一下.
      

  7.   


    报错:PHP Parse error:  parse error, unexpected T_OBJECT_OPERATOR
    14行报错$cell ->Characters(3,6)->Font->Name = '宋体'
      

  8.   

    我把14行($cell ->Characters(3,6)->Font->Name = '宋体')注释掉了,15行报错。不晓得是不是$cell ->Characters(3,6)的问题?我装的是office2003
      

  9.   

    把14,15行都注释掉就没有问题了,test.xls就正常生成了应该说是我这边不支持$cell ->Characters(3,6)->Font->ColorIndex = 3; 会不会是office版本的问题呀?
      

  10.   

    再贴一边源程序,因为是日文系统下的,你得改动一点<?php$app = new Com("excel.application");
    $app ->Workbooks ->add();
    $app ->WorkSheets[1] ->Activate();
    $book = $app->Workbooks(1);
    $sheet = $book->Worksheets(1);
     
    $cell=$sheet->Cells(1,1);
    //$cell->Activate;
    $cell ->Value = "aaaabbaaaaaaa";//Characters(strat,length)
    $cell ->Characters(3,6)->Font->Name = 'MS P明朝';
    $cell ->Characters(3,6)->Font->ColorIndex = 3;$book ->SaveAs("d:\\test.xls");
    $book ->Close(true);
     
    unset($sheet);
    unset($book);
     
    $app ->Quit();
    $app = null;echo "end";
    ?>
      

  11.   

    再请问一下
    如何取到$cell ->Characters(3,6)下面的属性,方法??
      

  12.   

    我也没找到什么简单的方法,网上很难能搜到php操作com的相关的细节资料
    就是根据VBA,改造例如下面一段VBA:改变单元格里面选中字符的字体,那么就Characters得Font属性,其中font属性又包含很多属性,比方说字体名Name ,字体颜色ColorIndex ,字体大小Size 这样就找到了相关的属性,然后再php中改造一下,php中对象的调用是使用“->”这个符号的$cell ->Characters(3,6)->Font->Size = 18; 就是改变字体大小至于Characters下面有什么属性,那你就可以用VBA把它找出来,可以通过录制宏,把你想要得操作录制成宏,再从中找出它的属性改变字体Range("D13").SelectActiveCell.FormulaR1C1 = "Change Font"With ActiveCell.Characters(Start:=1, Length:=11).Font  .Name = "Bernard MT Condensed"  .FontStyle = "Regular"  .Size = 12  .Strikethrough = False  .Superscript = False  .Subscript = False  .OutlineFont = False  .Shadow = False  .Underline = xlUnderlineStyleNone  .ColorIndex = xlAutomaticEnd With'设置字体颜色为红色Range("C4").SelectActiveCell.FormulaR1C1 = "Change Color"With ActiveCell.Characters(Start:=1, Length:=12).Font  .Name = "宋体"  .FontStyle = "Regular"  .Size = 12  .Strikethrough = False  .Superscript = False  .Subscript = False  .OutlineFont = False  .Shadow = False  .Underline = xlUnderlineStyleNone  .ColorIndex = 3End With