从CSV读取数据到屏幕输出 [英] Reading Data from CSV to Screen output

查看:168
本文介绍了从CSV读取数据到屏幕输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不准,但我会问反正。

我试图开发类似,但鸭绿江为自定义日志的产品。

我有我要读的表中可以滚动CSV文件,并显示在屏幕上,过滤,有色等类似鸭绿江。

我有code读取和解密CSV和在一个整洁格式到另一种文件输出。我想这个适应它到屏幕上。

我在C#和C经验,但唯一的控制台有关。可能有人指导我的诠释,他正确的方向至少可以得到在一个窗口中读取类似的格式文件,以鸭绿江?什么是我的可能的措施?我不能确定从哪里开始....

感谢


解决方案

code以下结果显示在一个DataGridView

\r
\r
使用系统

;\r
使用System.Collections.Generic;\r
使用System.ComponentModel;\r
使用System.Data这;\r
使用System.Drawing中;\r
使用System.Linq的;\r
使用System.Text;\r
使用System.Windows.Forms的;\r
使用System.IO;\r
使用System.Data.OleDb;\r
\r
命名空间WindowsFormsApplication1\r
{\r
    公共部分Form1类:表格\r
    {\r
        公共Form1中()\r
        {\r
            的InitializeComponent();\r
        }\r
\r
        常量字符串文件名= @C:\\ TEMP \\ test.csv\r
        私人无效的button1_Click(对象发件人,EventArgs的发送)\r
        {\r
            CSVReader csvReader =新CSVReader();\r
            DataSet的DS = csvReader.ReadCSVFile(文件名,真实);\r
            dataGridView1.DataSource = ds.Tables [表1];\r
        }\r
    }\r
    公共类CSVReader\r
    {\r
\r
        公共数据集ReadCSVFile(字符串完整路径,布尔headerRow)\r
        {\r
\r
            串路径= fullPath.Substring(0,fullPath.LastIndexOf(\\\\)+1);\r
            字符串文件名= fullPath.Substring(fullPath.LastIndexOf(\\\\)+1);\r
            DataSet的DS =新的DataSet();\r
\r
            尝试\r
            {\r
                如果(File.Exists(完整路径))\r
                {\r
                    字符串构造=的String.Format(供应商= Microsoft.Jet.OLEDB.4.0;数据源= {0}+;扩展属性= \\文本; HDR = {1}; FMT =分隔\\\\\\ ?路径,headerRow是:否);\r
                    字符串SQL =的String.Format(SELECT * FROM {0},文件名);\r
                    OleDbDataAdapter的适配器=新OleDbDataAdapter的(SQL,构造);\r
                    adapter.Fill(DS,文本文件);\r
                    ds.Tables [0] .TableName =表1;\r
                }\r
                的foreach(DataColumn的山坳中ds.Tables [表1]。列)\r
                {\r
                    col.ColumnName = col.ColumnName.Replace(,_);\r
                }\r
            }\r
\r
            赶上(异常前)\r
            {\r
                MessageBox.Show(ex.Message);\r
            }\r
            返回DS;\r
        }\r
    }\r
}\r
\r

\r

\r
\r

This may not be allowed but I'll ask anyway.

I'm trying to develop a product similar to YALV but for a custom logs.

I have a CSV file that I want to read in and display on screen in a table that can be scrolled, filtered, coloured, etc similarly to YALV.

I have code for reading and deciphering the CSV and outputting in a neat format to another file. I want to adapt this to get it to screen.

I have experience in C# and C but only console related. Could someone guide me int he right direction to at least get the file being read in a Window in a similar format to YALV? What would be my possible steps? I'm unsure where to start....

Thanks

解决方案

Code below displays results in a DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const string FILENAME = @"c:\temp\test.csv";
        private void button1_Click(object sender, EventArgs e)
        {
            CSVReader csvReader = new CSVReader();
            DataSet ds = csvReader.ReadCSVFile(FILENAME, true);
            dataGridView1.DataSource = ds.Tables["Table1"];
        }
    }
    public class CSVReader
    {

        public DataSet ReadCSVFile(string fullPath, bool headerRow)
        {

            string path = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
            string filename = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
            DataSet ds = new DataSet();

            try
            {
                if (File.Exists(fullPath))
                {
                    string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}" + ";Extended Properties=\"Text;HDR={1};FMT=Delimited\\\"", path, headerRow ? "Yes" : "No");
                    string SQL = string.Format("SELECT * FROM {0}", filename);
                    OleDbDataAdapter adapter = new OleDbDataAdapter(SQL, ConStr);
                    adapter.Fill(ds, "TextFile");
                    ds.Tables[0].TableName = "Table1";
                }
                foreach (DataColumn col in ds.Tables["Table1"].Columns)
                {
                    col.ColumnName = col.ColumnName.Replace(" ", "_");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ds;
        }
    }
}

​

这篇关于从CSV读取数据到屏幕输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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