using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace SCM
{
public partial class Gy_BarCodeDetailList : Form
{
//[h_c_barcodedetail]
//h_c_barcodemain
public Gy_BarCodeDetailList()
{
InitializeComponent();
dateTimePicker1.Text = DateTime.Now.AddDays(-7).ToString();
dateTimePicker2.Text = DateTime.Now.ToString();
comboBox1.SelectedItem = "全部";
comboBox2.SelectedItem = "默认排序";
oWeb.Url = SCM.ClsPub1.WEBSURL;
}
public frmBillQueryCondition_Base frmCondition;
public string ViewName = "h_c_barcodedetail";
public string ModCaption = "成品分切检验明细表";
public const string ModName = "1250";
public string sDlgWhere = ""; //外窗体递入
public int selectRow = 0;
SCM.WMSWeb.WebService1 oWeb = new SCM.WMSWeb.WebService1();
///
/// 查询
///
///
///
private void cx_Click(object sender, EventArgs e)
{
Display();
}
public void SetCondition(frmBillQueryCondition_Base frmCondition, string ViewName, string Name)
{
frmCondition.Tag = "";
frmCondition.ViewName = ViewName;
frmCondition.ModName = Name;
}
private void Display()
{
StringBuilder whereBuilder = new StringBuilder();
if (string.IsNullOrEmpty(dateTimePicker1.Text) || string.IsNullOrEmpty(dateTimePicker2.Text))
{
MessageBox.Show("输入时间不能为空");
return;
}
if(DateTime.Parse(dateTimePicker1.Text)> DateTime.Parse(dateTimePicker2.Text))
{
MessageBox.Show("开始时间不能大于截至时间");
return;
}
whereBuilder.Append(" and c.HMakeDate>''"+dateTimePicker1.Text+"'' and c.HMakeDate<''"+dateTimePicker2.Text+" 23:59:59''");
if (!string.IsNullOrEmpty(txtHBillNo.Text))
whereBuilder.Append(" and c.HMaterName like ''%"+txtHBillNo.Text+"%''");
if(!string.IsNullOrEmpty(textBox1.Text))
whereBuilder.Append(" and d.FNUMBER like ''%"+textBox1.Text+"%''");
if (!string.IsNullOrEmpty(textBox2.Text))
whereBuilder.Append(" and b.FLOT_TEXT like ''%"+textBox2.Text+"%''");
if(comboBox1.Text!="全部")
whereBuilder.Append(" and c.HProdLev=''" + comboBox1.Text+"''");
if (!string.IsNullOrEmpty(textBox4.Text))
whereBuilder.Append(" and g.HName like ''%"+textBox4.Text+"%''");
if (!string.IsNullOrEmpty(textBox5.Text))
whereBuilder.Append(" and e.HName like ''%"+textBox5.Text+"%''");
if (!string.IsNullOrEmpty(textBox6.Text))
whereBuilder.Append(" and c.HWorkLineName like ''%"+textBox6.Text+"%''");
if (!string.IsNullOrEmpty(textBox3.Text))
whereBuilder.Append(" and h.FDATAVALUE like ''%" + textBox3.Text + "%''");
if (!string.IsNullOrEmpty(textBox7.Text))
whereBuilder.Append(" and i.FDATAVALUE like ''%" + textBox7.Text + "%''");
if (!string.IsNullOrEmpty(textBox8.Text))
whereBuilder.Append(" and a.FBILLNO like ''%" + textBox8.Text + "%''");
string orderBy = " order by 汇报单号 desc,产品编码 desc,分卷批次 desc ";
if(comboBox2.Text=="产品名称")
{
orderBy = " order by c.HMaterName desc ";
}
if(comboBox2.Text=="批次排序")
{
orderBy = " order by c.HBatchNo desc";
}
string execSql = "exec "+ViewName+" '"+whereBuilder+"','"+orderBy+"'";
DataSet DSet = oWeb.getDataSetBySQL(execSql, ViewName, ref DBUtility.ClsPub.sExeReturnInfo);
//生成首行标题
if (DSet == null)
{
MessageBox.Show("没有返回任何结果,请在过滤框中点击【恢复】按钮,尝试再次查询!" + DBUtility.ClsPub.sExeReturnInfo);
return;
}
var dt = AddDatable(DSet.Tables[0]);
//
grdMain.DataSource = dt.DefaultView;
//冻结
//int FrCol = DBUtility.ClsPub.isInt(frmCondition.txtFrozenCol.Text);
//string s = frmCondition.cmbHComplete.Text;
//ClsPub1.DisplayGrid(grdMain, this.Name, s, FrCol);
}
public DataTable AddDatable(DataTable dt)
{
decimal tal = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
tal += Convert.ToDecimal(dt.Rows[i][7]);
}
DataRow dr = dt.NewRow();
dr[0] = "合计";
dr[7] = tal;
dt.Rows.Add(dr);
return dt;
}
///
/// 退出
///
///
///
private void tc_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 网格 导出EXCEL 网格,文件名,标题名
///
public static void DataGridViewToExcel2(DataGridView grdMain, string sText, string sBTText)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl文件(*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "数据视图导出EXCEL文件";
saveFileDialog.FileName = sText;
//saveFileDialog.ShowDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
DateTime start = DateTime.Now;
try
{
if (sBTText != "")
{
sw.WriteLine(sBTText);
}
//写标题
bool sFirstCol = true;
for (int i = 0; i < grdMain.ColumnCount; i++)
{
if (i > 0 && grdMain.Columns[i].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[i].HeaderText.Substring(0, 1) != "H" && sFirstCol != true)
{
str += "\t";
}
if (grdMain.Columns[i].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[i].HeaderText.Substring(0, 1) != "H")
{
sFirstCol = false;
str += grdMain.Columns[i].HeaderText;
}
}
str = str.Replace("\n", "");
sw.WriteLine(str);
//写内容
for (int j = 0; j < grdMain.Rows.Count; j++)
{
string tempStr = "";
sFirstCol = true;
for (int k = 0; k < grdMain.Columns.Count; k++)
{
if (k > 0 && grdMain.Columns[k].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[k].HeaderText.Substring(0, 1) != "H" && sFirstCol != true)
{
tempStr += "\t";
}
if (grdMain.Columns[k].HeaderText.Substring(0, 1) != "h" && grdMain.Columns[k].HeaderText.Substring(0, 1) != "H")
{
sFirstCol = false;
if (grdMain.Rows[j].Cells[k].Value == null)
{
tempStr += string.Empty;
}
else
{
tempStr += grdMain.Rows[j].Cells[k].Value.ToString();
}
}
}
tempStr = tempStr.Replace("\n", "");
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Close();
myStream.Close();
}
MessageBox.Show("将此工作表导出为excel共耗时:" + DateTime.Now.Subtract(start).TotalMilliseconds.ToString() + "毫秒");
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void yl_Click(object sender, EventArgs e)
{
DataGridViewToExcel2(grdMain, this.Text, this.Text);
}
private void grdMain_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();//添加行号
}
private void grdMain_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
}
}
}