从 CSV 读取数据到屏幕输出 [英] Reading Data from CSV to Screen output
问题描述
这可能不允许,但我还是会问.
This may not be allowed but I'll ask anyway.
我正在尝试开发一种类似于 YALV 的产品,但用于自定义日志.
I'm trying to develop a product similar to YALV but for a custom logs.
我有一个 CSV 文件,我想读取它并在屏幕上显示在一个表格中,该表格可以像 YALV 一样滚动、过滤、着色等.
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.
我有用于读取和解密 CSV 并以整洁的格式输出到另一个文件的代码.我想对其进行调整以使其出现在屏幕上.
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.
我有 C# 和 C 方面的经验,但只与控制台相关.有人可以指导我在正确的方向上至少以与 YALV 类似的格式在窗口中读取文件吗?我可能的步骤是什么?我不确定从哪里开始......
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....
谢谢
推荐答案
下面的代码在 DataGridView 中显示结果
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: emp est.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屋!