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? 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(); // ¸ÅÇÎ µ¥ÀÌÅÍ¿ë ¿öÅ©ºÏ ¹× ½ÃÆ® »ý¼º 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); } /// /// ¿öÅ©ºÏ ÀúÀå /// 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(MappingSheet); ReleaseComObject(TitleBlockWorkbook); ReleaseComObject(MappingWorkbook); ReleaseComObject(ExcelApplication); TitleBlockSheet = null; TextEntitiesSheet = 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}"); } } } }