简体   繁体   中英

How do I delete certain column from .csv file

I am downloading the list of data but some i would like to ignore some columns, is there any way i can remove them, this is how my database look:

    ID  Name   Sname   MobileUsage   NoBrought
    1   test   test    12mb          1
    2   test1  test1   23mb          8
    3   test2  test2   20mb          2

This is what i am getting when i download a .csv file

    ID  Name   Sname   MobileUsage   NoBrought
    1   test           12mb           
    2   test1          23mb           
    3   test2          20mb   

Is there any way i can delete Sname and NoBrought as i am not using, my output should be

    ID  Name   MobileUsage   
    1   test   12mb           
    2   test1  23mb           
    3   test2  20mb  

This is what i have done, i assume this program have to look for column heading eg Sname & NoBrought, delete the entire column and shift left. This event occurs when a user clicks a button, as a result the deleting/editing should happen when download button clicked.

protected void Page_Load(object sender, EventArgs e)
{
    CsvExport<l> csv = new CsvExport<l>(getList());
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=test.csv");
    Response.ContentType = "text/plain";
    Response.Output.Write(csv.Export());
    Response.End();
}

public List<l> getList()
{
    List<l> n = new List<l>();
    List<string> nl = new List<string>();
    SqlConnection conn = new SqlConnection(Connection goes here...);
    SqlCommand command = new SqlCommand("select ID, Name, MobileUsage FROM TableName");

    conn.Open();
    SqlDataReadData ReadData = command.ExecuteReader();
    while (ReadData.Read())
    {
        l data = new l();
        data.ID = ReadData["ID"].ToString();
        data.Name = ReadData["Name"].ToString();
            etc...
        n.Add(data);
    }
    conn.Close();
    return n;
}

I won't go into detail on how you get your data. So lets just assume you get a csv file.

//class to strongly type our results
public class csvClass
{
   public csvClass(string name; string mu)
   {
     this.Name = name; this.MobileUsage = mu;
   }
   public string Name { get; set; }
   public string MobileUsage { get; set; }
}

//just load your csv from wherever you need
var csvData = from row in File.ReadLines(@"Path/to/file.csv")
              // data is still in one line. Split by delimiter
              let column = row.Split(';')
              //strongly type result
              select new csvClass
              {
                  //Ingore column 2 and 4
                  //Take first column
                  Name = column[0],
                  //Take third column
                  MobileUsage = column[2]
              };

After this you should have an IEnumerable<csvClass>() with just the 2 columns you want, which you can write to anywhere you want - new csv-file, database ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM