本文介绍了使用列标题和行的正确格式编写CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被分配了一个任务,我从数据库中的数据库中取出一些记录然后将其导出到.csv文件。文件写得正确,但格式不存在我的意思是一切都只在单元格上。
可以在我错误的地方涂抹plsss帮助吗?
我的代码在这里是gvn: -
Hi,
I have been assigned a task where I am fetching some records from Database in data table and then I am exporting it to .csv file. the file is being written properly but the formatting is not there i mean everything is coming under on cell only.
Can smeone plsss help where m I going wrong ??
My code is gvn here:-
private static void CreateCSVFile(DataTable dt, string strFilePath, string webServerFileLink)
{
// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(strFilePath, false);
// First we will write the headers.
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if ((!Convert.IsDBNull(dr[i])) && (i == 7))
{
sw.Write(dr[i].ToString().Replace("\r", " "));
}
else
{
sw.Write(dr[i].ToString().Replace(",", " "));
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
代码格式。
Code formatting.
推荐答案
这篇关于使用列标题和行的正确格式编写CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!