如何在excel csv文件中添加两个表?

时间:2022-06-29 10:17:49

I am creating excel CSV file using the following code its working fine for me generating the first image table.But i want to generate the CSV for the image second.Can anyone please tell me how can i add two tables in single file and how can i insert record where no heading of column and how can i add a emty row and column in excel csv?

我正在使用以下代码创建excel CSV文件,它对生成第一个映像表很有用。但是我想要为图像生成CSV。请告诉我如何在单个文件中添加两个表,以及如何插入没有列标题的记录,以及如何在excel csv中添加一个emty行和列?

如何在excel csv文件中添加两个表?

CsvExport myExport = new CsvExport();
string fileName = "AbsentEmployeesReport" + Session["id"] + ".csv";
foreach (var item in lstEmployees)
{
myExport.AddRow();
myExport["Enroll ID"] = item.EnrollNumber;
myExport["Employee (Firt and Last)"] = item.Name;
myExport["Department"] = item.Department;
myExport["Absent Count"] = item.AbsentCount;
}
string myCsv = myExport.Export();
myExport.ExportToFile(Server.MapPath("/Reports/" + fileName), myCsv);

using System;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
using System.Collections.Generic;

/// <summary>
/// Simple CSV export
public class CsvExport
{
  /// <summary>
  /// To keep the ordered list of column names
  /// </summary>
  List<string> fields = new List<string>();

  /// <summary>
  /// The list of rows
  /// </summary>
  List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();

  /// <summary>
  /// The current row
  /// </summary>
  Dictionary<string, object> currentRow { get { return rows[rows.Count - 1]; } }

  /// <summary>
  /// Set a value on this column
  /// </summary>
  public object this[string field]
  {
    set
    {
      // Keep track of the field names, because the dictionary loses the ordering
      if (!fields.Contains(field)) fields.Add(field);
      currentRow[field] = value;
    }
  }

  /// <summary>
  /// Call this before setting any fields on a row
  /// </summary>
  public void AddRow()
  {
    rows.Add(new Dictionary<string, object>());
  }

  /// <summary>
  /// Converts a value to how it should output in a csv file
  /// If it has a comma, it needs surrounding with double quotes
  /// Eg Sydney, Australia -> "Sydney, Australia"
  /// Also if it contains any double quotes ("), then they need to be replaced with quad quotes[sic] ("")
  /// Eg "Dangerous Dan" McGrew -> """Dangerous Dan"" McGrew"
  /// </summary>
  string MakeValueCsvFriendly(object value)
  {
    if (value == null) return "";
    if (value is INullable && ((INullable)value).IsNull) return "";
    if (value is DateTime)
    {
      if (((DateTime)value).TimeOfDay.TotalSeconds==0)
        return ((DateTime)value).ToString("yyyy-MM-dd");
      return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
    }
    string output = value.ToString();
    if (output.Contains(",") || output.Contains("\""))
      output = '"' + output.Replace("\"", "\"\"") + '"';
    return output;
  }

  /// <summary>
  /// Output all rows as a CSV returning a string
  /// </summary>
  public string Export()
  {
    StringBuilder sb = new StringBuilder();

    // The header
    foreach (string field in fields)
      sb.Append(field).Append(",");
    sb.AppendLine();

    // The rows
    foreach (Dictionary<string, object> row in rows)
    {
      foreach (string field in fields)
        sb.Append(MakeValueCsvFriendly(row[field])).Append(",");
      sb.AppendLine();
    }

    return sb.ToString();
  }

  /// <summary>
  /// Exports to a file
  /// </summary>
  public void ExportToFile(string path)
  {
    File.WriteAllText(path, Export());
  }

  /// <summary>
  /// Exports as raw UTF8 bytes
  /// </summary>
  public byte[] ExportToBytes()
  {
    return Encoding.UTF8.GetBytes(Export());
  }
}

How can i generate excel CSV like following: 如何在excel csv文件中添加两个表?

如何生成excel CSV如下:

1 个解决方案

#1


1  

Well, CSV stands for comma separated values, if you open up your generated file through note pad you will have a real look what those data really looks like and you will have an idea on what your asking for.

CSV表示逗号分隔的值,如果你通过记事本打开你生成的文件你会看到这些数据是什么样子的你会知道你想要什么。

  1. to insert empty row you just do "","","",""
  2. 要插入空行,只需执行"" "" "" ""
  3. To insert empty column (let's say, first column is empty) you do "","data1","data2"
  4. 要插入空列(比方说,第一列是空的),需要“”、“data1”、“data2”
  5. to insert your new table 2, you do the same as creating your table1 but you insert your table heads first after the table1. so the data should like this:
  6. 为了插入新表2,您可以创建表1,但在表1之后插入表头。所以数据应该是这样的:

column1-1,column1-2,column1-3
datat1-1,"data1-2,data11-3
column2-1,column2-2,column2-3
data12-1,data12-2,data12-3"
.....

column1-1,column1-2,column1-3 datat1-1,“data1-2,data11-3 column2-1,column2-2,column2-3 data12-1,data12-2,data12-3”

#1


1  

Well, CSV stands for comma separated values, if you open up your generated file through note pad you will have a real look what those data really looks like and you will have an idea on what your asking for.

CSV表示逗号分隔的值,如果你通过记事本打开你生成的文件你会看到这些数据是什么样子的你会知道你想要什么。

  1. to insert empty row you just do "","","",""
  2. 要插入空行,只需执行"" "" "" ""
  3. To insert empty column (let's say, first column is empty) you do "","data1","data2"
  4. 要插入空列(比方说,第一列是空的),需要“”、“data1”、“data2”
  5. to insert your new table 2, you do the same as creating your table1 but you insert your table heads first after the table1. so the data should like this:
  6. 为了插入新表2,您可以创建表1,但在表1之后插入表头。所以数据应该是这样的:

column1-1,column1-2,column1-3
datat1-1,"data1-2,data11-3
column2-1,column2-2,column2-3
data12-1,data12-2,data12-3"
.....

column1-1,column1-2,column1-3 datat1-1,“data1-2,data11-3 column2-1,column2-2,column2-3 data12-1,data12-2,data12-3”