我目前正在C#中开发一个应用程序,我需要从它从MySQL数据库中检索的数据中编写一个制表符分隔的CSV文件。数据库检索工作正常。在c#中编写制表符分隔的csv文件
我遇到的问题是写入文件。在我写的每个变量之间,我使用了\ t,我认为将一个制表符放入csv中,因此,在Excel中打开时,每个变量都将位于其自己的单元格中。
但是由于某些原因,它并没有这样做,它只是将整行写成一个长字符串。以下是我编写的代码示例:
while (reader.Read())
{
int bankID = reader.GetInt16("ban_bankID");
int userID = reader.GetInt16("ban_userID");
string bankUsername = reader.GetString("ban_username");
string accountName = reader.GetString("ban_accountName");
string accountType = reader.GetString("ban_accountType");
decimal overdraft = reader.GetDecimal("ban_overdraft");
char defaultAccount = reader.GetChar("ban_defaultAccount");
string line = bankID + "\t" + userID + "\t" + bankUsername + "\t" + accountName + "\t"
+ accountType + "\t" + overdraft + "\t" + defaultAccount + "\n";
tw.WriteLine(line);
感谢您对此问题的帮助。
+1我在盯着这个问题思考:“输出没什么问题,它必须是它的加载方式”,但完全错过了提到CSV文件但用tab分开的问题。 – 2010-11-13 02:07:43