Excel etcがあれば、CSVファイルを見るのは簡単なんですが、Excel etcがインストールされていないパソコンもあるので、そういうときには使えるかもしれません。
ご自由にご利用ください。例によって、動作保証はしませんが。
(めちゃくちゃ行数が大きいファイルとかでどうなるかは知りません。)
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
public class Program
{
[STAThread]
public static void Main()
{
Application.Run( new FormCSV());
}
}
public class FormCSV : Form
{
private MenuStrip ms;
private ToolStripMenuItem[][] tsmi;
private DataGridView dgv;
public FormCSV()
{
this.ClientSize = new System.Drawing.Size( 360, 360);
/*--------------------------------------------------*/
this.ms = new MenuStrip();
this.tsmi = new ToolStripMenuItem[1][];
this.tsmi[0] = new ToolStripMenuItem[3];
for( int i = 0; i < this.tsmi.Length; i++)
{
this.tsmi[i][0] = new ToolStripMenuItem();
this.ms.Items.Add( this.tsmi[i][0]);
for( int j = 1; j < this.tsmi[i].Length; j++)
{
this.tsmi[i][j] = new ToolStripMenuItem();
this.tsmi[i][0].DropDownItems.Add( this.tsmi[i][j]);
this.tsmi[i][j].Click += new EventHandler( this.Menu_Click);
}
}
this.tsmi[0][0].Text = "File (&F)";
this.tsmi[0][1].Text = "Open (&O)";
this.tsmi[0][2].Text = "Exit (&X)";
this.Controls.Add( this.ms);
this.MainMenuStrip = this.ms;
/*--------------------------------------------------*/
this.dgv = new DataGridView();
//this.dgv.SetBounds( 0, 0, 360, 360);
//this.dgv.Anchor = ( AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom);
this.dgv.Dock = DockStyle.Fill;
this.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
this.dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
this.dgv.AllowDrop = true;
this.dgv.DragEnter += new DragEventHandler( this.dgv_DragEnter);
this.dgv.DragDrop += new DragEventHandler( this.dgv_DragDrop);
this.Controls.Add( this.dgv);
/*--------------------------------------------------*/
sample_csv( System.IO.Directory.GetCurrentDirectory() + @"\test.csv");
/*--------------------------------------------------*/
string[] args = Environment.GetCommandLineArgs();
if( 1 < args.Length)
{
open_csv( args[1]);
}
}
private void Menu_Click( object sender, EventArgs e)
{
if( sender.ToString() == this.tsmi[0][1].Text)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "csv file|*.csv";
if( ofd.ShowDialog() == DialogResult.OK)
{
open_csv( ofd.FileName);
}
}
else if( sender.ToString() == this.tsmi[0][2].Text)
{
this.Close();
}
}
private void dgv_DragEnter( object sender, DragEventArgs e)
{
if( e.Data.GetDataPresent( DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Move;
}
}
private void dgv_DragDrop( object sender, DragEventArgs e)
{
string[] filename = (string[]) e.Data.GetData( DataFormats.FileDrop, false);
open_csv( filename[0]);
}
/*--------------------------------------------------*/
private void open_csv( string filename)
{
this.Text = Path.GetFileName( filename);
this.dgv.Rows.Clear();
this.dgv.Columns.Clear();
try
{
StreamReader sr = new StreamReader( filename);
string txt = sr.ReadToEnd();
sr.Close();
txt.Replace( "\r\n", "\n");
string[] lines = txt.Split( '\n');
string[] str;
str = lines[0].Split( ',');
for( int i = 0; i < str.Length; i++)
{
this.dgv.Columns.Add( i.ToString(), i.ToString());
}
for( int i = 1; i < lines.Length; i++)
{
str = lines[i].Split( ',');
this.dgv.Rows.Add( str);
}
for( int i = 0; i < dgv.Rows.Count - 1; i++)
{
this.dgv.Rows[i].HeaderCell.Value = i.ToString();
}
}
catch( System.Exception err)
{
MessageBox.Show( err.Message);
}
}
/*--------------------------------------------------*/
private void sample_csv( string filename)
{
try
{
StreamWriter sw = new StreamWriter( filename, false);
for( int i = 0; i < 16; i++)
{
sw.WriteLine( i.ToString() + ", " + ( i * i).ToString());
}
sw.Close();
}
catch( Exception e)
{
MessageBox.Show( e.Message);
}
}
}
0 件のコメント:
コメントを投稿