I use the codes below to transfer data from datagridview to Excel. The codes are quite fast and useful. But the only problem is with the date format. How can we get data in columns containing dates in short date format?
C#
private void ToCsV(DataGridView dGV, string filename)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
for (int i = 0; i < dGV.RowCount; i++)
{
string stLine = "";
for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xls)|*.xls";
sfd.FileName = "Sinav Toplu Oturum Bilgileri.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name
}
MessageBox.Show("Sinav Toplu Oturum Bilgileri Excel formatinda masaüstüne kaydedildi!");
}
Jithu ThomasRank #437 · 5.7K reputationPosted Apr 21, 2024, 5:00 AM EDT
Also modify the code as per the below: -
private void ToCsV(DataGridView dGV, string filename) { Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR"); // Set culture for Turkish locale (optional) StringBuilder sb = new StringBuilder(); // Export titles: string sHeaders = ""; for (int j = 0; j < dGV.Columns.Count; j++) { sHeaders += dGV.Columns[j].HeaderText + "\t"; } sb.AppendLine(sHeaders); // Export data with formatted dates: for (int i = 0; i < dGV.RowCount; i++) { string stLine = ""; for (int j = 0; j < dGV.Rows[i].Cells.Count; j++) { object cellValue = dGV.Rows[i].Cells[j].Value; if (cellValue is DateTime) { // Format date using short date format for Turkish locale (optional) stLine += ((DateTime)cellValue).ToString("d", CultureInfo.GetCultureInfo("tr-TR")) + "\t"; } else { stLine += cellValue + "\t"; } } sb.AppendLine(stLine); } Encoding utf16 = Encoding.GetEncoding(1254); byte[] output = utf16.GetBytes(sb.ToString()); FileStream fs = new FileStream(filename, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(output, 0, output.Length); //write the encoded file bw.Flush(); bw.Close(); fs.Close(); }Jobin SRank #886 · 2.4K reputationPosted Apr 21, 2024, 5:00 AM EDT
string dateFormat = "dd/MM/yyyy";
Loop inside :
object cellValue = dGV.Rows[i].Cells[j].Value;
stLine += ((DateTime)cellValue).ToString(dateFormat) + "\t";
Jithu ThomasRank #437 · 5.7K reputationPosted Apr 21, 2024, 4:58 AM EDT
Please refer the below mentioned book from @Mahesh Chand
Mahesh Chand's Book - DateTime in C# link - https://www.c-sharpcorner.com/ebooks/datetime-in-csharp