dynamic 형식을 통해 훨씬 간결해진 코드로 엑셀 COM 컴포넌트를 참조, 사용하여 게임 런타임 중에 게임 로그를 기록한다.
using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace COMInterop
{
class MainApp
{
public static void UseExcelCOMComponent(string[,] data, string savePath)
{
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Add();
Excel._Worksheet workSheet = (Excel._Worksheet)excelApp.ActiveSheet;
for (int i = 0; i < data.GetLength(0); ++i)
{
workSheet.Cells[i + 1, 1].Value = data[i, 0];
workSheet.Cells[i + 1, 2].Value = data[i, 1];
}
workSheet.SaveAs(savePath + "\\\\game-log-dynamic.xlsx");
excelApp.Quit();
}
static void Main(string[] args)
{
string savePath = System.IO.Directory.GetCurrentDirectory();
string[,] array = new string[50, 2];
string datetime = DateTime.Now.ToString("HH:mm:ss:fffffff");
array[0, 0] = datetime;
array[0, 1] = "Start Game";
string gameLog;
for (int i = 1; i < 50; ++i)
{
datetime = DateTime.Now.ToString("HH:mm:ss:fffffff");
array[i, 0] = datetime;
gameLog = $"Walked {i} step";
if (i != 1) gameLog += "s";
array[i, 1] = gameLog;
}
Console.WriteLine("Creating Excel document");
UseExcelCOMComponent(array, savePath);
}
}
}