我怎样才能在excel csv文件中添加两个表?

我正在使用下面的代码创buildexcel CSV文件,其工作正常,生成第一个图像表。但我想为图像生成第二个CSV。可以请任何人告诉我如何在单个文件中添加两个表,以及如何我插入logging哪里没有列的标题,我怎么能在Excel中添加一个emty行和列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()); } } 

我怎样才能生成excel CSV如下所示: 在这里输入图像说明

那么,CSV代表逗号分隔值,如果你通过记事本打开你生成的文件,你将有一个真实的看看这些数据真的看起来像,你会对你的要求有一个想法。

  1. 插入空行你只是做“”,“”,“”,“”
  2. 要插入空列(比方说,第一列是空的)你做“”,“data1”,“data2”
  3. 要插入新的表格2,则与创build表格1相同,但是在table1之后首先插入表格。 所以数据应该是这样的:

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