使用列标题和行的正确格式编写CSV文件 [英] writing a CSV file with proper formatting of columns headers and rows

查看:72
本文介绍了使用列标题和行的正确格式编写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文件是否格式化?谁手动与他们合作?它们主要用于将数据从一个系统传输到另一个系统或类似的东西。毕竟,创建一些代表任何CSV的软件,或者只使用一些可用的电子表格软件。或者将CSV转换为HTML。



文本文件并非真正用于呈现类似表格的数据。您的方法基于固定宽度字体,很少使用。如果你使用制表符,它可能在一个编辑器中看起来很好,并且因为制表符宽度不同而在另一个编辑器中被破坏。你只是处理污垢。不要浪费你的时间。



-SA
I think this is a waste of your time. Who cares if CSV files are formatted well or not? Who works with them manually? They a mostly used to transfer data from one system to another or something like that. After all, create some software which represents any CSV in a form of some data grid or just use some available spreadsheet software. Or convert CSV to HTML.

Text files are not really designed to present table-like data. Your method is based on fixed-width font, which are rarely used. If you use tabs, it may look good in one editor and gets mangled in another one just because the tab widths are different. You are just dealing with dirt. Don't waste your time on it.

—SA


如果你的意思是您在Excel中打开CSV文件,everuthing在一个单元格中。如果是这种情况,最有可能的原因是列表分隔符中存在不匹配。打开CSV时,请检查您是否使用了正确的分隔符。
If you mean that when you open the CSV file in Excel, everuthing is in one cell. If that's the case, the likeliest reason is that you have a mismatch in the list separator. When opening the CSV, check that you're using the correct delimiter.


这篇关于使用列标题和行的正确格式编写CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆