2024-10-07 12:52:54 +04:00
|
|
|
|
using Excel = Microsoft.Office.Interop.Excel;
|
2024-10-07 00:07:33 +04:00
|
|
|
|
using System.ComponentModel;
|
2024-10-07 12:52:54 +04:00
|
|
|
|
|
2024-10-06 21:52:59 +04:00
|
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
|
{
|
|
|
|
|
public partial class ComponentExcelWithImage : Component
|
|
|
|
|
{
|
2024-10-07 00:07:33 +04:00
|
|
|
|
public ComponentExcelWithImage()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentExcelWithImage(IContainer container) : this()
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
private void CheckInputValues(string filePath, string tableTitle, string[] imagePaths)
|
2024-10-06 21:52:59 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
if (string.IsNullOrEmpty(filePath))
|
2024-10-06 21:52:59 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(filePath));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(tableTitle))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(tableTitle));
|
|
|
|
|
}
|
|
|
|
|
if (imagePaths == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(imagePaths));
|
|
|
|
|
}
|
|
|
|
|
foreach (string imagePath in imagePaths)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(imagePath))
|
2024-10-06 21:52:59 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
throw new ArgumentNullException("Image path is null");
|
2024-10-06 21:52:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-07 00:07:33 +04:00
|
|
|
|
}
|
2024-10-06 21:52:59 +04:00
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
public void CreateExcelWithImages(string filePath, string tableTitle, string[] imagePaths)
|
2024-10-07 00:07:33 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
CheckInputValues(filePath, tableTitle, imagePaths);
|
2024-10-07 00:07:33 +04:00
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
Excel.Application excelApp = new Excel.Application();
|
|
|
|
|
Excel.Workbook workbook = excelApp.Workbooks.Add();
|
|
|
|
|
Excel.Worksheet worksheet = workbook.Sheets[1];
|
2024-10-06 21:52:59 +04:00
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
worksheet.Cells[1, 1] = tableTitle;
|
|
|
|
|
|
|
|
|
|
InsertImagesInSingleCell(worksheet, imagePaths, 2, 1);
|
|
|
|
|
|
|
|
|
|
workbook.SaveAs(filePath);
|
|
|
|
|
workbook.Close();
|
|
|
|
|
excelApp.Quit();
|
|
|
|
|
|
|
|
|
|
ReleaseObject(worksheet);
|
|
|
|
|
ReleaseObject(workbook);
|
|
|
|
|
ReleaseObject(excelApp);
|
2024-10-06 21:52:59 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
private void InsertImagesInSingleCell(Excel.Worksheet worksheet, string[] imagePaths, int rowIndex, int columnIndex)
|
2024-10-06 21:52:59 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
Excel.Range cell = worksheet.Cells[rowIndex, columnIndex];
|
|
|
|
|
double currentTopOffset = 0;
|
2024-10-07 00:07:33 +04:00
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
foreach (string imagePath in imagePaths)
|
2024-10-07 00:07:33 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
if (File.Exists(imagePath))
|
|
|
|
|
{
|
|
|
|
|
Excel.Pictures pictures = (Excel.Pictures)worksheet.Pictures(System.Reflection.Missing.Value);
|
|
|
|
|
Excel.Picture picture = pictures.Insert(imagePath);
|
2024-10-06 21:52:59 +04:00
|
|
|
|
|
2024-10-07 12:52:54 +04:00
|
|
|
|
picture.Left = cell.Left;
|
|
|
|
|
picture.Top = cell.Top + currentTopOffset;
|
|
|
|
|
|
|
|
|
|
currentTopOffset += picture.Height;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("Image not found at: " + imagePath);
|
|
|
|
|
}
|
2024-10-07 00:07:33 +04:00
|
|
|
|
}
|
2024-10-07 12:52:54 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReleaseObject(object obj)
|
|
|
|
|
{
|
|
|
|
|
try
|
2024-10-07 00:07:33 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
|
|
|
|
|
obj = null;
|
|
|
|
|
}
|
2024-10-07 00:07:33 +04:00
|
|
|
|
}
|
2024-10-07 12:52:54 +04:00
|
|
|
|
catch (Exception ex)
|
2024-10-06 21:52:59 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
obj = null;
|
2024-10-07 13:20:18 +04:00
|
|
|
|
Console.WriteLine("Error releasing object: " + ex.ToString());
|
2024-10-06 21:52:59 +04:00
|
|
|
|
}
|
2024-10-07 12:52:54 +04:00
|
|
|
|
finally
|
2024-10-06 21:52:59 +04:00
|
|
|
|
{
|
2024-10-07 12:52:54 +04:00
|
|
|
|
GC.Collect();
|
2024-10-06 21:52:59 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-07 12:52:54 +04:00
|
|
|
|
}
|