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.
209 lines
7.3 KiB
C#
209 lines
7.3 KiB
C#
using DevExpress.ClipboardSource.SpreadsheetML;
|
|
using Mesnac.Action.ChemicalWeighing.MainDetailControl.Entity;
|
|
using Microsoft.Office.Interop.Excel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using DataTable = System.Data.DataTable;
|
|
using Workbook = Microsoft.Office.Interop.Excel.Workbook;
|
|
using Worksheet = Microsoft.Office.Interop.Excel.Worksheet;
|
|
|
|
namespace Mesnac.Action.ChemicalWeighing.LjReport.WetWaitTime
|
|
{
|
|
public class Export
|
|
{
|
|
string FileName = @"D:\WeightExportFile.xlsx";
|
|
List<Lj_RGVTimeRecord> list;
|
|
List<Lj_RGVTimeRecord> listMore;
|
|
DataTable dt;
|
|
DataTable MoreDataTable;
|
|
public Export(List<Lj_RGVTimeRecord> list, string address)
|
|
{
|
|
this.list = list;
|
|
FileName = address;
|
|
this.ExportTo();
|
|
|
|
}
|
|
|
|
public Export(List<Lj_RGVTimeRecord> list)
|
|
{
|
|
this.list = list;
|
|
this.ExportTo();
|
|
}
|
|
|
|
public void ExportTo()
|
|
{
|
|
dt = new DataTable();
|
|
dt.Columns.Add("机台", typeof(string));
|
|
dt.Columns.Add("湿混等待时间", typeof(string));
|
|
dt.Columns.Add("RGV等待时间", typeof(string));
|
|
dt.Columns.Add("总等待时间", typeof(string));
|
|
|
|
MoreDataTable = new DataTable();
|
|
MoreDataTable.Columns.Add("湿混等待时间", typeof(string));
|
|
MoreDataTable.Columns.Add("RGV等待时间", typeof(string));
|
|
MoreDataTable.Columns.Add("总等待时间", typeof(string));
|
|
MoreDataTable.Columns.Add("起始时间", typeof(string));
|
|
MoreDataTable.Columns.Add("结束时间", typeof(string));
|
|
|
|
// 使用 Microsoft Office Interop Excel 组件导出 Excel 文件
|
|
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
|
|
Workbook workbook = excelApp.Workbooks.Add();
|
|
|
|
AllExport(workbook);
|
|
MoreDataExport(workbook);
|
|
|
|
// 将 Excel 文件保存到磁盘
|
|
try
|
|
{
|
|
workbook.SaveAs(this.FileName);
|
|
MessageBox.Show("导出成功!");
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show("请重新选择导出路径!");
|
|
}
|
|
|
|
// 关闭 Excel 应用程序和工作簿对象,并释放资源
|
|
workbook.Close();
|
|
excelApp.Quit();
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据和导出
|
|
/// </summary>
|
|
/// <param name="workbook"></param>
|
|
private void AllExport(Workbook workbook)
|
|
{
|
|
DataTableSum();
|
|
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
|
|
worksheet.Name = "总量统计";
|
|
worksheet.Cells[1, 1] = "机台";
|
|
worksheet.Cells[1, 2] = "湿混等待时间";
|
|
worksheet.Cells[1, 3] = "RGV等待时间";
|
|
worksheet.Cells[1, 4] = "总等待时间";
|
|
int row = 2;
|
|
//读取数据
|
|
foreach (DataRow item in dt.Rows)
|
|
{
|
|
worksheet.Cells[row, 1] = item[0];
|
|
worksheet.Cells[row, 2] = item[1];
|
|
worksheet.Cells[row, 3] = item[2];
|
|
worksheet.Cells[row, 4] = item[3];
|
|
row++;
|
|
}
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
|
|
}
|
|
|
|
//详细数据导出
|
|
private void MoreDataExport(Workbook workbook)
|
|
{
|
|
for(int i = 1; i <= 8; i++)
|
|
{
|
|
MoreData(i);
|
|
Worksheet worksheet = (Worksheet)workbook.Worksheets.Add();
|
|
worksheet.Name = "机台"+ i + "详细数据";
|
|
worksheet.Cells[1, 1] = "湿混等待时间";
|
|
worksheet.Cells[1, 2] = "RGV等待时间";
|
|
worksheet.Cells[1, 3] = "总等待时间";
|
|
worksheet.Cells[1, 4] = "发送接料信号时间";
|
|
worksheet.Cells[1, 5] = "RGV离开时间";
|
|
int row = 2;
|
|
foreach (DataRow item in MoreDataTable.Rows)
|
|
{
|
|
worksheet.Cells[row, 1] = item[0];
|
|
worksheet.Cells[row, 2] = item[1];
|
|
worksheet.Cells[row, 3] = item[2];
|
|
worksheet.Cells[row, 4] = item[3];
|
|
worksheet.Cells[row, 5] = item[4];
|
|
row++;
|
|
}
|
|
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据和计算
|
|
/// </summary>
|
|
private void DataTableSum()
|
|
{
|
|
dt.Clear();
|
|
|
|
for (int i = 1; i <= 8; i++)
|
|
{
|
|
var dr = dt.NewRow();
|
|
dr[0] = "湿混机" + i;
|
|
var MixWaitTime = list.Where(x => x.DeviceNo == i).Sum(x => x.MixWaitTime);
|
|
dr[1] = TimeConvert(MixWaitTime);
|
|
var RGVWaitTime = list.Where(x => x.DeviceNo == i).Sum(x => x.RgvWaitTime);
|
|
dr[2] = TimeConvert(RGVWaitTime);
|
|
var totalTime = MixWaitTime + RGVWaitTime;
|
|
dr[3] = TimeConvert(totalTime);
|
|
|
|
dt.Rows.Add(dr);
|
|
}
|
|
var edr = dt.NewRow();
|
|
edr[0] = "湿混等待时间总和";
|
|
edr[1] = TimeConvert(list.Sum(x => x.MixWaitTime));
|
|
dt.Rows.Add(edr);
|
|
var edr1 = dt.NewRow();
|
|
edr1[0] = "RGV等待时间总和";
|
|
edr1[1] = TimeConvert(list.Sum(x => x.RgvWaitTime));
|
|
dt.Rows.Add(edr1);
|
|
var edr2 = dt.NewRow();
|
|
edr2[0] = "总等待时间总和";
|
|
edr2[1] = TimeConvert(list.Sum(x => x.MixWaitTime + x.RgvWaitTime));
|
|
dt.Rows.Add(edr2);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 详细数据计算
|
|
/// </summary>
|
|
/// <param name="num"></param>
|
|
private void MoreData(int num)
|
|
{
|
|
MoreDataTable.Clear();
|
|
listMore = list.Where(x => x.DeviceNo == num).ToList();
|
|
for (int i = 0; i < listMore.Count; i++)
|
|
{
|
|
var dr = MoreDataTable.NewRow();
|
|
dr[0] = TimeConvert(listMore[i].MixWaitTime);
|
|
dr[1] = TimeConvert(listMore[i].RgvWaitTime);
|
|
dr[2] = TimeConvert(listMore[i].MixWaitTime + listMore[i].RgvWaitTime);
|
|
dr[3] = listMore[i].MixReadyStartTime;
|
|
dr[4] = listMore[i].RGVLeaveTime;
|
|
MoreDataTable.Rows.Add(dr);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间转换
|
|
/// </summary>
|
|
/// <param name="t"></param>
|
|
/// <returns></returns>
|
|
private string TimeConvert(int t)
|
|
{
|
|
int h = t / 3600;
|
|
int m = (t / 60) % 60;
|
|
int s = t % 60;
|
|
if (h != 0)
|
|
{
|
|
return h + "时" + m + "分" + s + "秒";
|
|
}
|
|
if (m != 0)
|
|
{
|
|
return m + "分" + s + "秒";
|
|
}
|
|
return s + "秒";
|
|
}
|
|
}
|
|
}
|