如何安排下载的信息以适应行

本文关键字:信息 何安排 下载 | 更新日期: 2023-09-27 18:09:32

SqlConnection connection = new SqlConnection(@"Data Source=localhost'SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");
SqlCommand Command = connection.CreateCommand();
SqlDataReader SQLRD;
Command.CommandText = "Select * from Attendance";
connection.Open();
SQLRD = Command.ExecuteReader();
string data = "";
while (SQLRD.Read())
{
    data += SQLRD[0].ToString()+  "'n";
    data += SQLRD[1].ToString() + "'n";
    data += SQLRD[2].ToString() + "'n";
    data += SQLRD[3].ToString() + "'n";
    data += SQLRD[4].ToString() + "'n";
    data += SQLRD[5].ToString() + "'n";
    data += SQLRD[6].ToString() + "'n";
    data += SQLRD[7].ToString() + "'n";
}
SQLRD.Close();
connection.Close();
string filename = @"C:'download.csv";//specified location
FileStream fs = new FileStream(filename,FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(data);
sw.Flush();
sw.Close();
fs.Close();
这是我到目前为止所做的。目前,在点击下载时,所有信息都显示在1列中,而不是分成行。我需要知道如何把它们排成一行。我也可以知道我如何显示一个对话框,当用户点击下载。当前文件只是存储在指定的位置?

如何安排下载的信息以适应行

导出为。csv格式时,要在列之间使用逗号(,),在行之间使用行返回符('n)。目前,您在每个列之间放置一行返回。试试这样做:

while (SQLRD.Read())
{
     data += SQLRD[0].ToString() + ",";
                   //              ^^^ note the change from ''n' to ','
     data += SQLRD[1].ToString() + ",";
     data += SQLRD[2].ToString() + ",";
     ...
     data += SQLRD[7].ToString(); // final column doesn't need a ','
     data += "'n"; // final line separator for the entire row
}

问好,