1
我試圖將數據導出到csv。一切工作正常,但是,我的輸出被放在一列。它很好地移動到下一行等,但我不知道如何把它放在新的列。此外,我不能預定義列的數量,因爲這取決於數據庫。我幾乎可以肯定,我應該改變一件小事。將csv的內容移動到stringbuilder中的不同列
StringBuilder csvContent = new StringBuilder();
ClassDeclarationsDBEntities entities6 = new ClassDeclarationsDBEntities();
var subj = entities6.Subjects.Where(a => a.class_id == model.class_id).FirstOrDefault();
csvContent.AppendLine(subj.name.ToString());
csvContent.Append("Name,Surname");
foreach (var task in current)
{
csvContent.Append(task.name.Replace(" ",string.Empty)+",");
}
csvContent.AppendLine("Total");
foreach (var user in curr_users)
{
if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
{
csvContent.Append(user.name.Replace(" ", String.Empty) + ",");
csvContent.Append(user.surname.Replace(" ", String.Empty) + ",");
foreach (var task in current)
{
foreach (var grade in model.grading)
{
if (user.user_id == grade.user_id)
{
if (task.task_id == grade.task_id)
{
inside = grade.points;
i = i + grade.points;}}}
csvContent.Append(inside.ToString()+",");
inside = 0;}
csvContent.AppendLine(i.ToString()+",");}
i = 0;}
byte[] buffer = Encoding.ASCII.GetBytes(csvContent.ToString());
return File(buffer, "application/CSV", "Report.csv");
Excel使用語言環境列表分隔符。逗號在美國,但它可能是你的國家的其他東西。檢查TextInfo.ListSeparator –