You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
7.4 KiB
C#

using HighWayIot.Repository.domain;
using HighWayIot.Repository.service.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
namespace RFIDSocket
{
public partial class RFIDLog : Form
{
List<RFIDContent> rFIDContents = new List<RFIDContent>();
BaseContentServiceImpl sql = new BaseContentServiceImpl();
public RFIDLog()
{
InitializeComponent();
StartTime.Value = DateTime.Now.AddDays(-3);
List<string> list = new List<string>
{
"",
"NB",
"GR",
"MR"
};
ReadKind.DataSource = list;
txtPath.Text = Environment.CurrentDirectory.ToString();
Init();
}
private void Init()
{
rFIDContents = sql.GetContentInfos();
}
private void TimeSelect_Click(object sender, EventArgs e)
{
if (LogContent != null)
{
LogContent.DataSource = null;
LogContent.DataSource = LogControl.LogTimeSelect(rFIDContents, StartTime.Value, EndTime.Value);
}
}
private void ReadKindSelect_Click(object sender, EventArgs e)
{
if (LogContent != null)
{
LogContent.DataSource = null;
LogContent.DataSource = LogControl.LogReadKindSelect(rFIDContents, ReadKind.Text);
}
}
private void DeviceNoSelect_Click(object sender, EventArgs e)
{
if(!int.TryParse(DeviceNo.Text, out int no))
{
MessageBox.Show("设备编号格式不正确");
return;
}
if (LogContent != null)
{
LogContent.DataSource = null;
LogContent.DataSource = LogControl.LogDeviceNoSelect(rFIDContents, no);
}
}
private void ContentSelect_Click(object sender, EventArgs e)
{
if (LogContent != null)
{
LogContent.DataSource = null;
LogContent.DataSource = LogControl.LogContentSelect(rFIDContents, Content.Text);
}
}
private void SelectAll_Click(object sender, EventArgs e)
{
if (!int.TryParse(DeviceNo.Text, out int no))
{
MessageBox.Show("设备编号格式不正确");
return;
}
if (LogContent != null)
{
LogContent.DataSource = null;
LogContent.DataSource = LogControl.LogTimeSelect(
LogControl.LogReadKindSelect(
LogControl.LogDeviceNoSelect(
LogControl.LogContentSelect(rFIDContents,
Content.Text),
no),
ReadKind.Text),
StartTime.Value, EndTime.Value);
}
}
/// <summary>
/// 查询内容导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ExcelOutPut_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
// 列强制转换
for (int count = 0; count < LogContent.Columns.Count; count++)
{
DataColumn dc = new DataColumn(LogContent.Columns[count].HeaderText.ToString());
dt.Columns.Add(dc);
}
// 循环行
for (int count = 0; count < LogContent.Rows.Count; count++)
{
DataRow dr = dt.NewRow();
for (int countsub = 0; countsub < LogContent.Columns.Count; countsub++)
{
dr[countsub] = $"{Convert.ToString(LogContent.Rows[count].Cells[countsub].Value)} ";
}
dt.Rows.Add(dr);
}
ExportExcel(dt);
}
/// <summary>
/// 全部内容导出Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
var data = sql.GetContentInfos();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("格口编号", typeof(string));
dt.Columns.Add("状态码", typeof(string));
dt.Columns.Add("读取内容", typeof(string));
dt.Columns.Add("读取时间", typeof(string));
foreach(var d in data)
{
dt.Rows.Add(d.ID.ToString(), d.DeviceNo.ToString(), d.ReadKind, $"{d.Content} ", d.LogTime.ToString());
}
ExportExcel(dt);
}
public void ExportExcel(DataTable dt)
{
//设置导出文件路径
string path = txtPath.Text;
//设置新建文件路径及名称
string savePath = $"{path}{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.xls";
//创建文件
FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);
//以指定的字符编码向指定的流写入字符
StreamWriter sw = new StreamWriter(file, Encoding.GetEncoding("GB2312"));
StringBuilder strbu = new StringBuilder();
try
{
//写入标题
for (int i = 0; i < dt.Columns.Count; i++)
{
strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
}
//加入换行字符串
strbu.Append(Environment.NewLine);
//写入内容
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
strbu.Append(dt.Rows[i][j].ToString() + "\t");
}
strbu.Append(Environment.NewLine);
}
sw.Write(strbu.ToString());
sw.Flush();
file.Flush();
sw.Close();
sw.Dispose();
file.Close();
file.Dispose();
}
catch(Exception ex)
{
MessageBox.Show($"导出文件发生错误{ex.Message}");
sw.Close();
sw.Dispose();
file.Close();
file.Dispose();
}
}
/// <summary>
/// 文件路径选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.Description = "请选择文件夹";
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog1.ShowNewFolderButton = true;
if (txtPath.Text.Length > 0) folderBrowserDialog1.SelectedPath = txtPath.Text;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtPath.Text = folderBrowserDialog1.SelectedPath;
}
}
}
}