PopulateColumnWithImages
(
Workbook workbook,
String worksheetName,
String tableName,
String imageColumnName,
String keyColumnName,
TemporaryImages temporaryImages
)
{
Debug.Assert(workbook != null);
Debug.Assert( !String.IsNullOrEmpty(worksheetName) );
Debug.Assert( !String.IsNullOrEmpty(tableName) );
Debug.Assert( !String.IsNullOrEmpty(imageColumnName) );
Debug.Assert( !String.IsNullOrEmpty(keyColumnName) );
Debug.Assert(temporaryImages != null);
ListObject oTable;
Range oKeyColumnData;
// Get the table and the key column data.
if (!ExcelTableUtil.TryGetTable(workbook, worksheetName, tableName,
out oTable)
||
!ExcelTableUtil.TryGetTableColumnData(oTable, keyColumnName,
out oKeyColumnData)
)
{
// Nothing can be done without the table or key column.
return;
}
Range oImageColumnData;
// Add the image column if it doesn't already exist.
if ( !TryGetImageColumnData(oTable, imageColumnName,
out oImageColumnData) )
{
// The image column doesn't exist and couldn't be added.
return;
}
String sFolder = temporaryImages.Folder;
if (sFolder == null)
{
// No temporary images were created, so nothing more needs to be
// done.
return;
}
// Reduce the key and image column data to visible areas only.
Range oVisibleKeyColumnData, oVisibleImageColumnData;
if (
!ExcelUtil.TryGetVisibleRange(oKeyColumnData,
out oVisibleKeyColumnData)
||
!ExcelUtil.TryGetVisibleRange(oImageColumnData,
out oVisibleImageColumnData)
)
{
return;
}
Int32 iAreas = oVisibleKeyColumnData.Areas.Count;
if (iAreas != oVisibleImageColumnData.Areas.Count)
{
return;
}
// Get the size of each image, in points.
SizeF oImageSizePt =
GetImageSizePt(temporaryImages.ImageSizePx, workbook);
// Get any old images in the image column as a dictionary. This
// significantly speeds up the deletion of the old images, because
// Excel doesn't have to do a linear search on Shape.Name as each image
// is deleted by PopulateAreaWithImages().
Debug.Assert(oTable.Parent is Worksheet);
Dictionary<String, Microsoft.Office.Interop.Excel.Shape>
oOldImagesInColumn = GetImagesInColumn( (Worksheet)oTable.Parent,
imageColumnName );
// Populate each area of the image column with images.
workbook.Application.ScreenUpdating = false;
try
{
for (Int32 iArea = 1; iArea <= iAreas; iArea++)
{
PopulateAreaWithImages(oVisibleKeyColumnData.Areas[iArea],
oVisibleImageColumnData.Areas[iArea], imageColumnName,
oImageSizePt, oOldImagesInColumn, temporaryImages);
}
}
finally
{
workbook.Application.ScreenUpdating = true;
}
// Delete the entire temporary folder.
try
{
Directory.Delete(sFolder, true);
}
catch (IOException)
{
// A user reported the following exception thrown from the above
// Directory.Delete() call:
//
// "System.IO.IOException: The directory is not empty.:
//
// Others have reported this happenning at random times. For
// example:
//
// http://forums.asp.net/p/1114215/1722498.aspx
//
// I have also seen it happen from the command line outside of
// .NET. When it occurs, the directory IS empty but cannot be
// accessed in any way. The directory disappears when the machine
// is rebooted.
//
// I can't figure out the cause or the fix. Ignore the problem,
// which seems to be benign.
}
}