TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
RD Design
NA
25
26.7k
Why csv file doesnt show special characters when export?
May 31 2017 8:18 AM
Hi,
I have this code used on my application to export datagridview as csv file.It worked fine but csv doesnt show cell values correclty.See below images and I have pasted my code as well.What can I change on this code to fix this?
Here is the datagridview
This is the exported csv and it shows like this
Here is my code
public
void
writeCSV(DataGridView gridIn,
string
outputFile)
{
//test to see if the DataGridView has any rows
if
(gridIn.RowCount > 0)
{
string
value =
""
;
DataGridViewRow dr =
new
DataGridViewRow();
StreamWriter swOut =
new
StreamWriter(outputFile);
//write header rows to csv
for
(
int
i = 0; i <= gridIn.Columns.Count - 1; i++)
{
if
(i > 0)
{
swOut.Write(
","
);
}
swOut.Write(gridIn.Columns[i].HeaderText);
}
swOut.WriteLine();
//write DataGridView rows to csv
for
(
int
j = 0; j <= gridIn.Rows.Count - 2; j++)
{
if
(j > 0)
{
swOut.WriteLine();
}
dr = gridIn.Rows[j];
for
(
int
i = 0; i <= gridIn.Columns.Count - 1; i++)
{
if
(i > 0)
{
swOut.Write(
","
);
}
value = dr.Cells[i].Value.ToString();
//replace comma's with spaces
value = value.Replace(
','
,
' '
);
//replace embedded newlines with spaces
value = value.Replace(Environment.NewLine,
" "
);
swOut.Write(value);
}
}
swOut.Close();
}
}
Reply
Answers (
3
)
How to merge datagridview columns?
Access is denied