본문 바로가기
컴퓨터 활용(한글, 오피스 등)/기타

C# 폴더 내에 여러 엑셀 파일 셀 값 얻기

by 3604 2024. 4. 3.
728x90
using System;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

class ExcelReader
{
    static void Main(string[] args)
    {
        var excelApp = new Excel.Application();
        var files = Directory.GetFiles(@"C:\your\path\here\", "*.xlsx");

        foreach (var file in files)
        {
            var workbook = excelApp.Workbooks.Open(file);
            var sheet = workbook.Sheets[1] as Excel.Worksheet;
            var range = sheet.Range["A1"]; // A1을 원하는 셀 주소로 변경하세요.
            Console.WriteLine($"File: {file} - Value: {range.Value}");
            workbook.Close(false);
        }

        excelApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
    }
}
AI가 생성한 코드입니다. 신중하게 검토하고 사용하세요. FAQ의 자세한 정보.

이 코드는 C:\your\path\here\ 경로에 있는 모든 .xlsx 파일을 대상으로 합니다. 원하는 폴더 경로와 셀 주소로 변경하여 사용하세요. 또한, Excel이 설치된 환경에서만 실행됩니다. Excel Application을 사용하기 위해 COM 참조를 추가해야 합니다. 프로젝트에 Microsoft Excel 16.0 Object Library를 참조로 추가하고, 필요한 경우 Excel 버전에 맞게 참조 버전을 조정하세요.

728x90