locked
Why is IOS Mail app adding new lines to my csv file and corrupting the format? RRS feed

  • Question

  • User388138 posted

    Hello,

    I am using the Xamarin essential share function to share a file to the IOS mail application. When I send the email with the attachment the CSV file has new line spaces between all the data. When using Gmail this does not happen. Here is the code I use for constructing the file and sharing it to the mail application.

        async Task SendSaleTask(int saleid){
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder
            string libraryPath = Path.Combine(documentsPath, "..", "Library"); // Library folder
            var path = Path.Combine(libraryPath, DatabaseHelpers.DbFileName);
            var conn = new SQLiteConnection(path);
            // Create the connection  
         // get data for the file   
            string q = "SELECT Unit.UnitName, Plot.PlotNumber,Tree.Species,Tree.DBH,Tree.LogCount,Tree.Grades,Tree.Defects,Tree.FC,Plot.Notes,Sale.Cruiser " +
                "FROM Sale,Unit,Plot,Tree " +
                "WHERE Sale.SaleID = ? AND Unit.SaleID = ? AND Plot.SaleId = ? AND Tree.SaleId = ?" +
                "AND Plot.UnitId = Unit.UnitID AND Tree.PlotId = Plot.PlotID AND Tree.UnitId = Unit.UnitID";
            string fileName = Path.Combine(libraryPath, (_sale.SaleName + "_" + _sale.Cruiser + ".csv"));
    
            List<PipsData> DataList = conn.Query<PipsData>(q, _sale.SaleID, _sale.SaleID, _sale.SaleID, _sale.SaleID).OrderBy(c=>c.PlotNumber).OrderBy(x => x.UnitName).ToList();
    
            TextWriter tw = new StreamWriter(fileName);
            int counter = 1;
            tw.Write("Unit" + "," + "Plot" + "," + "TreeID" + "," + "FieldSP" + "," + "DBH" + "," + "LogHt" + "," + "LogGrade" + "," + "LogDefect" + "," + "FormClass" + "," + "Notes" + "," + "Cruiser" + "\r\n");
            foreach (var item in DataList)
            {
                if (item.FC == "0")
                {
                    item.FC = string.Empty;
    
                }
                tw.Write(
                    '"' + item.UnitName.ToString() + '"' + "," +
                    item.PlotNumber.ToString() + "," +
                    counter.ToString() + "," +
                    item.Species.ToString("D2") + "," +
                    item.DBH.ToString() + "," +
                    item.LogCount.ToString() + "," +
                    '"' + item.Grades.ToString() + '"' + "," +
                    '"' + item.Defects.ToString() + '"' + "," +
                    item.FC.ToString() + "," +
                    '"' + item.Notes.ToString() +'"'+ "," +
                    item.Cruiser.ToString() + "\r\n");
                counter++;
            }
            tw.Close();
    
            await Share.RequestAsync(new ShareFileRequest
            {
                Title = "Cruise file",
                File = new ShareFile(fileName),
                PresentationSourceBounds = DeviceInfo.Platform == DevicePlatform.iOS && DeviceInfo.Idiom == DeviceIdiom.Tablet
                            ? new System.Drawing.Rectangle(0, 20, 0, 0)
                            : System.Drawing.Rectangle.Empty
            });
    
        }
    

    Good data using gmail to share.

    Bad data using IOS mail application to share.

    Monday, August 10, 2020 3:24 PM

Answers

  • User369978 posted

    I guess the issue is caused because you are using "\r\n" at first line .

    Both \r and \n represent the end of a line , check https://stackoverflow.com/a/15433263/8187800 .

    Try to use \r or \n alone.

    For example

    tw.Write("Unit" + "," + "Plot" + "," + "TreeID" + "," + "FieldSP" + "," + "DBH" + "," + "LogHt" + "," + "LogGrade" + "," + "LogDefect" + "," + "FormClass" + "," + "Notes" + "," + "Cruiser" + "\n");
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 11, 2020 8:52 AM

All replies

  • User369978 posted

    I guess the issue is caused because you are using "\r\n" at first line .

    Both \r and \n represent the end of a line , check https://stackoverflow.com/a/15433263/8187800 .

    Try to use \r or \n alone.

    For example

    tw.Write("Unit" + "," + "Plot" + "," + "TreeID" + "," + "FieldSP" + "," + "DBH" + "," + "LogHt" + "," + "LogGrade" + "," + "LogDefect" + "," + "FormClass" + "," + "Notes" + "," + "Cruiser" + "\n");
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 11, 2020 8:52 AM
  • User388138 posted

    Thanks for the reply ColeX this lead me to explore using WriteLine rather than just Write. Using WriteLine got the expected results I was looking for.

    For Example

    tw.WriteLine("Unit" + "," + "Plot" + "," + "TreeID" + "," + "FieldSP" + "," + "DBH" + "," + "LogHt" + "," + "LogGrade" + "," + "LogDefect" + "," + "FormClass" + "," + "Notes" + "," + "Cruiser");

    Tuesday, August 11, 2020 2:06 PM