C# Formatting columns when exporting data into excel












0















I know this topic has been discussed but I think it has some differences. I have dates stored in my database. So when I export data table into EXCEL, it shows like the image.



Here is the image of the EXCEL file:



enter image description here



I need to add only date. Not HH:MM:SS included. my code is pasted below:



Microsoft.Office.Interop.Excel._Application app = new 
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "InternDetails";

for (int i = 1; i < dataGridView3.Columns.Count + 1; i++){
worksheet.Cells[1, i] = dataGridView3.Columns[i - 1].HeaderText;
}

for (int i = 0; i < dataGridView3.Rows.Count; i++){
for (int j = 0; j < dataGridView3.Columns.Count; j++){
worksheet.Cells[i + 2, j + 1] = dataGridView3.Rows[i].Cells[j].Value.ToString();
}
}

var saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "Interns";
saveFileDialog.DefaultExt = ".xlsx";

if (saveFileDialog.ShowDialog() == DialogResult.OK){
workbook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}


Thanks for the help.










share|improve this question

























  • you need to format excel to only show the date part

    – JohnB
    Nov 26 '18 at 3:45











  • Yes. But if there is any way to do that in the code?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:47











  • excel is tricky, it tries to be too helpful sometimes. you could set the exel column for that date as 'text and export the date into that to stop it doing a helpful auto-format.

    – JohnB
    Nov 26 '18 at 3:51











  • Ah. so u mean i have to edit the excel form?. that cant be done in c#?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:54











  • ok, you could try to pre-append to the your date string a single '. that tells excel to treat the value as explicit text and to not try auto formatting.

    – JohnB
    Nov 26 '18 at 3:58
















0















I know this topic has been discussed but I think it has some differences. I have dates stored in my database. So when I export data table into EXCEL, it shows like the image.



Here is the image of the EXCEL file:



enter image description here



I need to add only date. Not HH:MM:SS included. my code is pasted below:



Microsoft.Office.Interop.Excel._Application app = new 
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "InternDetails";

for (int i = 1; i < dataGridView3.Columns.Count + 1; i++){
worksheet.Cells[1, i] = dataGridView3.Columns[i - 1].HeaderText;
}

for (int i = 0; i < dataGridView3.Rows.Count; i++){
for (int j = 0; j < dataGridView3.Columns.Count; j++){
worksheet.Cells[i + 2, j + 1] = dataGridView3.Rows[i].Cells[j].Value.ToString();
}
}

var saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "Interns";
saveFileDialog.DefaultExt = ".xlsx";

if (saveFileDialog.ShowDialog() == DialogResult.OK){
workbook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}


Thanks for the help.










share|improve this question

























  • you need to format excel to only show the date part

    – JohnB
    Nov 26 '18 at 3:45











  • Yes. But if there is any way to do that in the code?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:47











  • excel is tricky, it tries to be too helpful sometimes. you could set the exel column for that date as 'text and export the date into that to stop it doing a helpful auto-format.

    – JohnB
    Nov 26 '18 at 3:51











  • Ah. so u mean i have to edit the excel form?. that cant be done in c#?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:54











  • ok, you could try to pre-append to the your date string a single '. that tells excel to treat the value as explicit text and to not try auto formatting.

    – JohnB
    Nov 26 '18 at 3:58














0












0








0








I know this topic has been discussed but I think it has some differences. I have dates stored in my database. So when I export data table into EXCEL, it shows like the image.



Here is the image of the EXCEL file:



enter image description here



I need to add only date. Not HH:MM:SS included. my code is pasted below:



Microsoft.Office.Interop.Excel._Application app = new 
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "InternDetails";

for (int i = 1; i < dataGridView3.Columns.Count + 1; i++){
worksheet.Cells[1, i] = dataGridView3.Columns[i - 1].HeaderText;
}

for (int i = 0; i < dataGridView3.Rows.Count; i++){
for (int j = 0; j < dataGridView3.Columns.Count; j++){
worksheet.Cells[i + 2, j + 1] = dataGridView3.Rows[i].Cells[j].Value.ToString();
}
}

var saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "Interns";
saveFileDialog.DefaultExt = ".xlsx";

if (saveFileDialog.ShowDialog() == DialogResult.OK){
workbook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}


Thanks for the help.










share|improve this question
















I know this topic has been discussed but I think it has some differences. I have dates stored in my database. So when I export data table into EXCEL, it shows like the image.



Here is the image of the EXCEL file:



enter image description here



I need to add only date. Not HH:MM:SS included. my code is pasted below:



Microsoft.Office.Interop.Excel._Application app = new 
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "InternDetails";

for (int i = 1; i < dataGridView3.Columns.Count + 1; i++){
worksheet.Cells[1, i] = dataGridView3.Columns[i - 1].HeaderText;
}

for (int i = 0; i < dataGridView3.Rows.Count; i++){
for (int j = 0; j < dataGridView3.Columns.Count; j++){
worksheet.Cells[i + 2, j + 1] = dataGridView3.Rows[i].Cells[j].Value.ToString();
}
}

var saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "Interns";
saveFileDialog.DefaultExt = ".xlsx";

if (saveFileDialog.ShowDialog() == DialogResult.OK){
workbook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}


Thanks for the help.







c# mysql excel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 6:11









kit

1,1063816




1,1063816










asked Nov 26 '18 at 3:41









Sandeep RuwanpathiranaSandeep Ruwanpathirana

13




13













  • you need to format excel to only show the date part

    – JohnB
    Nov 26 '18 at 3:45











  • Yes. But if there is any way to do that in the code?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:47











  • excel is tricky, it tries to be too helpful sometimes. you could set the exel column for that date as 'text and export the date into that to stop it doing a helpful auto-format.

    – JohnB
    Nov 26 '18 at 3:51











  • Ah. so u mean i have to edit the excel form?. that cant be done in c#?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:54











  • ok, you could try to pre-append to the your date string a single '. that tells excel to treat the value as explicit text and to not try auto formatting.

    – JohnB
    Nov 26 '18 at 3:58



















  • you need to format excel to only show the date part

    – JohnB
    Nov 26 '18 at 3:45











  • Yes. But if there is any way to do that in the code?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:47











  • excel is tricky, it tries to be too helpful sometimes. you could set the exel column for that date as 'text and export the date into that to stop it doing a helpful auto-format.

    – JohnB
    Nov 26 '18 at 3:51











  • Ah. so u mean i have to edit the excel form?. that cant be done in c#?

    – Sandeep Ruwanpathirana
    Nov 26 '18 at 3:54











  • ok, you could try to pre-append to the your date string a single '. that tells excel to treat the value as explicit text and to not try auto formatting.

    – JohnB
    Nov 26 '18 at 3:58

















you need to format excel to only show the date part

– JohnB
Nov 26 '18 at 3:45





you need to format excel to only show the date part

– JohnB
Nov 26 '18 at 3:45













Yes. But if there is any way to do that in the code?

– Sandeep Ruwanpathirana
Nov 26 '18 at 3:47





Yes. But if there is any way to do that in the code?

– Sandeep Ruwanpathirana
Nov 26 '18 at 3:47













excel is tricky, it tries to be too helpful sometimes. you could set the exel column for that date as 'text and export the date into that to stop it doing a helpful auto-format.

– JohnB
Nov 26 '18 at 3:51





excel is tricky, it tries to be too helpful sometimes. you could set the exel column for that date as 'text and export the date into that to stop it doing a helpful auto-format.

– JohnB
Nov 26 '18 at 3:51













Ah. so u mean i have to edit the excel form?. that cant be done in c#?

– Sandeep Ruwanpathirana
Nov 26 '18 at 3:54





Ah. so u mean i have to edit the excel form?. that cant be done in c#?

– Sandeep Ruwanpathirana
Nov 26 '18 at 3:54













ok, you could try to pre-append to the your date string a single '. that tells excel to treat the value as explicit text and to not try auto formatting.

– JohnB
Nov 26 '18 at 3:58





ok, you could try to pre-append to the your date string a single '. that tells excel to treat the value as explicit text and to not try auto formatting.

– JohnB
Nov 26 '18 at 3:58












2 Answers
2






active

oldest

votes


















0














Ok,if you want to show date only.and no time,you should set the number format.
just look at the code like this:
SomeCell.NumberFormat="yyyy-mm-dd"



it will show for example:"2018-05-20"






share|improve this answer































    0














    Correct me if I'm wrong, for what I saw in the provided screenshot, it consist of 3 date column which is statusdate, fplacementdate and periodcomplertion.



    Have you tried it this way?



    First create a conversion function for you date.



    public static string convertDateFormat(this string date)
    {
    DateTime dateFormat = Convert.ToDateTime(date);
    if (dateFormat != DateTime.MinValue)
    {
    return String.Format("{0:MM/dd/yyyy}", dateFormat);
    }
    else
    {
    return "";
    }
    }


    Then use it this way



    for (int i = 0; i < dataGridView3.Rows.Count; i++){
    for (int j = 0; j < dataGridView3.Columns.Count; j++){
    string formatValue = dataGridView3.Rows[i].Cells[j].Value.ToString();

    if( dataGridView3.Columns[j].HeaderText == "statusdate" || dataGridView3.Columns[j].HeaderText == "fplacementdate" || dataGridView3.Columns[j].HeaderText == "periodcomplertion"){
    formatValue.convertDateFormat();
    }

    worksheet.Cells[i + 2, j + 1] = formatValue;
    }
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474510%2fc-sharp-formatting-columns-when-exporting-data-into-excel%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Ok,if you want to show date only.and no time,you should set the number format.
      just look at the code like this:
      SomeCell.NumberFormat="yyyy-mm-dd"



      it will show for example:"2018-05-20"






      share|improve this answer




























        0














        Ok,if you want to show date only.and no time,you should set the number format.
        just look at the code like this:
        SomeCell.NumberFormat="yyyy-mm-dd"



        it will show for example:"2018-05-20"






        share|improve this answer


























          0












          0








          0







          Ok,if you want to show date only.and no time,you should set the number format.
          just look at the code like this:
          SomeCell.NumberFormat="yyyy-mm-dd"



          it will show for example:"2018-05-20"






          share|improve this answer













          Ok,if you want to show date only.and no time,you should set the number format.
          just look at the code like this:
          SomeCell.NumberFormat="yyyy-mm-dd"



          it will show for example:"2018-05-20"







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 4:04









          TownDrinTownDrin

          32




          32

























              0














              Correct me if I'm wrong, for what I saw in the provided screenshot, it consist of 3 date column which is statusdate, fplacementdate and periodcomplertion.



              Have you tried it this way?



              First create a conversion function for you date.



              public static string convertDateFormat(this string date)
              {
              DateTime dateFormat = Convert.ToDateTime(date);
              if (dateFormat != DateTime.MinValue)
              {
              return String.Format("{0:MM/dd/yyyy}", dateFormat);
              }
              else
              {
              return "";
              }
              }


              Then use it this way



              for (int i = 0; i < dataGridView3.Rows.Count; i++){
              for (int j = 0; j < dataGridView3.Columns.Count; j++){
              string formatValue = dataGridView3.Rows[i].Cells[j].Value.ToString();

              if( dataGridView3.Columns[j].HeaderText == "statusdate" || dataGridView3.Columns[j].HeaderText == "fplacementdate" || dataGridView3.Columns[j].HeaderText == "periodcomplertion"){
              formatValue.convertDateFormat();
              }

              worksheet.Cells[i + 2, j + 1] = formatValue;
              }
              }





              share|improve this answer




























                0














                Correct me if I'm wrong, for what I saw in the provided screenshot, it consist of 3 date column which is statusdate, fplacementdate and periodcomplertion.



                Have you tried it this way?



                First create a conversion function for you date.



                public static string convertDateFormat(this string date)
                {
                DateTime dateFormat = Convert.ToDateTime(date);
                if (dateFormat != DateTime.MinValue)
                {
                return String.Format("{0:MM/dd/yyyy}", dateFormat);
                }
                else
                {
                return "";
                }
                }


                Then use it this way



                for (int i = 0; i < dataGridView3.Rows.Count; i++){
                for (int j = 0; j < dataGridView3.Columns.Count; j++){
                string formatValue = dataGridView3.Rows[i].Cells[j].Value.ToString();

                if( dataGridView3.Columns[j].HeaderText == "statusdate" || dataGridView3.Columns[j].HeaderText == "fplacementdate" || dataGridView3.Columns[j].HeaderText == "periodcomplertion"){
                formatValue.convertDateFormat();
                }

                worksheet.Cells[i + 2, j + 1] = formatValue;
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  Correct me if I'm wrong, for what I saw in the provided screenshot, it consist of 3 date column which is statusdate, fplacementdate and periodcomplertion.



                  Have you tried it this way?



                  First create a conversion function for you date.



                  public static string convertDateFormat(this string date)
                  {
                  DateTime dateFormat = Convert.ToDateTime(date);
                  if (dateFormat != DateTime.MinValue)
                  {
                  return String.Format("{0:MM/dd/yyyy}", dateFormat);
                  }
                  else
                  {
                  return "";
                  }
                  }


                  Then use it this way



                  for (int i = 0; i < dataGridView3.Rows.Count; i++){
                  for (int j = 0; j < dataGridView3.Columns.Count; j++){
                  string formatValue = dataGridView3.Rows[i].Cells[j].Value.ToString();

                  if( dataGridView3.Columns[j].HeaderText == "statusdate" || dataGridView3.Columns[j].HeaderText == "fplacementdate" || dataGridView3.Columns[j].HeaderText == "periodcomplertion"){
                  formatValue.convertDateFormat();
                  }

                  worksheet.Cells[i + 2, j + 1] = formatValue;
                  }
                  }





                  share|improve this answer













                  Correct me if I'm wrong, for what I saw in the provided screenshot, it consist of 3 date column which is statusdate, fplacementdate and periodcomplertion.



                  Have you tried it this way?



                  First create a conversion function for you date.



                  public static string convertDateFormat(this string date)
                  {
                  DateTime dateFormat = Convert.ToDateTime(date);
                  if (dateFormat != DateTime.MinValue)
                  {
                  return String.Format("{0:MM/dd/yyyy}", dateFormat);
                  }
                  else
                  {
                  return "";
                  }
                  }


                  Then use it this way



                  for (int i = 0; i < dataGridView3.Rows.Count; i++){
                  for (int j = 0; j < dataGridView3.Columns.Count; j++){
                  string formatValue = dataGridView3.Rows[i].Cells[j].Value.ToString();

                  if( dataGridView3.Columns[j].HeaderText == "statusdate" || dataGridView3.Columns[j].HeaderText == "fplacementdate" || dataGridView3.Columns[j].HeaderText == "periodcomplertion"){
                  formatValue.convertDateFormat();
                  }

                  worksheet.Cells[i + 2, j + 1] = formatValue;
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 6:43









                  klitzklitz

                  285




                  285






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474510%2fc-sharp-formatting-columns-when-exporting-data-into-excel%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)