从C#导出到excel日期问题即将来临 [英] Exporting from C# to excel date problem is coming
本文介绍了从C#导出到excel日期问题即将来临的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将C#数据库表导出为excel。出口没问题。但是出口后问题是
我检查了那个excel文件。在那一个日期列中。有日期显示时间如
I am exporting C# database table to excel. Exporting is ok. But what is the problem is
after exporting I checked that excel file. in that one date column is there. there date display with time like
21-07-2017 12:00:00 AM
日期栏中的所有数据都只显示那样。为什么会这样,以及如何停止那个。
我的代码是
All data in date column is displaying like that only.Why is coming like that and how to stop that.
my code is
private void btnexport_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = "c:";
saveFileDialog1.Title = "Save AS Excel";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "Excel File 2003|*.xls|Excel File 2007|*.xlsx";
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Microsoft.Office.Interop.Excel.Application Excelapp = new Microsoft.Office.Interop.Excel.Application();
Excelapp.Application.Workbooks.Add(Type.Missing);
Excelapp.Columns.ColumnWidth = 20;
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
Excelapp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j =0; j < dataGridView1.Columns.Count; j++)
{
Excelapp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
Excelapp.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
Excelapp.ActiveWorkbook.Saved = true;
Excelapp.Quit();
}
我的尝试:
Igoogled但找不到合适的结果
What I have tried:
Igoogled but no suitable result found
推荐答案
使用此代码
use this code
private void ExportToExcel()
{
try
{
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;
app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Records";
try
{
for (int i = 0; i < dgvCus.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dgvCus.Columns[i].HeaderText;
}
for (int i = 0; i < dgvCus.Rows.Count; i++)
{
for (int j = 0; j < dgvCus.Columns.Count; j++)
{
if (dgvCus.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dgvCus.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
}
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveDialog.FilterIndex = 2;
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
app.Quit();
workbook = null;
worksheet = null;
}
}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
这篇关于从C#导出到excel日期问题即将来临的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文