public static void WriteCsv(List <String[]> wkLine) { try { string csvPath = ""; System.Text.StringBuilder csvFileName = new System.Text.StringBuilder(); System.Text.StringBuilder csvHeader = new System.Text.StringBuilder(); System.Text.StringBuilder csvText = new System.Text.StringBuilder(); String[] mwLiine = new String[20]; mwLiine = wkLine[0]; //set filename'''; csvFileName.Append(BusinessConst.REDAC_NAME_INVOICE); csvFileName.Append("_"); csvFileName.Append(mwLiine[0]); csvFileName.Append("_"); csvFileName.Append(DateTime.Now.ToString("yyyyMMddHHmmss")); csvFileName.Append(".csv"); csvPath = ImportCommon.GetAppSetting(BusinessConst.CSV_FILE_PATH) + csvFileName.ToString(); //set csv header csvHeader.Append(BusinessConst.CSV_DQ + "Order No" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Advanced Payment ID" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Submit Date" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Payment Date" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Payment Time" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Total Amount" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Redac Division" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Parent Redac Division" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Tenant Name" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Company Name" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Street" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Street2" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Street3" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "City" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "US State" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "ZIP/Postal code" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Country" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Main Phone" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Fax" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Email" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Purpose" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Amount" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Advance/Expense" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Payable to" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Payment Method" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Company ID" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "Type" + BusinessConst.CSV_DQ); csvHeader.Append(",").Append(BusinessConst.CSV_DQ + "CustomerGroup" + BusinessConst.CSV_DQ + Environment.NewLine); //set csv body for (int i = 0; i < wkLine.Count; i++) { String[] mwLiinefor = new String[20]; mwLiinefor = wkLine[i]; csvText.Append(BusinessConst.CSV_DQ + mwLiinefor[1] + BusinessConst.CSV_DQ); csvText.Append(",").Append(BusinessConst.CSV_DQ + mwLiinefor[1] + BusinessConst.CSV_DQ); csvText.Append(",").Append(BusinessConst.CSV_DQ + mwLiinefor[1] + BusinessConst.CSV_DQ); csvText.Append(",").Append(BusinessConst.CSV_DQ + mwLiinefor[1] + BusinessConst.CSV_DQ); csvText.Append(",").Append(BusinessConst.CSV_DQ + mwLiinefor[1] + BusinessConst.CSV_DQ); csvText.Append(",").Append(BusinessConst.CSV_DQ + mwLiinefor[1].Replace(BusinessConst.CSV_DQ, "\"\"") + BusinessConst.CSV_DQ); csvText.Append(",").Append(BusinessConst.CSV_DQ + mwLiinefor[1] + BusinessConst.CSV_DQ + Environment.NewLine); } ImportCommon.CreateCsv(csvPath, csvHeader.ToString(), csvText.ToString()); } catch (System.Exception e) { throw new Exception(e.Message); } }
static void Main(string[] args) { System.Threading.Mutex mtx = null; //connect to D365 var context = ImportCommon.Connect(); //read config string strFilePath = ImportCommon.GetAppSetting(BusinessConst.CSV_FILE_PATH); string strSuccessPath = ImportCommon.GetAppSetting(BusinessConst.SUC_FILE_PATH); string strErrorPath = ImportCommon.GetAppSetting(BusinessConst.ERR_FILE_PATH); string strLogPath = ImportCommon.GetAppSetting(BusinessConst.LOG_FILE_PATH); //log file StringBuilder logMsg = new System.Text.StringBuilder(); string strLogName = ImportCommon.GetLogFileName(BusinessConst.REDAC_NAME_INVOICE); string strfileName = ""; int iFileCnt = 0; Console.WriteLine("logName : {0}", strLogName); Console.WriteLine("FilePath : {0}", strFilePath); //mail set Console.WriteLine("mail setting :::"); EmailConf emailConf = new EmailConf(); try { string mutextName = "SanImportAccountCsv"; mtx = new System.Threading.Mutex(false, mutextName); mtx.WaitOne(); //open file ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, BusinessConst.LOG_ST); IEnumerable <System.IO.FileInfo> targetCsvfiles = ImportCommon.GetCsvFile(strFilePath); Console.WriteLine("start loop "); //import data foreach (System.IO.FileInfo targetCsvfile in targetCsvfiles) { iFileCnt++; strfileName = targetCsvfile.Name; ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "File name:" + targetCsvfile.Name); Console.WriteLine("strfileName : {0}", strfileName); //write import data List <string> accRecords = ImportCommon.WriteCsvFile(targetCsvfile); var wkLine = ImportCommon.TrimDoubleQuotationMarks(accRecords[1].Replace("\",\"", "\t")).Split('\t'); Console.WriteLine("create customer : "); string comId = wkLine[25]; if (wkLine[27].Equals("Tenant")) { comId = comId.Replace(",", ""); } if (SanODataQuerys.CheckByCustomId(context, comId)) { SanODataChangesets.CreateCustomAdv(wkLine, context); } else { Console.WriteLine("customer aleady exist: "); SanODataChangesets.AddCustomAddress(wkLine, context); } Console.WriteLine("create journal : "); SanODataChangesets.CreateGeneralJournalAdv(accRecords, context, logMsg); Console.WriteLine("create done !!"); //move file ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "Done:" + strfileName); ImportCommon.MoveCsvFile(strFilePath + "/" + strfileName, strSuccessPath + "/" + strfileName); } ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, "File count:" + iFileCnt); ImportCommon.Writelog(logMsg, BusinessConst.LOG_INFO, BusinessConst.LOG_EN); } catch (System.Exception e) { Console.WriteLine(e.Message); Console.WriteLine("err somthing : {0}", e.InnerException); //move file ImportCommon.Writelog(logMsg, BusinessConst.LOG_ERROR, "Detail1:" + e.Message); ImportCommon.Writelog(logMsg, BusinessConst.LOG_ERROR, "Detail2:" + e.InnerException); ImportCommon.MoveCsvFile(strFilePath + "/" + strfileName, strErrorPath + "/" + strfileName); SendEmail email = new SendEmail(emailConf); email.SendException(e, strLogName); if (mtx != null) { mtx.ReleaseMutex(); mtx = null; } } finally { Console.WriteLine("check log : {0} ", logMsg.ToString()); if (iFileCnt > 0) { ImportCommon.CreatelogFile(strLogPath + "/" + strLogName, logMsg.ToString()); } Console.WriteLine("end "); if (mtx != null) { mtx.ReleaseMutex(); } } }