$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();
$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.先取得这个单元格的文字
2.截取到目标文字
3.然后利用Excel的方法,属性设定字体颜色不晓得对不啦??麻烦各位出来指点一下。。
$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";
?>
vba的代码不熟悉,能看懂,但移植时间有问题。老兄有没有移植后的代码呀,让兄弟参考一下.
报错:PHP Parse error: parse error, unexpected T_OBJECT_OPERATOR
14行报错$cell ->Characters(3,6)->Font->Name = '宋体'
$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";
?>
如何取到$cell ->Characters(3,6)下面的属性,方法??
就是根据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