简体   繁体   English

我需要 pivot 行数据从平面文件到 html 表

[英]I need to pivot rows of data from a flat file to an html table

Visual Studio with 2019 and C#带有 2019 和 C# 的 Visual Studio

I have a flat file with data in this format:我有一个包含这种格式数据的平面文件:

CustomerName<tab>CustomerAddress<tab>CustomerEmail<tab>CustomerPhone etc....
Tom<tab>123 Chestnut Street, NY, NY 10001<tab>Tom@gmail.com<tab>212-555-1212
Dick<tab>234 Oak Street, LA, CA 91210<tab>Dick@gmail.com<tab>747-555-1212
Harry<tab>789 Maple Drive, Reno, NV 89508<tab>Harry@yahoo.com<tab>775-555-1212
etc...

The columns can vary from about 5 to 15 columns, depending on the customer file given to us.这些列可以从大约 5 到 15 列不等,具体取决于提供给我们的客户文件。

Our users want to be able to preview a sample of the data, anywhere from 1 to 50 sample rows of data.我们的用户希望能够预览数据样本,从 1 到 50 个样本数据行不等。 However they insist on seeing the data pivoted on the page:但是他们坚持要在页面上查看数据:

Column1                  Column2             Column3              etc...
Tom                      Dick                Harry
123 Chestnut Street...   234 Oak Street...   789 Maple Drive...
Tom@gmail.com            Dick@gmail.com      Harry@yahoo.com
212-555-1212             747-555-1212        775-555-1212
etc...

I have this special customer class for this purpose:为此,我有这个特殊客户 class:

public class Customer {
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; } 
//etc.... to 50 columns
}

But I am having difficulty coming up with an elegant way to code this.但是我很难想出一种优雅的编码方式。 If I can get the data into a System.Data.DataTable in that pivoted form, I can easily get it to an html table.如果我能以这种旋转形式将数据放入 System.Data.DataTable 中,我就可以轻松地将其放入 html 表中。 Any thoughts on how to cycle through the rows of data to get it into the class?关于如何循环遍历数据行以将其放入 class 的任何想法? Not sure what are the best intermediate classes to use.不确定最好使用的中间类是什么。 I don't want to hard code something like this:我不想像这样硬编码:

...
for (int i = 0; i < columnCount; i++) {
  Customer c = new Customer();
  c.Column1 = row1[i].ToString();  
  c.Column2 = row2[i].ToString();  
  c.Column3 = row3[i].ToString();  
//etc...
}

My approach would be a little different, and would constrast from the data structure that you created.我的方法会略有不同,并且会与您创建的数据结构形成对比。 I would instead deserialize the flat file rows into an array objects containing an array of strings.相反,我会将平面文件行反序列化为包含字符串数组的数组对象。 This way I don't have to hard-code anything and my "row count" and my object data properties length is dynamic.这样我就不必硬编码任何东西,我的“行数”和我的 object 数据属性长度是动态的。

// this is for a row's data
public class RowObject
{
    public List<string> RowObjectProperty;
    public RowObject()
    {
        RowObjectProperty = new List<string>();
    }
}


// ===================================================================
// ... somewhere in a class which deserializes the flat file

List<RowObject> rows = new List<RowObject>();

// ...read data into the rows....

// ...print out the data for each row as your columns
foreach (RowObject customer in rows)
{
    foreach (string customerProperty in customer)
    {
        // here is each customer property (name, email, phone, etc)
        // ... create your column for this customer here     ​
   ​}
}
// ===================================================================

It's a bit rudimentary, but I feel should get you going in a better direction.这有点简陋,但我觉得应该让你朝着更好的方向前进。 Now, if you already know that the data structure (customer data) will have specific and consistent data fields/types , then I would parse the flat file like suggested here: https://stackoverflow.com/a/18479420/1188197现在,如果您已经知道数据结构(客户数据)将具有特定且一致的数据字段/类型,那么我将按照此处建议的方式解析平面文件: https://stackoverflow.com/a/18479420/1188197

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要从数据库表创建具有固定字段长度的平面文件 - I need to create a flat file from database table with fixed field length 如何使用C#将动态添加的行中的数据保存到(csv / txt)文件到HTML表格? - How to save data to a (csv/txt) file from dynamically added rows to a HTML table using C#? 从其他文件向数据表添加行 - Adding rows in data table from a different file 通用实体从平面文件加载数据 - Generic entities to load data from flat file 将SQL数据从过程写入平面文件 - Writing sql data from a procedure to flat file 将数据从平面文件加载到 Sql 服务器表,并使用 SSIS 导出到 excel - Load data from flat file to Sql Server table and also export to excel using SSIS 我如何使用LINQ从父/子表中获得数据的“固定”输出 - How can I get a “flat” output of data from a parent / child table with LINQ 我需要将数据从2列合并到另一列。 然后将添加的列的所有行合并到另一张表的一个单元格中 - I need to merge data from 2 columns to another column. And then merge all rows of that added column into one cell in another table 连接到本地数据库时出错,我需要将信息从CSV文件传递到localdb表C# - Error connecting to Local Data Base i need to pass info from CSV file into the localdb table c# 如何将平面数据文件转换为可查询数据源 - How do I turn a flat file of data into a queryable data source
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM