官方简介:C#是微软公司发布的一种面向对象的、运行于.NET Framework之上的高级程序设计语言。并定于在微软职业开发者论坛(PDC)上登台亮相。C#是微软公司研究员Anders Hejlsberg的最新成果。C#看起来与Java有着惊人的相似;它包括了诸如单一继承、接口、与Java几乎同样的语法和编译成中间代码再运行的过程。
本文是小编首次接触C#,由一位大神成功代入C#世界,甚是欣喜!
项目需求:读取xml文件,并形成表格显示!
DataGridUtils类
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace Attendance
{
/// <summary>
/// Description of DataGridUtils.
/// </summary>
public sealed class DataGridUtils
{
public static string FZ_XML_PATH = null;
public static string KC_XML_PATH = null;
public static string MC_XML_PATH = null;
public static Dictionary<int,string> XML_FILE_MAP = new Dictionary<int,string>();
private DataGridUtils()
{
}
public static void InitXmlFileMap(){
if(FZ_XML_PATH==null){
XML_FILE_MAP.Remove(0);
}else{
XML_FILE_MAP[0] = FZ_XML_PATH;
}
if(KC_XML_PATH==null){
XML_FILE_MAP.Remove(1);
}else{
XML_FILE_MAP[1] = KC_XML_PATH;
}
if(MC_XML_PATH==null){
XML_FILE_MAP.Remove(2);
}else{
XML_FILE_MAP[2] = MC_XML_PATH;
}
}
}
}FileUtis类
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace Attendance
{
/// <summary>
/// Description of FileUtis.
/// </summary>
public sealed class FileUtis
{
private FileUtis()
{
}
public static string ParseCsDataTime(string filePath){
string csDateTime = "";
if (System.IO.File.Exists(filePath)) {
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
xmlDoc.Load(filePath);//加载XML文件
XmlNode xm = xmlDoc.SelectSingleNode("/root/head");
XmlElement ele = (XmlElement)xm;
csDateTime = ele.GetAttribute("cs_data_time");
}
return csDateTime;
}
public static List<List<string>> ParseData(string filePath){
List<List<string>> list = new List<List<string>>();
if (System.IO.File.Exists(filePath)) {
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
xmlDoc.Load(filePath);//加载XML文件
XmlNodeList nodeList = xmlDoc.SelectNodes("//data");
if(nodeList!=null && nodeList.Count>0){
for(int i=0;i<nodeList.Count;i++)
{
XmlElement ele = (XmlElement)(nodeList.Item(i));
XmlAttributeCollection attrCols = ele.Attributes;
List<string> rowData = new List<string>();
foreach(XmlAttribute attr in attrCols){
rowData.Add(ele.GetAttribute(attr.Name));
}
list.Add(rowData);
}
}
}
return list;
}
/// <summary>
/// </summary>
/// <param name="fileAllPath">文件全路径名</param>
/// <param name="data">要写入的字符</param>
/// <param name="encoding">编码</param>
/// <param name="fileMode">模式</param>
public static void WriteFile(string fileAllPath, string data, Encoding encoding, FileMode fileMode)
{
FileStream fs = null;
try {
if (encoding == null) {
encoding = Encoding.Default;
}
//这里的FileMode.create是创建这个文件,如果文件名存在则覆盖重新创建
fs = new FileStream(fileAllPath, fileMode);
//存储时时二进制,所以这里需要把我们的字符串转成二进制
byte[] bytes = encoding.GetBytes(data);
fs.Write(bytes, 0, bytes.Length);
} catch (Exception e) {
//Debug.LogError(ee.Message);
} finally {
if (fs != null) {
fs.Close();
}
}
}
}
}MainForm类
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Oracle.ManagedDataAccess.Client;
namespace Attendance
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
string strConnection = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.100.181)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));Persist Security Info=True;User ID=mine;Password=mine;";
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void MainFormLoad(object sender, EventArgs e)
{
InitUI();
InitXmlFilePath();
FillFzDataGrid(0);
}
private void InitUI()
{
this.cs_mine_code.Text = ConfUtil.GetProfileString("CS_MINE_CODE");
this.filePath.Text = ConfUtil.GetProfileString("FILENAME");
this.url.Text = ConfUtil.GetProfileString("URL");
this.port.Text = ConfUtil.GetProfileString("PORT");
this.cs_mine_code.Focus();
}
/// <summary>
/// 初始化xml文件路径
/// </summary>
private void InitXmlFilePath()
{
DataGridUtils.FZ_XML_PATH = null;
DataGridUtils.KC_XML_PATH = null;
DataGridUtils.MC_XML_PATH = null;
string filePath = this.filePath.Text;
DirectoryInfo dir = new DirectoryInfo(filePath);
if (!dir.Exists) {
MessageBox.Show("文件目录[" + this.filePath.Text + "]不存在");
} else {
var fileInfos = dir.GetFiles();
foreach (var fileInfo in fileInfos) {
string fileName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName);
if (fileName.StartsWith(this.cs_mine_code.Text.Trim() + "AQFZ")) {
DataGridUtils.FZ_XML_PATH = fileInfo.FullName;
}
if (fileName.StartsWith(this.cs_mine_code.Text.Trim() + "AQKC")) {
DataGridUtils.KC_XML_PATH = fileInfo.FullName;
}
if (fileName.StartsWith(this.cs_mine_code.Text.Trim() + "AQMC")) {
DataGridUtils.MC_XML_PATH = fileInfo.FullName;
}
}
DataGridUtils.InitXmlFileMap();
}
}
/// <summary>
/// 初始化实时数据文件combox
/// </summary>
private void InitSSXmlFileCombox()
{
string filePath = this.filePath.Text;
DirectoryInfo dir = new DirectoryInfo(filePath);
if (!dir.Exists) {
MessageBox.Show("文件目录[" + this.filePath.Text + "]不存在");
} else {
var fileInfos = dir.GetFiles();
this.cbox_ss.Items.Clear();
foreach (var fileInfo in fileInfos) {
string fileName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName);
if (fileName.StartsWith(this.cs_mine_code.Text.Trim() + "AQSS")) {
this.cbox_ss.Items.Add(fileName + ".xml");
}
}
if (this.cbox_ss.Items.Count> 0) {
this.cbox_ss.SelectedIndex = 0;
}else{
this.cbox_ss.SelectedIndex = -1;
this.dg_3.Rows.Clear();
}
}
}
//grid填充数据
private void FillFzDataGrid(int nTabIndex)
{
if (nTabIndex != 3) {
((DataGridView)((this.tabCtrl_fz.TabPages[nTabIndex].Controls.Find("dg_" + nTabIndex, false)[0]))).Rows.Clear();
string fileAllPath = null;
if(DataGridUtils.XML_FILE_MAP.ContainsKey(nTabIndex)){
fileAllPath = DataGridUtils.XML_FILE_MAP[nTabIndex];
}
if (fileAllPath == null) {
return;
}
string csDataTime = FileUtis.ParseCsDataTime(fileAllPath);
this.tabCtrl_fz.TabPages[nTabIndex].Controls.Find("lab_cstime_" + nTabIndex, false)[0].Text = csDataTime;
List<List<string>> datas = FileUtis.ParseData(fileAllPath);
foreach (List<string> rowData in datas) {
DataGridViewRow row = new DataGridViewRow();
foreach (string col in rowData) {
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = col;
row.Cells.Add(textboxcell);
}
((DataGridView)((this.tabCtrl_fz.TabPages[nTabIndex].Controls.Find("dg_" + nTabIndex, false)[0]))).Rows.Add(row);
}
} else if (nTabIndex == 3) {
InitSSXmlFileCombox();
}
}
void Btn_saveClick(object sender, EventArgs e)
{
if (this.cs_mine_code.Text.Trim().Equals("")) {
MessageBox.Show("煤矿代码不能为空");
this.cs_mine_code.Focus();
return;
}
if (this.filePath.Text.Trim().Equals("")) {
MessageBox.Show("文件目录不能为空");
this.filePath.Focus();
return;
}
if (this.url.Text.Trim().Equals("")) {
MessageBox.Show("ip地址不能为空");
this.url.Focus();
return;
}
if (this.port.Text.Trim().Equals("")) {
MessageBox.Show("端口号不能为空");
this.port.Focus();
return;
}
this.btn_save.Enabled =false;
ConfUtil.WritePrivateProfileString("CS_MINE_CODE", this.cs_mine_code.Text);
ConfUtil.WritePrivateProfileString("FILENAME", this.filePath.Text);
ConfUtil.WritePrivateProfileString("URL", this.url.Text);
ConfUtil.WritePrivateProfileString("PORT", this.port.Text);
this.btn_save.Enabled =true;
MessageBox.Show("保存成功");
InitXmlFilePath();
FillFzDataGrid(this.tabCtrl_fz.SelectedIndex);
}
//tab change事件
void TabCtrl_fzSelectedIndexChanged(object sender, EventArgs e)
{
FillFzDataGrid(this.tabCtrl_fz.SelectedIndex);
}
//grid添加行号事件
void Dg_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridView dg = (DataGridView)sender;
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dg.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dg.RowHeadersDefaultCellStyle.Font,
rectangle,
dg.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
//ss combox change事件
void Cbox_ssSelectedIndexChanged(object sender, EventArgs e)
{
this.dg_3.Rows.Clear();
string fileAllPath = this.cbox_ss.SelectedItem.ToString();
fileAllPath = this.filePath.Text + "\\" + fileAllPath;
if (fileAllPath == null) {
return;
}
string csDataTime = FileUtis.ParseCsDataTime(fileAllPath);
this.lab_cstime_3.Text = csDataTime;
List<List<string>> datas = FileUtis.ParseData(fileAllPath);
foreach (List<string> rowData in datas) {
DataGridViewRow row = new DataGridViewRow();
foreach (string col in rowData) {
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = col;
row.Cells.Add(textboxcell);
}
this.dg_3.Rows.Add(row);
}
}
void Btn_pathClick(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
this.filePath.Text = path.SelectedPath;
}
void Dg_1CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
void Dg_2CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}效果展示
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
