I recently imported a number of large excel 2010 spreadsheets (which was exported to CSV for the import) into a database. I have now been told that the cell comments are missing (I didn't even know there were cell comments in the original Excel file, I only ever had the CSV).
I need to search each row of the Excel file, find cells with cell comments, grab the comment and add it into the database for that row (the database references the row number from the spreadsheet, so if I can just extract row number and all the cell comments in that row, that's perfect)
My skills are in PHP and Perl. Ideally I would use PHP to read and process the data, but using something else to output text/csv, which I can then parse with PHP would work fine too.
Does anyone know a way to extract these cell comments, or have any pointers?
Thanks!
Use the PHPExcel library. Comments are held against the worksheet. and the worksheet's getComments() method will return an array of comment objects indexed by their cell address.
EDIT
include '/PHPExcel_Library/Classes/PHPExcel.php';
$inputFileType = 'Excel5';
$inputFileName = 'CS2template_all_products_v12.xls';
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$sheet = $objPHPExcel->getActiveSheet();
$comments = $sheet->getComments();
foreach($comments as $cellID => $comment) {
echo $cellID,PHP_EOL;
var_dump($comment->getText()->getPlainText());
}
Note, my example uses an Excel .xls file, but the method will work just as easily replacing the $inputFileType with "Excel2007" for an .xlsx file
I can't take credit for this one. I got if from this website: http://chandoo.org/wp/2009/09/03/get-cell-comments/
Just insert a module in VBA and copy and paste this in:
Function getComment(incell) As String
' accepts a cell as input and returns its comments (if any) back as a string
On Error Resume Next
getComment = incell.Comment.Text
End Function
Just put it in a cell, reference the cell with a comment and autofill to your hearts content. For example:
=getComment(C42)
If you need the row number of a row, you can just do:
=row()
Not sure if you needed to know that but I just thought I would add that in.