电邮至CSV C# - 字符串未正确格式化

时间:2018-06-05 20:10:59

标签: c# csv stringbuilder

因此,我正在解析我们从工作中收到的电子邮件,所有这些电子邮件的格式几乎完全相同,而且我从所有这些中获取了所需的数据,但是我在检索&#34时遇到了问题;描述",有多行。我试过删除" \ n"在这些字符串中,但它并不总是有效。当我打开.csv文件时,格式有时很好,但是否则字符串中的这些额外返回会破坏整个事物。

这是我的代码:

    public static void Main(string[] args)
    {
        helper();
        Console.WriteLine("Press any key to exit ... ");
        Console.ReadKey();

    } //end main method

    public static void helper()
    {
        //initiate outlook
        Microsoft.Office.Interop.Outlook.Application app = null;
        Microsoft.Office.Interop.Outlook._NameSpace ns = null;
        Microsoft.Office.Interop.Outlook.MailItem item = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;

        try
        {
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("MAPI");
            ns.Logon(null, null, false, false);

            inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            subFolder = inboxFolder.Folders["WorkOrders"]; //folder.Folders[1];
            Console.WriteLine("Folder Name: {0}", subFolder.Name);
            Console.WriteLine("Number of Emails: {0}", subFolder.Items.Count.ToString());
            Console.WriteLine();
            Console.WriteLine("-------------------------");
            Console.WriteLine();

            //Variables for loop
            String sub;
            String date;
            String time;
            String body;
            String desc;
            String storeNumber;
            String workOrderNumber;
            int indexOfDesc;
            int indexOfSp;
            int items = subFolder.Items.Count;

            DataTable dt = new DataTable();
            dt.Columns.Add("Work Order Number");
            dt.Columns.Add("Date");
            dt.Columns.Add("Store Number");
            dt.Columns.Add("Description");
            dt.Rows.Add("Work Order Number", "Date", "Store Number", "Description");
            dt.Rows.Add("", "", "", ""); // just a blank row

            for (int i = 1; i <= items; i++)
            {
                item = (Microsoft.Office.Interop.Outlook.MailItem)subFolder.Items[i];
                sub = item.Subject;
                date = item.SentOn.ToLongDateString();
                body = item.Body;
                desc = "";
                storeNumber = sub.Substring(0, 5);
                workOrderNumber = sub.Substring(sub.Length - 10);
                indexOfDesc = body.IndexOf("Description Overview:");
                indexOfSp = body.IndexOf("Safety Precautions:");
                desc = body.Substring(indexOfDesc, indexOfSp - indexOfDesc);
                desc = desc.Replace("\t", string.Empty);
                desc = desc.Replace("\n", string.Empty);

                // Add row in data table
                dt.Rows.Add(workOrderNumber, date, storeNumber, desc);

                // Process Date
                createCSV(dt);

                // *** Console Output for testing *** //
                Console.WriteLine("Index: {0}", i.ToString());
                Console.WriteLine("Store Number: {0}", sub.Substring(0, 5));
                Console.WriteLine("Work Order Number: # " + workOrderNumber);
                Console.WriteLine("Sent: {0}", date);
                Console.WriteLine(desc);
                Console.WriteLine();
                Console.WriteLine("-------------------------");
                Console.WriteLine();
            }

            PrintTable(dt);
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            ns = null;
            app = null;
            inboxFolder = null;
        } //end finally

    }

    private static void PrintTable(DataTable dt)
    {
        DataTableReader dtReader = dt.CreateDataReader();
        while (dtReader.Read())
        {
            for (int i = 0; i < dtReader.FieldCount; i++)
            {
                Console.Write("{0} = {1} ",
                    dtReader.GetName(i).Trim(),
                    dtReader.GetValue(i).ToString().Trim());
            }
            Console.WriteLine();
        }
        dtReader.Close();
    }

    /**
     * createCSV(DataTable dt) takes each line from data 
     * table and appends it to a csv file created at the var path
     * @param dt - DataTable 
     **/
    public static void createCSV(DataTable dt)
    {
        String path = "C:\\Users\\WORK\\Desktop\\test.csv";
        StringBuilder sb = new StringBuilder();
        foreach(DataRow dr in dt.Rows){
            foreach(DataColumn dc in dt.Columns){
                sb.Append(FormatCSV(dr[dc.ColumnName].ToString()) + ",");
            } // end foreach
            sb.Remove(sb.Length-1,1); //remove comma
            sb.AppendLine();
        } // end foreach
        File.WriteAllText(path, sb.ToString());
    } // end createCSV()

    public static string FormatCSV(string input)
    {
        try
        {
            if (input == null)
                return string.Empty;

            bool containsQuote = false;
            bool containsComma = false;
            int len = input.Length;
            for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++)
            {
                char ch = input[i];
                if (ch == '"')
                    containsQuote = true;
                else if (ch == ',')
                    containsComma = true;
            }

            if (containsQuote && containsComma)
                input = input.Replace("\"", "\"\"");

            if (containsComma)
                return "\"" + input + "\"";
            else
                return input;
        }
        catch
        {
            throw;
        }
    }

} //end class

以下是CSV文件的截图。请注意,电子邮件中的所有描述都有额外的空格和内容,我突出显示了按照我想要的方式工作的行:没有多余的空格,只有一行正确的信息。您可以看到回报如何破坏电子表格。 Image of the CSV - file opened in excel

所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

使用换行符时的一个问题是,有时它不是Windows使用的标准CRLF(\r\n)。有时它只是\r,有时它只是\n

当你无法控制换行符时,它可以成为一个不错的安全网来取代所有三个。

var sub = string.Empty;
var foo = myString.Replace("\r\n", sub).Replace("\r", sub).Replace("\n", sub);

这可以解决您正在说话的情况

Some line item\r\n Another line item\n Last line item\r

仅删除\n可能在一个应用程序中正常工作,但显示相同字符串的另一个应用程序仍可将其放在新行上。