0

Good morning all,

First of all, thank you for taking the time to read my post, its my first, please be gentle :) I have created a windows form using VS2019 /c#. The user inserts the data and hits "insert" which then saves the data as a CSV file. Tableau then reads the CSV file, which all works. However I need the date format as yyyy-MM-dd. I cannot for the life of me get that to work. It always saves as MM-dd-yyyy. I am using a datetimepicker. Please see my code below for more info, and thank you so much for your help.

            Excel.Workbook xlworkbook;
            Excel.Worksheet xlworksheet;
            object misValue = System.Reflection.Missing.Value;

            xlworkbook = xlap.Workbooks.Add(misValue);

            xlworksheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(1);

            Excel.Sheets worksheets = xlworkbook.Worksheets;
            worksheets[3].Delete();
            worksheets[2].Delete();

            xlworksheet.Cells[1, 1] = textBox1.Text;
            xlworksheet.Cells[1, 2] = dateTimePicker1.Value.ToString("yyyyMMdd");
            xlworksheet.Cells[1, 3] = textBox2.Text;
            xlworksheet.Cells[1, 4] = textBox3.Text;
            xlworksheet.Cells[1, 5] = textBox4.Text;
            xlworksheet.Cells[1, 6] = textBox5.Text;
            xlworksheet.Cells[1, 7] = textBox6.Text;
            xlworksheet.Cells[1, 8] = textBox7.Text;
            xlworksheet.Cells[1, 9] = textBox8.Text;
            xlworksheet.Cells[1, 10] = textBox9.Text;
            xlworksheet.Cells[1, 13] = textBox10.Text;
            xlworksheet.Cells[1, 11] = comboBox1.Text;
            xlworksheet.Cells[1, 12] = comboBox2.Text;

            xlworkbook.SaveAs(@"\\172.21.4.14\BusinessLanding\BusinessLanding_Sync\operations_sync\FlightPlanning\savings_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv", Excel.XlFileFormat.xlCSVWindows);
            xlworkbook.Close(true);
            xlap.Quit();

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        
    }

      
  • You can setup custom format for your date picker - https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datetimepicker?view=net-5.0#examples And also you can apply a custom format for any DateTime value - https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – PWND Nov 25 '20 at 09:34
  • The problem is not how the date is presented on the form. The problem is how its saved to CSV. I need the date format to save as yyyy-MM-dd. But it ignores my coding and still saves it as MM-dd-yyyy. – Shane Nicholson Nov 25 '20 at 10:36

0 Answers0