77 lines
2.5 KiB
C#

using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LicenseContext = OfficeOpenXml.LicenseContext;
namespace UnvisableComponents
{
public partial class ImageExcel : Component
{
public ImageExcel()
{
InitializeComponent();
}
public ImageExcel(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void Load(ImageInfo info)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
Check(info);
using (ExcelPackage excelPackage = new ExcelPackage())
{
excelPackage.Workbook.Properties.Author = "Qawithexperts";
excelPackage.Workbook.Properties.Title = "test Excel";
excelPackage.Workbook.Properties.Subject = "Write in Excel";
excelPackage.Workbook.Properties.Created = DateTime.Now;
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
worksheet.Cells["A1"].Value = info.Title ;
int posString = 1;
int posColumn = 0;
int i = 1;
foreach(var image in info.Files)
{
ExcelPicture excelImage = null;
Stream stream = new MemoryStream(image);
excelImage = worksheet.Drawings.AddPicture("image" + i, stream);
excelImage.SetPosition(posString, 0, posColumn, 0);
excelImage.SetSize(100, 100);
i++;
posString += 10;
}
FileInfo fi = new FileInfo(info.Path);
excelPackage.SaveAs(fi);
}
}
public void Check(ImageInfo info)
{
if (string.IsNullOrEmpty(info.Title))
throw new Exception("Забыли заглавие");
if(string.IsNullOrEmpty(info.Path))
throw new Exception("Забыли путь");
if (!info.Path.EndsWith(".xlsx"))
info.Path += ".xlsx";
if (info.Files.Count == 0)
throw new Exception("забыли фото");
}
}
}