五丰称屠宰重客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

221 lines
8.2 KiB

using System;
using System.Data;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using Forks.Utils.Data;
using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;
namespace B3ButcherWeightClient {
public partial class QueryForm : Form {
private int textBoxNum;
private DataSet mResultDataSet = new DataSet();
public QueryForm() {
InitializeComponent();
}
private AutoSizeFormClass asc = new AutoSizeFormClass();
private void QueryForm_Load(object sender, EventArgs e) {
monthCalendar.Visible = false;
monthCalendar.LostFocus += (s, e1) => { this.monthCalendar.Visible = false; };
textBoxBeginTime.Text = DateTime.Today.ToString("yyyy-MM-dd") ;
textBoxEndTime.Text = DateTime.Today.ToString("yyyy-MM-dd") ;
asc.ControllInitializeSize(this);
}
private void textBoxBeginTime_DoubleClick(object sender, EventArgs e) {
textBoxNum = 1;
monthCalendar.Visible = true;
monthCalendar.Focus();
}
private void textBoxEndTime_DoubleClick(object sender, EventArgs e) {
textBoxNum = 2;
monthCalendar.Visible = true;
monthCalendar.Focus();
}
private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e) {
if (textBoxNum == 1) {
textBoxBeginTime.Text = this.monthCalendar.SelectionStart.ToString("yyyy-MM-dd",
DateTimeFormatInfo.InvariantInfo);
}
if (textBoxNum == 2) {
string str = this.monthCalendar.SelectionStart.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
textBoxEndTime.Text = str;
}
monthCalendar.Visible = false;
}
private void QueryForm_SizeChanged(object sender, EventArgs e) {
asc.controlAutoSize(this);
}
private void button1_Click(object sender, EventArgs e)
{
NumberColumn.Visible = false;
IDColumn.Visible = true;
SeqColumn.Visible = true;
DateColumn.Visible = true;
var querySql =
"select [ID],[Sequence],[PhaseCode],[DateTime],[Livestock_Name],[Weight],[SubWeight],[Weight]+ISNULL([SubWeight],0) as [AllWeight] from [WeightTable] ";
DoSelect(querySql, "", " order by [DateTime]");
}
//汇总查询
private void button2_Click(object sender, EventArgs e) {
//阶段号、级别、头数、重量、扣重、净重
NumberColumn.Visible = true;
IDColumn.Visible = false;
SeqColumn.Visible = false;
DateColumn.Visible = false;
string querySql =
"select [PhaseCode],[Livestock_Name],SUM(1) AS [Number],SUM([Weight]) AS [Weight],SUM([SubWeight]) AS [SubWeight],SUM([Weight]+ISNULL([SubWeight],0)) as [AllWeight] from [WeightTable] ";
DoSelect(querySql, " group by [PhaseCode],[Livestock_Name]", "order by [PhaseCode]");
}
private void button3_Click(object sender, EventArgs e) {
//级别、头数、重量、扣重、净重
NumberColumn.Visible = true;
IDColumn.Visible = false;
SeqColumn.Visible = false;
DateColumn.Visible = false;
string querySql =
"select [Livestock_Name],SUM(1) AS [Number],SUM([Weight]) AS [Weight],SUM([SubWeight]) AS [SubWeight],SUM([Weight]+ISNULL([SubWeight],0)) as [AllWeight] from [WeightTable] ";
DoSelect(querySql, " group by [Livestock_Name]", "order by [Livestock_Name]");
}
private void DoSelect(string querySql, string groupBy,string orderBy) {
var sql = new StringBuilder();
sql.Append(querySql);
var isAddBegin = !string.IsNullOrEmpty(this.textBoxBeginTime.Text);
var isAddEnd = !string.IsNullOrEmpty(this.textBoxEndTime.Text);
var condition = new StringBuilder();
//if(isAddBegin||isAddEnd||isAddLevel)
sql.Append("where 1=1 ");
if (isAddBegin)
condition.Append(string.Format("and [DateTime] >= '{0}' ", this.textBoxBeginTime.Text));
else {
condition.Append(string.Format("and [DateTime] >= '{0}' ", DateTime.Today.AddDays(-1).ToShortDateString()));
}
if (!string.IsNullOrWhiteSpace(txtShunXuHao.Text))
{
condition.Append(string.Format("and [Sequence] = '{0}' ", txtShunXuHao.Text));
}
if (isAddEnd)
condition.Append(string.Format("and [DateTime] <= '{0} 23:59:59' ", this.textBoxEndTime.Text));
condition.Append(groupBy);
condition.Append(orderBy);
if (condition.Length > 0)
sql.Append(condition.ToString());
using (ISqlUtil sqlUtil = new SqlUtil(ConfigUtil.ConnectionStr)) {
try {
lock (EncodeString.RwLocker) {
this.mResultDataSet = sqlUtil.ExecuteSql(sql.ToString());
dataGridView1.DataSource = mResultDataSet.Tables[0];
}
sqlUtil.Close();
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
private void button4_Click(object sender, EventArgs e) {
if (OutToExcelFromDataGridView("称重数据", dataGridView1, true)) {
MessageBox.Show("导出数据成功!");
}
}
public bool OutToExcelFromDataGridView(string title, DataGridView dgv, bool isShowExcel) {
var titleColumnSpan = 0; //标题的跨列数
var fileName = ""; //保存的excel文件名
int columnIndex; //列索引
if (dgv.Rows.Count == 0)
return false;
/*保存对话框*/
var sfd = new SaveFileDialog();
sfd.Filter = "导出Excel(*.xlsx)|*.xlsx";
sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");
if (sfd.ShowDialog() == DialogResult.OK) {
label2.Text = "数据导出中.....";
fileName = sfd.FileName;
/*建立Excel对象*/
var excel = new Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcel;
/*分析标题的跨列数*/
foreach (DataGridViewColumn column in dgv.Columns) {
if (column.Visible == true)
titleColumnSpan++;
}
/*合并标题单元格*/
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.get_Range(worksheet.Cells[1, 1] as Range, worksheet.Cells[1, titleColumnSpan] as Range).Merge();
/*生成标题*/
excel.Cells[1, 1] = title;
SetAlign(excel.Cells[1, 1], XlHAlign.xlHAlignCenter);
//生成字段名称
columnIndex = 1;
for (int i = 0; i < dgv.ColumnCount; i++) {
if (dgv.Columns[i].Visible == true) {
excel.Cells[2, columnIndex] = dgv.Columns[i].HeaderText;
switch (dgv.Columns[i].HeaderText) {
case "时间":
((Range)excel.Cells[2, columnIndex]).ColumnWidth = 20;
break;
case "级别":
((Range)excel.Cells[2, columnIndex]).ColumnWidth = 15;
break;
}
SetAlign(excel.Cells[2, columnIndex], XlHAlign.xlHAlignCenter);
columnIndex++;
}
}
//填充数据
for (int i = 0; i < dgv.RowCount; i++) {
columnIndex = 1;
for (int j = 0; j < dgv.ColumnCount; j++) {
if (dgv.Columns[j].Visible == true) {
if (dgv[j, i].ValueType == typeof(string)) {
excel.Cells[i + 3, columnIndex] = "'" + dgv[j, i].Value;
} else {
excel.Cells[i + 3, columnIndex] = dgv[j, i].Value;
}
SetAlign(excel.Cells[i + 3, columnIndex], XlHAlign.xlHAlignLeft);
columnIndex++;
}
}
}
worksheet.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
excel.Quit();
GC.Collect();
return true;
}
return false;
}
private static void SetAlign(Range range, XlHAlign a) {
range.HorizontalAlignment = a;
}
private void textBoxBeginTime_TextChanged(object sender, EventArgs e) {
}
}
}