一旦数据经过检查,尝试从excel读取数据并重写为csv。 [英] Trying to read data from excel and rewrite into a csv, once the data has been checked through.

查看:84
本文介绍了一旦数据经过检查,尝试从excel读取数据并重写为csv。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我正在尝试前进的东西,但我正在尝试。

I am new to C# and I am trying to probably have ago at something to advanced but I am trying.

我知道我想做什么,而且我有关于它的想法,因此我把下面的代码放在一起,但我不知道如何将它们全部链接并添加我需要的相关位。

I know what I want to do, and I have ideas around it, hence me putting together the code below, but I do not know how to link it all and add the relevant bits I need.

我想要实现的是,打开记录列表,Excel文件。验证它们,然后检查行,第一行是标题,下一行是数字,下一行是空白,继续检查,如果超过10个空白行,移动到下一个文件等,然后一次
it已经完成了所有这些工作,然后将它读取的数据放入csv文件中。

What I am trying to achieve is, open a list of records, excel files. Validate them, then check the rows, is the first row a title, is the next row numeric, is the next row blank, keep checking, if more than 10 blank rows, move onto next file etc, then once it has worked through all this the data it reads is then placed into a csv file.

我知道我需要一些额外的x = 1,但是有很多不同的方式,我只是从地方获得了片段并提出了粗略的指南

I know i need some x=1 extra but there is so much different ways, I have just got snippets from places and come up with a rough guide

如果有人可以指导我,我们将不胜感激。

If anyone can guide me, it would be much appreciated.

Records = openExcelFile
List<daf> dafs //this is the starting point, getting my files ready and open to read

Foreach (record in records)
{
   // what type of row we have?
    rowType = ValiateRow(record);

    switch (rowTYpe)
      {
        Case RowType.Header:
        Continue;    //move to the next record
        Case RowType.Blank:
        blankRows++;
        
if (blankRows > maxBLankRows)
               return;   // We are at the end
          else
            continue;             //next record
               Case RowType.Valid:
                DAF daf = ConvertTOCSV(record)
           If (CSV!= null)
            
 CSV.add(csv);
            Continue;
               }             
}  // can this be done for each recrd/file?

推荐答案

您好,

如果工作表是结构化的,例如你知道列A是名字,列B是姓氏,列C是出生日,例如然后看看
以下库仅适用于.xlsx。

If the sheet is structured e.g. you know that column A is first name, column B is last name, column C is Birth day e.g then look at the following library which works only for .xlsx.

所以我会读如下

public List<Customer> ClassFromExcelDemo(string pFileName)
{
    List<Customer> customers = new List<Customer>();
    var readerService = new ClassToExcelReaderService<Customer>();
    customers = readerService.ReadWorksheet(pFileName, "Customers", true);
    readerService = null;
    return customers;
}

使用客户类 

With Customer class 

namespace ConcreteLibrary
{
    public class Customer
    {
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
    }
}

然后一旦数据返回迭代列表,在这种情况下我可能会寻找空单元格值例如并且可以编写一个lambda语句来查找它们或者只是在for-each中迭代。在这里,我使用了一个可视化的DataGridView,不需要你。

Then once the data comes back iterate the list, in this case I might be looking for empty cell values e.g. and could write a lambda statement to find them or simply iterate in a for-each. Here I used a DataGridView for a visual, for you that is not needed.


这篇关于一旦数据经过检查,尝试从excel读取数据并重写为csv。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆