123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using Microsoft.Win32;
- using System.IO;
- using System.Reflection;
- using System.Data;
- using AGV_WPF.Models;
- using System.Text.RegularExpressions;
- namespace AGV_WPF
- {
- /// <summary>
- /// ExceptionManage.xaml 的交互逻辑
- /// </summary>
- public partial class ExceptionManage : Window
- {
- public int carid = -1;
- public string strdate;
- public ExceptionManage()
- {
- InitializeComponent();
- this.dataGrid1.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.DataGridSoftware_LoadingRow);
- BindCombox();
- this.DataContext = new ExceptionManageModel(this);
- }
- /// <summary>
- /// 表格增加行号
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DataGridSoftware_LoadingRow(object sender, DataGridRowEventArgs e)
- {
- e.Row.Header = e.Row.GetIndex() + 1;
- }
- /// <summary>
- /// 下拉框绑定
- /// </summary>
- private void BindCombox()
- {
- //绑定AGV小车编号
- DAL.ZSql sql1 = new DAL.ZSql();
- sql1.Open("select DISTINCT CarID from T_Ex order by CarID");
- /*cbCarid.ItemsSource = sql1.m_table.DefaultView;
- cbCarid.DisplayMemberPath = "CarID";
- cbCarid.SelectedValuePath = "CarID";*/
- for (int i = 0; i < sql1.m_table.Rows.Count;i++ )
- {
- cbCarid.Items.Add(sql1.m_table.Rows[i]["CarID"].ToString());
- }
- cbCarid.Items.Add("所有AGV");
- sql1.Close();
- //日期绑定到今天的
- cbYear.Text = DateTime.Now.Year.ToString(); //获取年份
- }
- /// <summary>
- /// 全部删除
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnDeleteAll_Click(object sender, RoutedEventArgs e)
- {
- if (MessageBox.Show("确认要删除当前记录?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
- {
- DAL.ZSql sql1 = new DAL.ZSql();
- if (carid == 0)
- {
- sql1.Open("delete from T_Ex where ExTimer like '" + strdate + "%'");
- }
- else
- sql1.Open("delete from T_Ex where CarID=" + carid.ToString() + " and ExTimer like '" + strdate + "%'");
- sql1.Close();
- LoadDataGrid(carid, strdate);
- }
- }
- /// <summary>
- /// AGV编号下拉框选择响应
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cbCarid_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (cbCarid.SelectedValue != null)
- {
- if (cbCarid.SelectedValue.ToString().Trim() == "所有AGV")
- {
- carid = 0;
- }
- else
- carid = Convert.ToUInt16(cbCarid.SelectedValue.ToString().Trim());
- LoadDataGrid(carid,strdate);
- }
- }
- /// <summary>
- /// 加载数据表格
- /// </summary>
- /// <param name="paracarid"></param>
- /// <param name="strdate"></param>
- private void LoadDataGrid(int paracarid,string strdate)
- {
- if (paracarid >= 0)
- {
- DAL.ZSql sql2 = new DAL.ZSql();
- if (paracarid==0)
- {
- sql2.Open("select * from T_Ex where ExTimer like '" + strdate + "%' order by ExTimer");
- }
- else
- sql2.Open("select * from T_Ex where CarID=" + paracarid.ToString() + " and ExTimer like '" + strdate + "%' order by ExTimer");
- dataGrid1.ItemsSource = sql2.m_table.DefaultView;
- sql2.Close();
- }
- }
- /// <summary>
- /// 日期下拉框选择响应
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cbDate_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (cbYear.SelectedValue != null && cbMonth.SelectedValue != null && cbDay.SelectedValue !=null)
- {
- ComboBoxItem cbi = (ComboBoxItem)cbYear.SelectedItem;
- string stryear =cbi.Content.ToString();
- cbi = (ComboBoxItem)cbMonth.SelectedItem;
- string strmonth = cbi.Content.ToString();
- cbi = (ComboBoxItem)cbDay.SelectedItem;
- string strday = cbi.Content.ToString();
- if (strmonth == "-选择月份-")
- {
- strmonth = "%";
- }
- if (strday == "-选择日期-")
- {
- strday = "";
- }
- strdate=stryear + "-" + strmonth + "-" + strday;
- LoadDataGrid(carid, strdate);
- }
- }
- /// <summary>
- /// 导出到excel
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnExcel_Click(object sender, RoutedEventArgs e)
- {
- MyExportDataGrid(dataGrid1);
- }
- #region DataGrid数据导出Excel
- private void MyExportDataGrid(DataGrid dg)
- {
- if (dg.HasItems)//判断datagrid中是否有数据
- {
- try
- {
- string strPath = Environment.CurrentDirectory;
- Microsoft.Win32.SaveFileDialog dialogOpenFile = new Microsoft.Win32.SaveFileDialog();
- dialogOpenFile.DefaultExt = "csv";//默认扩展名
- dialogOpenFile.AddExtension = true;//是否自动添加扩展名
- dialogOpenFile.Filter = "*.csv|.csv";
- dialogOpenFile.OverwritePrompt = true;//文件已存在是否提示覆盖
- dialogOpenFile.FileName = "文件名";//默认文件名
- dialogOpenFile.CheckPathExists = true;//提示输入的文件名无效
- dialogOpenFile.Title = "对话框标题";
- //显示对话框
- bool? b = dialogOpenFile.ShowDialog();
- if (b == true)//点击保存
- {
- using (Stream stream = dialogOpenFile.OpenFile())
- {
- string c = "";
- StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("gb2312"));
- //DataTable dt = (DataTable)dg.DataContext;
- DataTable dt = ((DataView)dg.ItemsSource).Table;
- for (int k = 0; k < dt.Columns.Count; k++)
- {
- string strcolumns;
- switch (dt.Columns[k].Caption)
- {
- case "CarID":
- strcolumns = "小车编号";
- break;
- case "ExTimer":
- strcolumns = "报警时间";
- break;
- case "ExType":
- strcolumns = "报警类别";
- break;
- case "ExWorkLine":
- strcolumns = "报警生产区";
- break;
- case "ExRouteNum":
- strcolumns = "报警路线";
- break;
- case "ExMarkNum":
- strcolumns = "报警地标";
- break;
- default:
- strcolumns = dt.Columns[k].Caption;
- break;
- }
- c += strcolumns + ",";
- }
- c = c.Substring(0, c.Length - 1) + "\r\n";
- writer.Write(c);
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- c = "";
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- c += dt.Rows[i][j].ToString() + ",";
- }
- c = c.Substring(0, c.Length - 1) + "\r\n";
- writer.Write(c);
- }
- /*Byte[] fileContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(c);
- stream.Write(fileContent, 0, fileContent.Leth);*/
- writer.Close();
- stream.Close();
- }
- }
- //恢复系统路径-涉及不到的可以去掉
- Environment.CurrentDirectory = strPath;
- }
- catch (Exception msg)
- {
- MessageBox.Show(msg.ToString());
- }
- finally
- { MessageBox.Show("导出成功!", "系统提示"); }
- }
- else
- {
- MessageBox.Show("没有可以输出的数据!", "系统提示");
- }
- }
- #endregion
- private void comBox_DropDownClosed(object sender, EventArgs e)
- {
- if (comboBoxRankType.Text == "站点")
- {
- comboBoxWorkLine.Visibility = Visibility.Visible;
- }
- else
- comboBoxWorkLine.Visibility = Visibility.Hidden;
- LoadRank();
- }
- void LoadRank()
- {
- string date = "";
- if (!Regex.IsMatch(cbMonth.Text, @"^[0-9][0-9]*$"))
- {
- date = string.Format("{0}-", cbYear.Text);
- }
- else if (!Regex.IsMatch(cbDay.Text, @"^[0-9][0-9]*$"))
- {
- date = string.Format("{0}-{1}-", cbYear.Text,cbMonth.Text);
- }
- else if (!Regex.IsMatch(cbDay.Text, @"^[0-9][0-9]*$") && !Regex.IsMatch(cbMonth.Text, @"^[0-9][0-9]*$"))
- {
- date = string.Format("{0}-", cbYear.Text);
- }
- else if (Regex.IsMatch(cbDay.Text, @"^[0-9][0-9]*$") && Regex.IsMatch(cbMonth.Text, @"^[0-9][0-9]*$"))
- {
- date = string.Format("{0}-{1}-{2}", cbYear.Text, cbMonth.Text,cbDay.Text);
- }
- if (this.DataContext == null)
- return;
- string carID = cbCarid.Text;
- if (checkBoxCurrentDate.IsChecked.Value)
- {
- ((ExceptionManageModel)this.DataContext).LoadRankData(date, comboBoxExType.Text, comboBoxRankType.Text, comboBoxWorkLine.Text, carID);
- }
- else
- {
- ((ExceptionManageModel)this.DataContext).LoadRankData("", comboBoxExType.Text, comboBoxRankType.Text, comboBoxWorkLine.Text, carID);
- }
- }
- }
- }
|