Game Engine/Unity

Unity C# EPPlus ExcelPackage Save()시에 엑셀 파일을 열수 없는 문제

OneSeat 2022. 7. 7.
반응형

🧱 사양

Unity 2021 LTS, Microsoft 365

💣 문제

Unity Custom Editor 작업 중에 코드 상에서 .xlsm 확장자인 Excel 파일을 수정하고 저장하기 위해 Save() 함수를 사용한 뒤 실제 엑셀 파일을 열어볼 경우 아래와 같은 메시지가 출력됩니다.

파일 형식 또는 파일 확장명이 잘못되어 '.xlsm' 파일을 열 수 없습니다. 파일이 손상되지 않았는지, 파일 확장명이 파일 형식과 일치하는지 확인하십시오.

혹은

Excel cannot open the file '.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

 

 

 

💡 해결

1. 엑셀 파일의 확장자를 .xlsm에서 .xlsx로 변경합니다. 그리고 다시 해당 엑셀 파일을 열어보면 정상적으로 열립니다.

 

2. 추가적인 해결(.xlsm 확장자 유지하기)

위의 1번의 방법대로 하면 문제없이 엑셀 파일을 열 수 있지만 확장자가 변경된다는 점입니다.
확장자가 변경되는 것이 싫다면 아래의 방법으로 확장자를 유지합니다.

확장자가 .xlsm이 기본인 엑셀 파일인 경우에만 유지인 것이지, .xlsx를 해당 방법으로 저장하면 유지가 아니라 .xlsm으로 변경됩니다.

 

 

엑셀 파일의 확장자가 xlsm이지만 실제로 존재하는 매크로가 없을 경우에는 Save() 시에 .xlsx로 변경이 됩니다.

그래서 .xlsm이라는 확장자를 유지하기 위해서 코드 단에 Save() 이전에 해당 코드를 추가해야 합니다.

if (package.Workbook.VbaProject == null)
{
    package.Workbook.CreateVBAProject();
}
사용되고 있는 엑셀의 확장자에 따라 해당 코드를 사용할지 안 할지 고려해야 합니다.

 

참고할 링크

 

Saving XSLM gives currupted error message on opening · Issue #112 · JanKallman/EPPlus

Opening and saving attached XSLM with following code: [TestMethod] public void Test() { using (var package = new ExcelPackage(new FileInfo(@"C:\temp\MakroTemplate.xlsm"))) { package.SaveA...

github.com

 

 

 

What is a VBA Project? (And How to Edit) - Automate Excel

In this ArticleAccessing the VBE EditorRecording a MacroOpening the VBE Editor directly from ExcelThe VBA ProjectInserting a Module or Form into the VBA ProjectAdding a Reference to a VBA ProjectVBA Project PropertiesRenaming a ProjectAdding Project Securi

www.automateexcel.com

 

 
 
 
 
 
반응형

댓글