使用此代码,我可以在excel文件中创建注释.
$comment = $data_sheet->getCommentByColumnAndRow($col, 1);
$comment->setAuthor($table_name . '.' . $field_name);
$comment->setWidth('200px');
$comment->setHeight('24px');
$comment->setVisible(false); # ActiveCell.Comment.Visible = True
$objCommentRichText = $comment->getText()->createTextRun($table_name . '.' . $field_name);
保存时,文件中包含注释,可以对其进行编辑,但是在重新打开该文件时,注释对象处于默认状态.
$comment = $data_sheet->getComment('A1');
结果是:-
$comment = PHPExcel_Comment Object (
[_author:private] => Author
[_text:private] => PHPExcel_RichText Object ( [_richTextElements:private] => Array ( ) )
[_width:private] => 96pt
[_marginLeft:private] => 59.25pt
[_marginTop:private] => 1.5pt
[_visible:private] =>
[_height:private] => 55.5pt
[_fillColor:private] => PHPExcel_Style_Color Object (
[_argb:private] => FFFFFFE1
[_isSupervisor:private] =>
[_parent:private] =>
[_parentPropertyName:private] =>
)
)
编辑:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<authors><author>products.products_id</author><author>products.part_code</author><author>products.products_name</author><author>products.products_quantity</author><author>products.products_status</author><author>products.cost</author><author>products.cost_modifier</author><author>products.delivery_cost</author><author>products.shipping_strategy</author><author>products.products_weight</author><author>products.shipping_amount</author><author>products.products_price</author><author>products.rrp</author><author>products.trade</author><author>products_feed.amazon_price</author><author>products_feed.ebay_price</author></authors>
<commentList>
<comment ref="A1" authorId="0"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_id</t></r></text></comment>
<comment ref="B1" authorId="1"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.part_code</t></r></text></comment>
<comment ref="C1" authorId="2"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_name</t></r></text></comment>
<comment ref="D1" authorId="3"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_quantity</t></r></text></comment>
<comment ref="E1" authorId="4"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_status</t></r></text></comment>
<comment ref="F1" authorId="5"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.cost</t></r></text></comment>
<comment ref="G1" authorId="6"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.cost_modifier</t></r></text></comment>
<comment ref="H1" authorId="7"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.delivery_cost</t></r></text></comment>
<comment ref="I1" authorId="8"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.shipping_strategy</t></r></text></comment>
<comment ref="J1" authorId="9"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_weight</t></r></text></comment>
<comment ref="K1" authorId="10"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.shipping_amount</t></r></text></comment>
<comment ref="L1" authorId="11"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.products_price</t></r></text></comment>
<comment ref="M1" authorId="12"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.rrp</t></r></text></comment>
<comment ref="N1" authorId="13"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products.trade</t></r></text></comment>
<comment ref="O1" authorId="14"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products_feed.amazon_price</t></r></text></comment>
<comment ref="P1" authorId="15"><text><r><rPr><sz val="11"/><color rgb="FF000000"/><rFont val="Calibri"/></rPr><t>products_feed.ebay_price</t></r></text></comment>
</commentList>
</comments>
因此,注释存在于.xlsx文件中.
这些都还没有收到评论.
$comment = $data_sheet->getCommentByColumnAndRow($col, 1);
$comment = $data_sheet->getComment('A'. $row);
$comments = $data_sheet->getComments();
– 编辑
问题,注释是否正确以“ Excel2007”格式加载?
解决方法:
这是我使用PHPExcel从单元格中提取注释的代码片段
$objPHPExcel = PHPExcel_IOFactory::load("MyExcelFile.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
// loop through each row in excel file
foreach ($objWorksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
// loop through each column in row
foreach ($cellIterator as $cell) {
// get the value of the cell
$value = $cell->getValue();
// get the comment in the cell (if a comment exists)
$comment = $objWorksheet->getComment($cell->getCoordinate())->getText();
}
}
这是到PHPExcel Comments Documentation的链接