PIbd-33_Sergunov_M.A._COP_4/Library/CustomComponents/BigTextComponent.cs

54 lines
1.3 KiB
C#
Raw Permalink Normal View History

2023-11-16 19:05:42 +04:00
using System.ComponentModel;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace CustomComponents
{
public partial class BigTextComponent : Component
{
public BigTextComponent()
{
InitializeComponent();
}
public BigTextComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateExcel(string file, string title, string[] text)
{
if (string.IsNullOrEmpty(file))
throw new ArgumentException("Не указан путь к файлу");
if (string.IsNullOrEmpty(title))
throw new ArgumentException("Не указан заголовок документа");
if (text == null || text.Length == 0)
throw new ArgumentException("Массив с текстом пуст либо null");
if (File.Exists(file)) { File.Delete(file); }
var xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
xlWorkSheet.Cells[1, 1] = title;
for (int i = 0; i < text.Length; i++)
{
xlWorkSheet.Cells[i + 3, 1] = text[i];
}
xlApp.Application.ActiveWorkbook.SaveAs(file);
xlWorkBook.Close(true);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
MessageBox.Show("Документ успешно создан!");
}
}
}