C#
Excel 파일 <-> DataTable
구리Guri
2025. 1. 8. 14:38
using System;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
public partial class CustomScript
{
public void Execute_Code()
{
string filePath = @"C:\Users\WW\Documents\rpa_test\Sample1.xlsx";
string sheetName = "Sheet1";
// Excel Application 생성
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = null;
Excel.Worksheet worksheet = null;
Excel.Range range = null;
try
{
// 엑셀 파일 열기
workbook = excelApp.Workbooks.Open(filePath);
worksheet = workbook.Sheets[sheetName] as Excel.Worksheet;
if (worksheet == null)
throw new Exception("Sheet '" + sheetName + "'를 찾을 수 없습니다.");
range = worksheet.UsedRange;
int rows = range.Rows.Count;
int columns = range.Columns.Count;
// DataTable에 열 추가
for (int col = 1; col <= columns; col++)
{
Excel.Range cell = range.Cells[1, col] as Excel.Range;
string columnName = (cell != null && cell.Value2 != null) ? cell.Value2.ToString() : "Column"+col;
dataTable.Columns.Add(columnName);
PrintLog(columnName);
}
// DataTable에 데이터 추가
for (int row = 2; row <= rows; row++)
{
DataRow dataRow = dataTable.NewRow();
for (int col = 1; col <= columns; col++)
{
Excel.Range cell = range.Cells[row, col] as Excel.Range;
// 셀 값 가져오기
object cellValue = cell.Value2;
dataRow[col - 1] = (cellValue != null) ? cellValue.ToString() : string.Empty;
}
dataTable.Rows.Add(dataRow);
}
}
catch (Exception ex)
{
PrintLog("Error: " + ex.Message);
}
finally
{
// Excel 객체 해제
if (range != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
if (worksheet != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
if (workbook != null) workbook.Close(false);
if (excelApp != null) excelApp.Quit();
if (workbook != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
if (excelApp != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}
}
}
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
public partial class CustomScript
{
public void Execute_Code()
{
string filePath = @"C:\Users\WW\Documents\rpa_test\Sample2.xlsx";
string sheetName = "Sheet1";
// Excel Application 생성
Excel.Application excelApp = new Excel.Application();
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = workbook.Sheets[sheetName] as Excel.Worksheet;
try
{
// 파일이 이미 존재하면 삭제
if (File.Exists(filePath))
{
File.Delete(filePath);
PrintLog("기존 파일을 삭제했습니다.");
}
// DataTable의 컬럼을 Excel의 첫 번째 행에 추가
for (int col = 0; col < dataTable.Columns.Count; col++)
{
worksheet.Cells[1, col + 1] = dataTable.Columns[col].ColumnName;
}
// DataTable의 데이터를 Excel에 채우기
for (int row = 0; row < dataTable.Rows.Count; row++)
{
for (int col = 0; col < dataTable.Columns.Count; col++)
{
worksheet.Cells[row + 2, col + 1] = dataTable.Rows[row][col].ToString();
}
}
// 각 열의 너비를 글자 길이에 맞게 자동으로 조정
worksheet.Columns.AutoFit();
// 엑셀 파일 저장
workbook.SaveAs(filePath);
PrintLog("DataTable을 엑셀 파일로 저장했습니다: " + filePath);
}
catch (Exception ex)
{
PrintLog("Error: " + ex.Message);
}
finally
{
// Excel 객체 해제
workbook.Close(false);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}
}
}