엑셀로 로또 당첨 번호를 불러와서 충복된 번호가 나온 적이 있는지 확인하는 프로그램
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;
namespace ConsoleApp_study
{
class study1
{
static Excel.Application excelApp = null;
static Excel.Workbook workBook = null;
static Excel.Worksheet workSheet = null;
static void Main(string[] args)
{
string[] num = new string[1000];
string[] num2 = new string[8];
int i = 0;
int c = 0;
try
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string path = Path.Combine(desktopPath, "lotto.xlsx");
excelApp = new Excel.Application();
workBook = excelApp.Workbooks.Open(path);
workSheet = workBook.Worksheets.get_Item(1) as Excel.Worksheet;
Excel.Range range = workSheet.UsedRange;
for (int row = 4; row <= range.Rows.Count; row++)
{
for (int column = 2; column <= 9; column++)
{
num2[i] = Convert.ToString((range.Cells[row, column]).Value2);
if (i < 7)
{
i++;
}
else
{
i = 0;
}
}
num[c] = num2[0] + "회 " + num2[1] + ", " + num2[2] + ", " + num2[3] + ", " + num2[4] + ", " + num2[5] + ", " + num2[6] + " 보너스 " + num2[7];
c++;
}
string sub1, sub2;
int index, bo_index;
for (i = 0; i < 985; i++)
{
index = Convert.ToInt32(num[i].IndexOf("회"));
bo_index = Convert.ToInt32(num[i].IndexOf("보"));
sub1 = num[i].Substring(index + 2, bo_index - 5);
for (int b = i + 1; b < 985; b++)
{
index = Convert.ToInt32(num[b].IndexOf("회"));
bo_index = Convert.ToInt32(num[b].IndexOf("보"));
sub2 = num[b].Substring(index + 2, bo_index - 5);
if (sub1 == sub2)
{
Console.WriteLine(num[i]);
Console.WriteLine(num[b]);
}
}
}
workBook.Close(true);
excelApp.Quit();
}
finally
{ ReleaseObject(workSheet);
ReleaseObject(workBook);
ReleaseObject(excelApp);
}
}
static void ReleaseObject(object obj)
{
try
{
if (obj != null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
} catch (Exception ex)
{
obj = null;
throw ex;
} finally
{
GC.Collect();
}
}
}
}