using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace DwgExtractorManual.Models
{
///
/// Excel COM ��ü ���� �� �⺻ �۾��� ����ϴ� Ŭ����
///
internal class ExcelManager : IDisposable
{
public Excel.Application? ExcelApplication { get; private set; }
public Excel.Workbook? TitleBlockWorkbook { get; private set; }
public Excel.Workbook? MappingWorkbook { get; private set; }
public Excel.Worksheet? TitleBlockSheet { get; private set; }
public Excel.Worksheet? TextEntitiesSheet { get; private set; }
public Excel.Worksheet? NoteEntitiesSheet { get; private set; }
public Excel.Worksheet? MappingSheet { get; private set; }
///
/// Excel ���ø����̼� �� ��ũ��Ʈ �ʱ�ȭ
///
public void InitializeExcel()
{
try
{
var excelApp = new Excel.Application();
ExcelApplication = excelApp;
ExcelApplication.Visible = false; // WPF������ ���� ó��
Excel.Workbook workbook = excelApp.Workbooks.Add();
TitleBlockWorkbook = workbook;
// Title Block Sheet ���� (�⺻ Sheet1)
TitleBlockSheet = (Excel.Worksheet)workbook.Sheets[1];
TitleBlockSheet.Name = "Title Block";
SetupTitleBlockHeaders();
// Text Entities Sheet �߰�
TextEntitiesSheet = (Excel.Worksheet)workbook.Sheets.Add();
TextEntitiesSheet.Name = "Text Entities";
SetupTextEntitiesHeaders();
// Note Entities Sheet �߰�
NoteEntitiesSheet = (Excel.Worksheet)workbook.Sheets.Add();
NoteEntitiesSheet.Name = "Note Entities";
SetupNoteEntitiesHeaders();
// ���� �����Ϳ� ��ũ�� �� ��Ʈ ����
MappingWorkbook = excelApp.Workbooks.Add();
MappingSheet = (Excel.Worksheet)MappingWorkbook.Sheets[1];
MappingSheet.Name = "Mapping Data";
SetupMappingHeaders();
}
catch (System.Exception ex)
{
Debug.WriteLine($"Excel �ʱ�ȭ �� ���� ��: {ex.Message}");
ReleaseExcelObjects();
throw;
}
}
///
/// ���� Excel ������ ���� ���� ��Ʈ�� ����
///
public bool OpenExistingFile(string excelFilePath)
{
try
{
if (!File.Exists(excelFilePath))
{
Debug.WriteLine($"? Excel ������ �������� �ʽ��ϴ�: {excelFilePath}");
return false;
}
if (ExcelApplication == null)
{
ExcelApplication = new Excel.Application();
ExcelApplication.Visible = false;
}
MappingWorkbook = ExcelApplication.Workbooks.Open(excelFilePath);
MappingSheet = (Excel.Worksheet)MappingWorkbook.Sheets["Mapping Data"];
if (MappingSheet == null)
{
Debug.WriteLine("? 'Mapping Data' ��Ʈ�� ã�� �� �����ϴ�.");
return false;
}
return true;
}
catch (System.Exception ex)
{
Debug.WriteLine($"? Excel ���� ���� �� ����: {ex.Message}");
return false;
}
}
///
/// ���ο� ��ũ�� ���� (Height ���Ŀ�)
///
public Excel.Workbook CreateNewWorkbook()
{
if (ExcelApplication == null)
{
ExcelApplication = new Excel.Application();
ExcelApplication.Visible = false;
}
return ExcelApplication.Workbooks.Add();
}
// Title Block ��Ʈ ��� ����
private void SetupTitleBlockHeaders()
{
if (TitleBlockSheet == null) return;
TitleBlockSheet.Cells[1, 1] = "Type"; // ��: AttributeReference, AttributeDefinition
TitleBlockSheet.Cells[1, 2] = "Name"; // BlockReference �̸� �Ǵ� BlockDefinition �̸�
TitleBlockSheet.Cells[1, 3] = "Tag"; // Attribute Tag
TitleBlockSheet.Cells[1, 4] = "Prompt"; // Attribute Prompt
TitleBlockSheet.Cells[1, 5] = "Value"; // Attribute �� (TextString)
TitleBlockSheet.Cells[1, 6] = "Path"; // ���� DWG ���� ��ü ���
TitleBlockSheet.Cells[1, 7] = "FileName"; // ���� DWG ���� �̸���
// ��� �� ��Ÿ��
Excel.Range headerRange = TitleBlockSheet.Range["A1:G1"];
headerRange.Font.Bold = true;
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightBlue);
}
// Text Entities ��Ʈ ��� ����
private void SetupTextEntitiesHeaders()
{
if (TextEntitiesSheet == null) return;
TextEntitiesSheet.Cells[1, 1] = "Type"; // DBText, MText
TextEntitiesSheet.Cells[1, 2] = "Layer"; // Layer �̸�
TextEntitiesSheet.Cells[1, 3] = "Text"; // ���� �ؽ�Ʈ ����
TextEntitiesSheet.Cells[1, 4] = "Path"; // ���� DWG ���� ��ü ���
TextEntitiesSheet.Cells[1, 5] = "FileName"; // ���� DWG ���� �̸���
// ��� �� ��Ÿ��
Excel.Range headerRange = TextEntitiesSheet.Range["A1:E1"];
headerRange.Font.Bold = true;
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen);
}
// ���� ������ ��Ʈ ��� ����
private void SetupMappingHeaders()
{
if (MappingSheet == null) return;
MappingSheet.Cells[1, 1] = "FileName"; // ���� �̸�
MappingSheet.Cells[1, 2] = "MapKey"; // ���� Ű
MappingSheet.Cells[1, 3] = "AILabel"; // AI ��
MappingSheet.Cells[1, 4] = "DwgTag"; // DWG Tag
MappingSheet.Cells[1, 5] = "Att_value"; // DWG ��
MappingSheet.Cells[1, 6] = "Pdf_value"; // PDF �� (����� �� ��)
// ��� �� ��Ÿ��
Excel.Range headerRange = MappingSheet.Range["A1:F1"];
headerRange.Font.Bold = true;
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightYellow);
}
// Note Entities ��Ʈ ��� ����
private void SetupNoteEntitiesHeaders()
{
if (NoteEntitiesSheet == null) return;
NoteEntitiesSheet.Cells[1, 1] = "Type"; // Note, NoteContent
NoteEntitiesSheet.Cells[1, 2] = "Layer"; // Layer �̸�
NoteEntitiesSheet.Cells[1, 3] = "Text"; // ���� �ؽ�Ʈ ����
NoteEntitiesSheet.Cells[1, 4] = "X"; // X ��ǥ
NoteEntitiesSheet.Cells[1, 5] = "Y"; // Y ��ǥ
NoteEntitiesSheet.Cells[1, 6] = "SortOrder"; // ���� ����
NoteEntitiesSheet.Cells[1, 7] = "TableCsv"; // ���̺� CSV ������
NoteEntitiesSheet.Cells[1, 8] = "Path"; // ���� DWG ���� ��ü ���
NoteEntitiesSheet.Cells[1, 9] = "FileName"; // ���� DWG ���� �̸���
// ��� �� ��Ÿ��
Excel.Range headerRange = NoteEntitiesSheet.Range["A1:I1"];
headerRange.Font.Bold = true;
headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightCoral);
}
///
/// ��ũ�� ����
///
public bool SaveWorkbook(Excel.Workbook? workbook = null)
{
try
{
if (workbook != null)
{
workbook.Save();
return true;
}
if (MappingWorkbook != null)
{
MappingWorkbook.Save();
return true;
}
if (TitleBlockWorkbook != null)
{
TitleBlockWorkbook.Save();
return true;
}
return false;
}
catch (System.Exception ex)
{
Debug.WriteLine($"? Excel ���� ���� �� ����: {ex.Message}");
return false;
}
}
///
/// ��ũ���� ������ ��ο� ����
///
public void SaveWorkbookAs(Excel.Workbook? workbook, string savePath)
{
if (workbook == null) return;
string? directory = Path.GetDirectoryName(savePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
workbook.SaveAs(savePath,
FileFormat: Excel.XlFileFormat.xlOpenXMLWorkbook,
AccessMode: Excel.XlSaveAsAccessMode.xlNoChange);
}
///
/// Excel ��Ʈ������ ����� �� �ִ� ��ȿ�� �̸��� �����մϴ�.
///
public string GetValidSheetName(string originalName)
{
if (string.IsNullOrEmpty(originalName))
return "Sheet";
// Excel ��Ʈ������ ������ �ʴ� ���� ����
string validName = originalName;
char[] invalidChars = { '\\', '/', '?', '*', '[', ']', ':' };
foreach (char c in invalidChars)
{
validName = validName.Replace(c, '_');
}
// 31�ڷ� ���� (Excel ��Ʈ�� �ִ� ����)
if (validName.Length > 31)
{
validName = validName.Substring(0, 31);
}
return validName;
}
public void CloseWorkbooks()
{
if (TitleBlockWorkbook != null)
{
try { TitleBlockWorkbook.Close(false); }
catch { }
}
if (MappingWorkbook != null)
{
try { MappingWorkbook.Close(false); }
catch { }
}
if (ExcelApplication != null)
{
try { ExcelApplication.Quit(); }
catch { }
}
}
private void ReleaseExcelObjects()
{
ReleaseComObject(TitleBlockSheet);
ReleaseComObject(TextEntitiesSheet);
ReleaseComObject(NoteEntitiesSheet);
ReleaseComObject(MappingSheet);
ReleaseComObject(TitleBlockWorkbook);
ReleaseComObject(MappingWorkbook);
ReleaseComObject(ExcelApplication);
TitleBlockSheet = null;
TextEntitiesSheet = null;
NoteEntitiesSheet = null;
MappingSheet = null;
TitleBlockWorkbook = null;
MappingWorkbook = null;
ExcelApplication = null;
}
private void ReleaseComObject(object? obj)
{
try
{
if (obj != null && Marshal.IsComObject(obj))
{
Marshal.ReleaseComObject(obj);
}
}
catch (System.Exception)
{
// ���� �� ���� �� �� ����
}
}
public void Dispose()
{
try
{
Debug.WriteLine("[DEBUG] ExcelManager Dispose ����");
CloseWorkbooks();
ReleaseExcelObjects();
GC.Collect();
GC.WaitForPendingFinalizers();
Debug.WriteLine("[DEBUG] ExcelManager Dispose �Ϸ�");
}
catch (System.Exception ex)
{
Debug.WriteLine($"[DEBUG] ExcelManager Dispose �� ����: {ex.Message}");
}
}
}
}