C# Formatting columns when exporting data into excel
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:
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
add a comment |
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:
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
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 betoo
helpful sometimes. you could set the exel column for that date as'text
and export the date into that to stop it doing ahelpful
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
add a comment |
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:
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
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:
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
c# mysql excel
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 betoo
helpful sometimes. you could set the exel column for that date as'text
and export the date into that to stop it doing ahelpful
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
add a comment |
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 betoo
helpful sometimes. you could set the exel column for that date as'text
and export the date into that to stop it doing ahelpful
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
add a comment |
2 Answers
2
active
oldest
votes
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"
add a comment |
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;
}
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 26 '18 at 4:04
TownDrinTownDrin
32
32
add a comment |
add a comment |
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;
}
}
add a comment |
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;
}
}
add a comment |
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;
}
}
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;
}
}
answered Nov 26 '18 at 6:43
klitzklitz
285
285
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 ahelpful
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