如何处理类似excel工作表在C#以及结构化文本 [英] How to process a well structured text similar to excel sheet in c#

查看:147
本文介绍了如何处理类似excel工作表在C#以及结构化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个文本文件中读取数据,并将其保存到数据库中通过使用ado.net code。文本文件包含表格数据:列,行格式,但我不知道哪里出了问题,我的思念,从文本文件中的某些DATAS。所使用的code如下所示

 字符串文件名= @D:\ EMS_DATA \ firstfile.txt;
 字符串[]行= NULL;
 字符串[] COLS = NULL;
 INT计数器= 0;
 使用(StreamReader的读者=新的StreamReader(文件名))
 {
     字符串[]行= File.ReadAllLines(文件名)。凡(ARG =>!string.IsNullOrWhiteSpace(ARG))。的ToArray();
     COLS =行[0] .Trim()斯普利特(新[] {'\ T'},StringSplitOptions.RemoveEmptyEntries)。

     的foreach(在lines.Skip串线(1))
     {
         行= line.Trim()斯普利特(新[] {'\ T'},StringSplitOptions.RemoveEmptyEntries)。

         的for(int i =计数器; I< cols.Length;我++)
         {
             对于(INT J =计数器; J< rows.Length; J ++)
             {
                 Console.WriteLine(值{0} {1},COLS [计数器],行[窗口]);
                 ++计数器;
                 打破;
             }
         }
     }
 }
 

Texfile是如下

格式

 名姓工资时代
SASI基兰88000 32
拉维·基兰92000 23
jafer谢里夫34000 45
基兰贝迪45000 34
 

解决方案

首先删除的StreamReader 如果你不打算使用它。其次,你不需要两个嵌套循环。然后实际使用循环变量在code。还防止具有比头更少的条目行。最后,您可以添加一个变量来跟踪当前行。

 字符串文件名= @D:\ EMS_DATA \ firstfile.txt;
字符串[]行= File.ReadAllLines(文件名)
    。凡(ARG =>!string.IsNullOrWhiteSpace(ARG))。的ToArray();
字符串[] COLS =行[0] .Trim()
    .Split(新[] {'\ T'},StringSplitOptions.RemoveEmptyEntries);

INT行= 1;
的foreach(在lines.Skip串线(1))
{
    字符串[]单元= line.Trim()
        .Split(新[] {'\ T'},StringSplitOptions.RemoveEmptyEntries);
    对于(INT计数器= 0;反< cols.Length;反++)
    {
        字符串cellValue =N / A;
        如果(柜< cells.Length)
            cellValue =细胞[窗口];
        Console.WriteLine(
            在行值{0}列{1}在{2} {3},
            线,
            计数器,
            COLS [计数器]
            cellValue);
    }

    行++;
}
 

I was fetching data from a text file and saving it to a database by using ado.net code. Textfile contains data in the form: column , row format, but I dont know what went wrong, I am missing certain datas from textfile. The code used is shown below

 string filename = @"D:\EMS_DATA\firstfile.txt";
 string[] rows = null;
 string[] cols = null;
 int counter = 0;
 using (StreamReader reader = new StreamReader(filename))
 {
     string[] lines = File.ReadAllLines(filename).Where(arg => !string.IsNullOrWhiteSpace(arg)).ToArray();
     cols = lines[0].Trim().Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

     foreach (string line in lines.Skip(1))
     {
         rows = line.Trim().Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

         for (int i = counter; i <cols.Length; i++)
         {
             for (int j = counter; j <rows.Length; j++)
             {
                 Console.WriteLine("values are {0}{1}", cols[counter], rows[counter]);
                 ++counter;
                 break;
             }
         }
     }
 }

Texfile is in the format shown below

firstname  lastname  salary  age
sasi   kiran    88000   32
ravi   kiran    92000   23
jafer  sharif   34000   45
kiran  bedi     45000   34

解决方案

First remove the StreamReader if you are not going to use it. Second you don't need two nested for loops. Then actually use the for loop variable in your code. Also guard against lines that have fewer entries than the header. Finally you can add a variable to keep track of the current line.

string filename = @"D:\EMS_DATA\firstfile.txt";
string[] lines = File.ReadAllLines(filename)
    .Where(arg => !string.IsNullOrWhiteSpace(arg)).ToArray();
string[] cols = lines[0].Trim()
    .Split(new[] {'\t'}, StringSplitOptions.RemoveEmptyEntries);

int line = 1;
foreach (string line in lines.Skip(1))
{
    string[] cells = line.Trim()
        .Split(new[] {'\t'}, StringSplitOptions.RemoveEmptyEntries);
    for (int counter = 0; counter < cols.Length; counter++)
    {
        string cellValue = "N/A";
        if(counter < cells.Length)
            cellValue = cells[counter];
        Console.WriteLine(
            "values at row {0} column {1} are {2} : {3}", 
            line, 
            counter, 
            cols[counter], 
            cellValue);
    }

    line++;
}

这篇关于如何处理类似excel工作表在C#以及结构化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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