static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FolderBrowserDialog dialog = new FolderBrowserDialog(); string dirpath = ""; string filepath = ""; if (dialog.ShowDialog() == DialogResult.OK) { dirpath = dialog.SelectedPath; string[] fileEntries = Directory.GetFiles(dirpath); //Initialize an excel file and a line number to be passed to the Output method int rowNumber = 1; CreateExcel(dirpath); bool save = false; for (int i = 0; i < fileEntries.Length; i++) { filepath = fileEntries[i]; //Check if the inspected file is a .rcv or .xml or .env, if not, continue to next file string extension = Path.GetExtension(filepath); if (extension != ".rcv" && extension != ".xml" && extension != ".env") { continue; } Delfor Message = new Delfor(filepath); rowNumber++; //Flag for saving only at last entry to the excel if (i == fileEntries.Length - 1) { save = true; } OutputToExcel(Message, rowNumber, dirpath + "\\Delfor_Analysis.xlsx", save); } } }
private static void OutputToExcel(Delfor Message, int rowNumber, string path, bool save) { Message.MessageFrom = Message.GetMessageFrom(Message.DelforMessageXml); Message.MessageTo = Message.GetMessageTo(Message.DelforMessageXml); Message.AliasNumber = Message.GetAliasNumber(Message.DelforMessageXml); Message.BuyerID = Message.GetBuyerID(Message.DelforMessageXml); Message.SellerID = Message.GetSellerID(Message.DelforMessageXml); Message.LoadingPlace = Message.GetLoadingPlace(Message.DelforMessageXml); Message.ShipToPlace = Message.GetShipToPlace(Message.DelforMessageXml); Message.PlantCode = Message.GetPlantCode(Message.DelforMessageXml); Message.Loc1 = Message.GetLoc1(Message.DelforMessageXml); Message.Loc2 = Message.GetLoc2(Message.DelforMessageXml); Message.VWType10 = Message.SellerID + Message.PlantCode; Message.TimeOfMessage = Message.GetTimeOfMessage(Message.DelforMessageXml); Excel.Application excel = new Excel.Application(); excel.Visible = false; Excel.Workbook wb = excel.Workbooks.Open(path); Excel.Worksheet sh = wb.ActiveSheet; sh.Cells[rowNumber, "A"] = Message.FileName; sh.Cells[rowNumber, "B"] = Message.MessageFrom; sh.Cells[rowNumber, "C"] = Message.MessageTo; sh.Cells[rowNumber, "D"] = Message.AliasNumber; sh.Cells[rowNumber, "E"] = Message.BuyerID; sh.Cells[rowNumber, "F"] = Message.SellerID; sh.Cells[rowNumber, "G"] = Message.LoadingPlace; sh.Cells[rowNumber, "H"] = Message.ShipToPlace; sh.Cells[rowNumber, "I"] = Message.PlantCode; sh.Cells[rowNumber, "J"] = Message.Loc1; sh.Cells[rowNumber, "K"] = Message.Loc2; sh.Cells[rowNumber, "L"] = Message.VWType10; sh.Cells[rowNumber, "M"] = Message.TimeOfMessage; if (save) { wb.Save(); } wb.Close(true); excel.Quit(); }