读取CSV文件,然后使用变量以表格格式显示 [英] Read CSV Files, then Display in Table format using variables

查看:116
本文介绍了读取CSV文件,然后使用变量以表格格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以表格格式读取和显示csv文件中的数据。然后,我将需要使用这些数据来计算不同的值。

这是CSV文件的外观:

展开 | 选择 | Wrap | 行号

解决方案

您发布的CSV文件示例不是逗号分隔值(CSV)格式。


如果您的文件实际上看起来像您发布的内容,您只需使用Response.Write来编写整个将内容内容存储到输出流中,它看起来像一个表(因为该文件包含一个HTML表)。


无论您的文件与文件类型不匹配你是谁描述,你正在做的事情很好:将文件转换为内存中的.NET数据表,并将其指定为GridView的源代码,在浏览器中显示为表格。


您还可以将GridViews的源绑定到不是DataTable类型的源。


例如,您可以绑定到自定义的列表或数组如果你愿意,可以使用金属对象。


如果您有以下自定义Metal对象:

展开 | 选择 | Wrap | 行号


有点困惑与你的模型一起。

数据在CSV文件中我只把它变成了一个表让你理解。

我有2个CSV,第一个应该显示金属类型以磅为单位的价格。这些项应显示在DataGrid中。

下一个CSV有欧元和美元的汇率,我应该使用(读取)汇率并使用(乘以)DataGrid列2中的磅率值,并得到第3列和第3列的总值; 4:

展开 | 选择 | Wrap | < span class =codeLinkonclick =LineNumbers(this);>行号


就像我说的,做你原来的事情没有错...你问了一个不同的方法。


这个模型有什么令人困惑的事?

模型类负责你的计算。

如果您需要从多个文件中读取数据,然后从文件中获取您收集的信息,并使用为您进行处理所需的数据填充该类。


然后在ASP标记代码中您可以将GridView的UI绑定到属性或执行方法来执行完成任务所需的计算。如果你不使用模型方法,那么在数据绑定期间使用GridView的ASP标记进行计算仍然是一个好主意。


-Frinny

I need to read and display data from a csv file in a table format. I then will need to use these data to calculate different values.
This is how the CSV file looks:

Expand|Select|Wrap|Line Numbers

解决方案

The CSV file example you posted is not in Comma Separated Values (CSV) format.

If your file actually looks like what you posted you can simply use Response.Write to write the entirety of the file contents contents into the output stream and it will look like a table (because the file contains an HTML table).

Regardless of the fact that your file doesn''t match the type that you are describing, what you are doing is fine: converting the file into an in-memory .NET data table and assigning it as the source of a GridView which displays as a table in the browser.

You an also bind the source of GridViews to sources that are not a DataTable type though.

For example, you can bind to a list or array of custom Metal objects if you would like.

Like, if you had the following custom Metal object:

Expand|Select|Wrap|Line Numbers


Bit confused with your model.
The data is in a CSV file I only made it into a table so you understand.
I have 2 CSV, first one should display the metal type and the price in pounds. These item should be shown in DataGrid.
The next CSV has rates in Euro and Dollar, I should use (read) the rates and use (multiply) by the pound rate value in the DataGrid column 2 and get a total value on column 3 & 4:

Expand|Select|Wrap|Line Numbers


Like I said, there is nothing wrong with doing what you were originally... you asked for a different approach though.

What is confusing about the model?
The model class takes care of the calculations for you.
If you need to read from multiple files you can and then take the information that you gather from the files to populate the class with the data required to do the processing for you.

Then in the ASP markup code for the UI for the GridView you can bind to the properties or execute methods to do the calculations that you need to do to accomplish your task. If you don''t use the model approach, it will still be a good idea to use the ASP markup for the GridView to do calculations during data binding.

-Frinny


这篇关于读取CSV文件,然后使用变量以表格格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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