๋ฐ˜์‘ํ˜•

C#์—์„œ ์‰ฝ๊ฒŒ ์—‘์…€(Excel) ์‚ฌ์šฉํ•˜๊ธฐ

์ด์ „์— MFC์—์„œ XLNT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ์—‘์…€ ํŒŒ์ผ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

[MFC] MFC์—์„œ XLNT๋ฅผ ์ด์šฉํ•œ ์—‘์…€(Excel)ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

 

[MFC] MFC์—์„œ XLNT๋ฅผ ์ด์šฉํ•œ ์—‘์…€(Excel)ํŒŒ์ผ ์ฝ๊ณ  ์“ฐ๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

์†Œ๊ฐœ C# ์—์„œ๋Š” Microsoft.Office.Interop.Excel dll์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„ํŽธํ•˜๊ฒŒ ์—‘์…€ ํŒŒ์ผ์— ์ ‘๊ทผํ•˜๊ณ , ๋ฐ์ดํ„ฐ ์ฝ๊ณ  ์“ฐ๊ธฐ๊ฐ€ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ C++ ์ด๋‚˜ MFC ๋Š” ์ œ๊ณต๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์—†๋‹ค ๋ณด๋‹ˆ ๋„ˆ๋ฌด๋‚˜๋„ ๋ถˆํŽธํ•ฉ

luckygg.tistory.com

MFC์—์„œ๋Š” XLNT ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ CMake๋กœ ๋นŒ๋“œํ•˜๊ณ , ํ”„๋กœ์ ํŠธ์— ์„ค์ •ํ•˜๋Š” ๋“ฑ์˜ ๊ณผ์ •์ด ๊ท€์ฐฎ์•˜์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ C#์€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ˆ„๊ฒŸ(NuGet)์—์„œ Excel dll๋งŒ ์„ค์น˜ํ•˜๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ, ๋ณธ๋ฌธ์—์„œ ๋ณธ๊ฒฉ์ ์œผ๋กœ C#์—์„œ ์—‘์…€ ํŒŒ์ผ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ณธ๋ฌธ์˜ ์ƒ˜ํ”Œ ์ฝ”๋“œ๋Š” ํ•˜๋‹จ์— ์ฒจ๋ถ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ค€๋น„ ๋‹จ๊ณ„

ExamWinformExcel์ด๋ผ๋Š” ์ด๋ฆ„์˜ WinForm ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , NuGet ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ํ›„ NuGet ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ ๋ฉ”๋‰ด ํด๋ฆญ

์ฐพ์•„๋ณด๊ธฐ ํƒญ์—์„œ Excel์„ ๊ฒ€์ƒ‰ํ•˜๊ณ , Microsoft.Office.Interop.Excel ํ•ญ๋ชฉ์„ ์„ ํƒํ•œ ํ›„ ์„ค์น˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

Microsoft.Office.Interop.Excel ์„ค์น˜

์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์•„๋ž˜์˜ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

using Microsoft.Office.Interop.Excel;

Excel ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

์ปดํ“จํ„ฐ์— ์กด์žฌํ•˜๋Š” ์—‘์…€ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

private void buttonOpen_Click(object sender, EventArgs e)
{
	Microsoft.Office.Interop.Excel.Application application = null;
	Workbook workBook = null;

	try
	{
		OpenFileDialog dlg = new OpenFileDialog();
		if (dlg.ShowDialog() != DialogResult.OK)
			return;

		//Excel ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰
		application = new Microsoft.Office.Interop.Excel.Application();
		//Excel ํ™”๋ฉด ๋„์šฐ๊ธฐ ์˜ต์…˜
		application.Visible = true;
		//ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
		workBook = application.Workbooks.Open(dlg.FileName);
	}
	catch(Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
	finally
	{
		//๋ณ€๊ฒฝ์  ์ €์žฅํ•˜๋ฉด์„œ ๋‹ซ๊ธฐ
		workBook.Close(true);
		//Excel ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
		application.Quit();
		//์˜ค๋ธŒ์ ํŠธ ํ•ด์ œ
		ReleaseExcelObject(workBook);
		ReleaseExcelObject(application);
	}
}

private void ReleaseExcelObject(object obj)
{
	try
	{
		if (obj != null)
		{
			Marshal.ReleaseComObject(obj);
			obj = null;
		}
	}
	catch (Exception ex)
	{
		obj = null;
		throw ex;
	}
	finally
	{
		GC.Collect();
	}
}

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, Application ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Workbook ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ˆœ์„œ์ž…๋‹ˆ๋‹ค. ํ•ด์ œํ•  ๋•Œ๋Š” ๋ฐ˜๋Œ€๋กœ Workbook ๊ฐ์ฒด๋ฅผ ๋‹ซ๊ณ  Application ๊ฐ์ฒด๋ฅผ ์ข…๋ฃŒํ•˜์ฃ . ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ ์ˆœ์„œ๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ด ์ˆœ์„œ๋ฅผ ๊ผญ ์ง€์ผœ์•ผ ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ์—์„œ 10๋ฒˆ์งธ ์ฝ”๋“œ๋Š” Excel ํ™”๋ฉด์„ ๋„์šธ์ง€์— ๋Œ€ํ•œ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ ํ•œ ์ค„์ด ์‹คํ–‰๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋น„์–ด์žˆ๋Š” ์—‘์…€ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์—…๋ฌด ์ž๋™ํ™” ํ”„๋กœ๊ทธ๋žจ์ด๋ผ๋ฉด ๊ตณ์ด ๋„์šธ ํ•„์š”๊ฐ€ ์—†๊ฒ ์ฃ ? ๊ทธ๋Ÿด ๊ฒฝ์šฐ์—๋Š” false๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Excel Application์ด ์‹คํ–‰๋œ ๋ชจ์Šต

๊ทธ๋ฆฌ๊ณ  12๋ฒˆ์งธ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ํŠน์ • ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์ •์ƒ์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

Excel ํŒŒ์ผ์ด ๋ถˆ๋Ÿฌ์™€์ง„ ๋ชจ์Šต

Excel ํŒŒ์ผ ์ €์žฅํ•˜๊ธฐ

์ €์žฅํ•˜๋Š” ๊ณผ์ •์€ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ณผ์ •๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

private void buttonSave_Click(object sender, EventArgs e)
{
	Microsoft.Office.Interop.Excel.Application application = null;
	Workbook workBook = null;

	try
	{
		SaveFileDialog dlg = new SaveFileDialog();
		dlg.Filter = "xlsx files|*.xlsx";
		if (dlg.ShowDialog() != DialogResult.OK)
			return;

		application = new Microsoft.Office.Interop.Excel.Application();
		application.Visible = true;

		//Workbook ์ƒ์„ฑ
		workBook = application.Workbooks.Add();
		//ํŠน์ • ๊ฒฝ๋กœ์— ์—‘์…€ ํŒŒ์ผ๋กœ ์ €์žฅ
		workBook.SaveAs(dlg.FileName, XlFileFormat.xlWorkbookDefault);
	}
	catch (Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
	finally
	{
		workBook.Close(true);
		application.Quit();

		ReleaseExcelObject(workBook);
		ReleaseExcelObject(application);
	}
}

๋ถˆ๋Ÿฌ์˜ค๊ธฐ์™€ ๋‹ค๋ฅธ ๋ถ€๋ถ„์€ 13๋ฒˆ์งธ, 15๋ฒˆ์งธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. 13๋ฒˆ์งธ ์ฝ”๋“œ๋Š” ๋น„์–ด์žˆ๋Š” Application์— ์ƒˆ๋กœ์šด Workbook์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๊ณ , 15๋ฒˆ์งธ ์ฝ”๋“œ๋Š” ํŠน์ • ๊ฒฝ๋กœ์— ์—‘์…€ ํŒŒ์ผ๋กœ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ €์žฅํ•˜๋Š” ๊ณผ์ •๋„ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์‹œํŠธ(Sheet) ์ถ”๊ฐ€ ๋ฐ ์ด๋ฆ„ ๋ณ€๊ฒฝํ•˜๊ธฐ

์ด๋ฒˆ์—๋Š” ์ƒˆ๋กœ ๋งŒ๋“  ์—‘์…€ ํŒŒ์ผ์— Sheet๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŠน์ • ์…€์— ๋ฐ์ดํ„ฐ๋„ ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

private void buttonEditSave_Click(object sender, EventArgs e)
{
	Microsoft.Office.Interop.Excel.Application application = null;
	Workbook workBook = null;
	Worksheet workSheet = null;

	try
	{
		SaveFileDialog dlg = new SaveFileDialog();
		dlg.Filter = "xlsx files|*.xlsx";
		if (dlg.ShowDialog() != DialogResult.OK)
			return;

		application = new Microsoft.Office.Interop.Excel.Application();
		application.Visible = true;

		workBook = application.Workbooks.Add();
		
		//๋งˆ์ง€๋ง‰ ์‹œํŠธ ๋’ค์— ์ƒˆ๋กœ์šด ์‹œํŠธ ์ถ”๊ฐ€
		workSheet = workBook.Worksheets.Add(After: workBook.Worksheets.Item[workBook.Worksheets.Count]);
		//์‹œํŠธ ์ด๋ฆ„ ๋ณ€๊ฒฝ
		workSheet.Name = "ํ…Œ์ŠคํŠธ ์‹œํŠธ";
		//1ํ–‰ 1์—ด์— ๋ฌธ์ž์—ด "ํ…Œ์ŠคํŠธ" ์ž…๋ ฅ
		Range cell1 = workSheet.Cells[1, 1];
		cell1.Value = "ํ…Œ์ŠคํŠธ";
		//2ํ–‰ 1์—ด์— ์ˆซ์ž "1234" ์ž…๋ ฅ
		Range cell2 = workSheet.Cells[2, 1];
		cell2.Value = "1234";
		//2ํ–‰ 2์—ด์— ๋ฌธ์ž์—ด "ABCDโ€ป" ์ž…๋ ฅ
		Range cell3 = workSheet.Cells[2, 2];
		cell3.Value = "ABCDโ€ป";

		workBook.SaveAs(dlg.FileName, XlFileFormat.xlWorkbookDefault);
	}
	catch (Exception exc)
	{
		MessageBox.Show(exc.Message);
	}
	finally
	{
		workBook.Close(true);
		application.Quit();

		ReleaseExcelObject(workBook);
		ReleaseExcelObject(application);
	}
}

์‹œํŠธ๋ฅผ ๊ทธ๋ƒฅ ์ถ”๊ฐ€ํ•ด์„œ๋Š” ์•ˆ๋˜๊ณ , 20๋ฒˆ์งธ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์–ด๋Š ์œ„์น˜ ์ดํ›„(After) ๋˜๋Š” ์ด์ „(Before)์œผ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ์‹œํŠธ๊ฐ€ ์‚ฝ์ž…๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฆ„ ๋ณ€๊ฒฝ์€ 22๋ฒˆ์งธ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ์…€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์…€ ์ž…๋ ฅ ๋ฐ ์‹œํŠธ ์ถ”๊ฐ€, ์ด๋ฆ„ ๋ณ€๊ฒฝํ•œ ๊ฒฐ๊ณผ

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ์ฒจ๋ถ€๋œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

ExamWinformExcel.zip
0.01MB

 

๋ฐ˜์‘ํ˜•

'Programming > .Net' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[.Net] C#์—์„œ CefSharp์œผ๋กœ ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ € ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (์˜ˆ์ œ ํฌํ•จ)  (1) 2021.09.11
[.Net] C#์—์„œ JPEG, PNG๋ฅผ WebP๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ• (์˜ˆ์ œ ํฌํ•จ)  (0) 2021.08.30
[.Net] C# Syntax Highlighting ์—๋””ํ„ฐ๋ฅผ ์œ„ํ•œ ScintillaNET ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•  (0) 2021.08.05
[.Net] C#์œผ๋กœ ๋Œ€์‹ ์ฆ๊ถŒ ์ฃผ์‹ ์ž๋™ ๋งค๋งค ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ - ํฌ๋ ˆ์˜จ ํ”Œ๋Ÿฌ์Šค ์ค€๋น„ ๋‹จ๊ณ„  (0) 2021.04.12
[.Net] C#์œผ๋กœ ๋Œ€์‹ ์ฆ๊ถŒ ์ฃผ์‹ ์ž๋™ ๋งค๋งค ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ  (2) 2021.04.09
[.Net] C#์—์„œ ์นด์นด์˜คํ†ก ์˜คํ”ˆAPI/REST API๋กœ ๋‚˜์—๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ (์˜ˆ์ œ ํฌํ•จ)  (10) 2021.03.23
[.Net] ๊ณต๊ณต๋ฐ์ดํ„ฐํฌํ„ธ API ์‚ฌ์šฉํ•˜์—ฌ C#์—์„œ ์‹ค์‹œ๊ฐ„ ๋ฒ„์Šค ์œ„์น˜ ์กฐํšŒํ•˜๊ธฐ(์˜ˆ์ œ ํฌํ•จ)  (0) 2021.03.20