Forum Xamarin.iOS

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

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.

Best Answer

Answers

  • Colton0012Colton0012 Member
    edited August 11

    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");

Sign In or Register to comment.