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.
CaiQie/RfidWeb/Frm/FrmBarCodeGridView.cs

145 lines
4.9 KiB
C#

using ClosedXML.Excel;
using DB.Service;
6 days ago
using HZH_Controls.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
6 days ago
using System.IO;
6 days ago
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
6 days ago
using Tool;
6 days ago
namespace RfidWeb.Frm
{
public partial class FrmBarCodeGridView : UserControl
{
private ReadBufLogService readBufLogService = new ReadBufLogService();
6 days ago
public FrmBarCodeGridView()
{
InitializeComponent();
this.dataGridView1.Columns["CreateTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
var keyValue = new List<KeyValuePair<string, string>>();
6 days ago
keyValue.Add(new KeyValuePair<string, string>("A", "收料1"));
keyValue.Add(new KeyValuePair<string, string>("B", "收料2"));
keyValue.Add(new KeyValuePair<string, string>("C", "废料"));
ucHorizontalList1.DataSource = keyValue;
var dt = DateTime.Now;
6 days ago
var a = Convert.ToDateTime(dt.ToString("yyyy-MM-dd 00:00:00"));
var b = Convert.ToDateTime(dt.ToString("yyyy-MM-dd 23:59:00"));
this.ucDatePickerExt21.CurrentTime = a;
this.ucDatePickerExt22.CurrentTime = b;
6 days ago
this.dataGridView1.AutoGenerateColumns = false;
ucPagerControl21.PageModel = PageModel.PageCount;
ucPagerControl21.PageIndex = 1;
ucPagerControl21.PageSize = 20;
6 days ago
this.ucPagerControl21.ShowSourceChanged +=
new HZH_Controls.Controls.PageControlEventHandler(this.ucPagerControl21_ShowSourceChanged);
this.ucHorizontalList1.SelectedItemEvent +=
new System.EventHandler(this.ucHorizontalList1_SelectedItemEvent);
6 days ago
ucPagerControl21_ShowSourceChanged(new object());
6 days ago
}
private void Replace()
{
ucPagerControl21.PageIndex = 1;
ucPagerControl21_ShowSourceChanged(new object());
}
private void ucPagerControl21_ShowSourceChanged(object currentSource)
{
this.dataGridView1.AutoGenerateColumns = false;
var selectedItemDataSource = ucHorizontalList1.SelectedItem.DataSource;
string key = selectedItemDataSource.Key;
6 days ago
var beginTime = ucDatePickerExt21.CurrentTime;
var endTime = ucDatePickerExt22.CurrentTime;
6 days ago
int index = ucPagerControl21.PageIndex;
var page = readBufLogService.GetPagedList(ucPagerControl21.PageIndex,
ucPagerControl21.PageSize, key, beginTime, endTime);
6 days ago
ucPagerControl21.PageCount = page.TotalPages;
this.dataGridView1.DataSource = page.Items;
}
private void ucBtnSelect_BtnClick(object sender, EventArgs e)
{
Replace();
}
private void ucHorizontalList1_SelectedItemEvent(object sender, EventArgs e)
{
Replace();
}
6 days ago
6 days ago
private void ucBtnExt1_BtnClick(object sender, EventArgs e)
{
var selectedItemDataSource = ucHorizontalList1.SelectedItem.DataSource;
string key = selectedItemDataSource.Key;
string val = selectedItemDataSource.Value;
var beginTime = ucDatePickerExt21.CurrentTime;
var endTime = ucDatePickerExt22.CurrentTime;
string directoryPath = "D:\\excel";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
Console.WriteLine("目录已创建。");
}
string suiji = Guid.NewGuid().ToString().Substring(0, 5);
string file = directoryPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + suiji + "_" + val + ".xlsx";
6 days ago
var dt = readBufLogService.GetDtaTable(key, beginTime, endTime);
ExportDataTableToExcel(dt, file);
6 days ago
MessageBox.Show("OK");
}
void ExportDataTableToExcel(DataTable table, string filePath)
{
using (var workbook = new XLWorkbook())
{
table.TableName = "Shell1";
var worksheet = workbook.Worksheets.Add(table.TableName);
// 写入列头
for (int col = 0; col < table.Columns.Count; col++)
{
worksheet.Cell(1, col + 1).Value = table.Columns[col].ColumnName;
}
// 写入行数据
for (int row = 0; row < table.Rows.Count; row++)
{
for (int col = 0; col < table.Columns.Count; col++)
{
worksheet.Cell(row + 2, col + 1).Value = table.Rows[row][col].ToString();
}
}
// 保存为文件
workbook.SaveAs(filePath);
}
}
6 days ago
}
}