`
feipigwang
  • 浏览: 743825 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

U6数据导出工具项目总结一 综合操作

 
阅读更多

C# WINDOWSForm应用程序


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;

namespace U6数据导出工具

{
public partial class Form2 : Form
{
Form1 form1;
String iautoid = "";
String mujianid = "";
String mujianname = "";
String mujianxinghao = "";
String jiecuncheck = "";
public Form2(Form1 fm1)
{
InitializeComponent();
form1 = fm1;
}

private void Form2_Load(object sender, EventArgs e)
{
String QueryString = "select iAutoId as '系统编码',CPSPCode as '母项产品编码',Inventory.cInvName as '母项产品名称' ,Inventory.cInvStd as '规格型号',ProductStructure.cDepCode as '部门编码',Department.cDepName as '部门名称 ' ,bExpand as '是否展开',iFinishExch as '成品率 ',cCheck as '审核人',(ProductStructure.cMemo) as '备注' from ProductStructure left join Inventory on ProductStructure.CPSPCode=Inventory.cInvCode left join department on ProductStructure.cDepCode=Department.cDepCode ";

using (SqlDataAdapter reader = new SqlDataAdapter(QueryString,GetSqlCon.connection))
{

DataSet ds = new DataSet();
reader.Fill(ds);

dataGridView1.DataSource = ds.Tables[0].DefaultView;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Silver;

}


}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
buttontable.Enabled = false;
buttontree.Enabled = false;
iautoid = this.dataGridView1.Rows[e.RowIndex].Cells["系统编码"].Value.ToString();
mujianid = this.dataGridView1.Rows[e.RowIndex].Cells["母项产品编码"].Value.ToString();
mujianname = this.dataGridView1.Rows[e.RowIndex].Cells["母项产品名称"].Value.ToString();
mujianxinghao = this.dataGridView1.Rows[e.RowIndex].Cells["规格型号"].Value.ToString();
buttontable.Enabled = true;
buttontree.Enabled = true;
}

private void buttonselect_Click(object sender, EventArgs e)
{
String QueryStringTemp = "";


if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "")
{
QueryStringTemp = "";
}
else if (textBox1.Text != "" && textBox2.Text == "" && textBox3.Text == "")
{
QueryStringTemp = " where cpspcode like '%" + textBox1.Text.ToString().Trim() + "%'";
}
else if (textBox1.Text == "" && textBox2.Text != "" && textBox3.Text == "")
{

QueryStringTemp = " where cinvname like '%" + textBox2.Text.ToString().Trim() + "%'";
}
else if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text != "")
{
QueryStringTemp = " where cInvStd like '%" + textBox3.Text.ToString().Trim() + "%'";
}
else if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text == "")
{
QueryStringTemp = " where cpspcode like '%" + textBox1.Text.ToString().Trim() + "%' AND cinvname like '%" + textBox2.Text.ToString().Trim() + "%'";
}
else if (textBox1.Text != "" && textBox2.Text == "" && textBox3.Text != "")
{
QueryStringTemp = " where cpspcode like '%" + textBox1.Text.ToString().Trim() + "%' ADN cInvStd like '%" + textBox3.Text.ToString().Trim() + "%'";
}
else if (textBox1.Text == "" && textBox2.Text != "" && textBox3.Text != "")
{
QueryStringTemp = " where cinvname like '%" + textBox2.Text.ToString().Trim() + "%' AND cInvStd like '%" + textBox3.Text.ToString().Trim() + "%'";
}
else if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
{
QueryStringTemp = " where cpspcode like '%" + textBox1.Text.ToString().Trim() + "%' AND cinvname like '%" + textBox2.Text.ToString().Trim() + "%' AND cInvStd like '%" + textBox3.Text.ToString().Trim() + "%'";
}



String QueryStringSelect = "select iAutoId as '系统编码',CPSPCode as '母项产品编码',Inventory.cInvName as '母项产品名称' ,Inventory.cInvStd as '规格型号',ProductStructure.cDepCode as '部门编码',Department.cDepName as '部门名称 ' ,bExpand as '是否展开',iFinishExch as '成品率 ',cCheck,(ProductStructure.cMemo) as '备注' from ProductStructure left join Inventory on ProductStructure.CPSPCode=Inventory.cInvCode left join department on ProductStructure.cDepCode=Department.cDepCode " + QueryStringTemp;
using(SqlDataAdapter reader = new SqlDataAdapter(QueryStringSelect,GetSqlCon.connection))
{
DataSet ds = new DataSet();
reader.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Silver;

}
}

private void buttontable_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();

dt.Columns.Add("母键系统编码");
dt.Columns.Add("子键系统编码");
dt.Columns.Add("标识符");
dt.Columns.Add("版本号");
dt.Columns.Add("编码");
dt.Columns.Add("名称");
dt.Columns.Add("规格型号");
dt.Columns.Add("主计量单位");
dt.Columns.Add("基本用量分子");
dt.Columns.Add("基本用量分母");
dt.Columns.Add("仓库名称");
dt.Columns.Add("用料车间");
dt.Columns.Add("工序名称");
dt.Columns.Add("物料类型");
dt.Columns.Add("替代标识");
dt.Columns.Add("库存数量");
dt.Columns.Add("库存单价");
dt.Columns.Add("库存金额");

DataRow dr = dt.NewRow();
dr["母键系统编码"] = iautoid;
dr["标识符"] = "母件";
dr["编码"] = mujianid;
dr["名称"] = mujianname;
dr["规格型号"] = mujianxinghao;

dt.Rows.Add(dr);

fetch(ref dt, iautoid, 0);


DataSet ds = new DataSet();
ds.Tables.Add(dt);

dataGridView2.DataSource = ds.Tables[0].DefaultView;
dataGridView2.AllowUserToAddRows = false;
dataGridView2.AllowUserToDeleteRows = false;
dataGridView2.AlternatingRowsDefaultCellStyle.BackColor = Color.Silver;

buttonoutdata.Enabled = true;



}
public void fetch(ref DataTable dt, String iautoid, int layer)
{
String QueryFetchString = " SELECT productstructures.ipid ,productstructures.childguid as childguid , flag = '', ProductStructures.AutoID,ProductStructures.cSocode as '版本号' ,ProductStructures.cPSPcode,ProductStructures.cPSCode as '子键编码',Inventory.cInvName as '子键名称',Inventory.cInvStd as 'jiliangdanweibianma',ComputationUnit.cComUnitCode as 'ComputationUnit-cCAAComunitCode ',ComputationUnit.cComUnitName as 'ComputationUnit - name',iPSQuantity=NULL ,case when ComputationUnit.cComUnitName IS null then null else ComputationUnit.iChangRate end as 'huansuanlv',Unit1.cComUnitName as '主计量单位' , cast(ProductStructures.iPSQuantity as float) as '基本用量分子',productstructures.tdqtyd as '基本用量分母',ProductStructures.iWasteRate as '损耗率',ProductStructures.cWhCode,Warehouse.cWhName as '仓库名称', ProductStructures.cPersonName as '库管员1',ProductStructures.cPersonName as '库管员2' ,ProductStructures.cDepCode, department.cDepName as '用料车间',ProductStructures.cProcedureName as '工序名称1',ProductStructures.cProcedureName as '工序名称2', ProductStructures.cInsertFirst as '插位1',ProductStructures.cInsertSecond as '插位2',ProductStructures.cInsertThird as '插位3' ,ProductStructures.cFree1, ProductStructures.cFree2 ,ProductStructures.cFree3, ProductStructures.cFree4 ,ProductStructures.cFree5, ProductStructures.cFree6 ,ProductStructures.cFree7, ProductStructures.cFree8 ,ProductStructures.cFree9, ProductStructures.cFree10,case dbo.productstructures.WIPType when 1 then '入库倒冲' when 3 then '正常领料' else '' end as '物料类型',case when dbo.productstructures.ReplaceFlag=1 then '是' else '否' end as '替代标识',Inventory.iInvSCost as '参考售价' FROM ProductStructures Left JOIN (Inventory Left Join ComputationUnit as Unit1 on Unit1.cComunitCode=Inventory.cComunitCode) ON Inventory.cInvCode = ProductStructures.cPSCode Left Join Department on Department.cDepCode=ProductStructures.cDepCode Left Join WareHouse On Warehouse.cWhCode=ProductStructures.cWhCode Left join ComputationUnit ON ComputationUnit.cComunitCode =Inventory.cSTComUnitCode where ipid = '{" + iautoid + "}' Order by ProductStructures.cPSCode";

using (GetSqlCon.connection = new SqlConnection(GetSqlConInfo.String))
{
GetSqlCon.connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = QueryFetchString;
command.Connection = GetSqlCon.connection;
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
DataRow dr = dt.NewRow();
String space = "";


for (int i = 0; i <= layer; i++)
{
space = space + ".";
}
dr["母键系统编码"] = reader["ipid"].ToString();
dr["子键系统编码"] = reader["childguid"].ToString();
dr["标识符"] = "子件" + space + (layer + 1);
dr["版本号"] = reader["版本号"].ToString();
dr["编码"] = reader["子键编码"].ToString();
dr["名称"] = reader["子键名称"].ToString();
dr["规格型号"] = "";
dr["主计量单位"] = reader["主计量单位"].ToString();
dr["基本用量分子"] = reader["基本用量分子"].ToString();
dr["基本用量分母"] = reader["基本用量分母"].ToString();
dr["仓库名称"] = reader["仓库名称"].ToString();
dr["用料车间"] = reader["用料车间"].ToString();
dr["物料类型"] = reader["物料类型"].ToString();
dr["替代标识"] = reader["替代标识"].ToString();



//获取查询结存信息的查询条件
jiecuncheck = reader["子键编码"].ToString();
//获取下一级递归的判断条件
iautoid = reader["childguid"].ToString();

String PriceQuery = "select Inventory.cInvCode,cinvname,cinvccode,isnull((select sum(IA_Subsidiary.iAInQuantity)from IA_Subsidiary where IA_Subsidiary.cInvCode = Inventory.cInvCode ),0) as '收入数量',isnull((select sum(IA_Subsidiary.iAOutQuantity) from IA_Subsidiary where IA_Subsidiary.cInvCode = Inventory.cInvCode ) ,0)as '发出数量',isnull((select sum(IA_Subsidiary.iAInPrice)from IA_Subsidiary where IA_Subsidiary.cInvCode = Inventory.cInvCode ),0) as '收入金额',isnull((select sum(IA_Subsidiary.iAOutPrice)from IA_Subsidiary where IA_Subsidiary.cInvCode = Inventory.cInvCode ),0) as '发出金额',iincost as '收入单价',iOutCost as '发出单价'from Inventory left join IA_Subsidiary on IA_Subsidiary.cInvCode=Inventory.cInvCode where Inventory.cInvCode = '" + jiecuncheck + "'";
using(GetSqlCon.connection = new SqlConnection(GetSqlConInfo.String))
{
GetSqlCon.connection.Open();

SqlCommand pricecom = new SqlCommand();
pricecom.Connection = GetSqlCon.connection;
pricecom.CommandText = PriceQuery;
SqlDataReader Pricesdr = pricecom.ExecuteReader();
Pricesdr.Read();


if (iautoid == "")
{
dr["库存数量"] = Pricesdr.GetDouble(3) - Pricesdr.GetDouble(4);

if (Pricesdr["收入单价"].ToString() != "" && Convert.ToDouble(dr["库存数量"]) > 0)
{
dr["库存单价"] = Pricesdr["收入单价"].ToString();
}
else if (Convert.ToDouble(dr["库存数量"]) > 0)
{
dr["库存单价"] = Pricesdr["发出单价"].ToString();

}
dr["库存金额"] = Convert.ToDecimal(Pricesdr[5]) - Convert.ToDecimal(Pricesdr[6]);
}

GetSqlCon.connection.Close();

}


dt.Rows.Add(dr);
//设置递归条件

if (iautoid != "")
{

layer++;
fetch(ref dt, iautoid, layer);


}
}

GetSqlCon.connection.Close();

}
}

private void buttonoutdata_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
//saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出EXCEL格式数据";
saveFileDialog.ShowDialog();



System.Reflection.Missing miss = System.Reflection.Missing.Value;



Excel.Application excel = new Excel.ApplicationClass();
Excel.Workbooks books = (Excel.Workbooks)excel.Workbooks;
Excel.Workbook book = (Excel.Workbook)(books.Add(miss));
Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;
sheet.Name = "sheet1";

int colIndex = 0;
foreach (DataGridViewColumn column in dataGridView2.Columns)
{

colIndex++;
excel.Cells[1, colIndex] = column.HeaderText;
}

for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
excel.Cells[i + 2, j + 1] = dataGridView2.Rows[i].Cells[j].Value;
}
}
excel.Columns.AutoFit();

String strName = saveFileDialog.FileName.ToString();
if (strName != "")
{
try
{
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
MessageBox.Show("成功导出数据!");
}

catch (Exception ex)
{
String errorinfo = ex.Message.ToString() + "请先关闭这个文件。";
MessageBox.Show(errorinfo);
}


book.Close(false, miss, miss);
books.Close();

excel.Quit();

}

//System.Runtime.InteropServices.Marshal.ReleaseComObject();

System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);

System.Runtime.InteropServices.Marshal.ReleaseComObject(book);

System.Runtime.InteropServices.Marshal.ReleaseComObject(books);

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

GC.Collect();

}

private void buttonexit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void buttontree_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();

String QuerySubString = " select iAutoId,CPSPCode,Inventory.cInvName,ProductStructure.cDepCode,Department.cDepName,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,bExpand,iFinishExch,cCheck,(ProductStructure.cMemo) as cMemo from ProductStructure left join Inventory on ProductStructure.CPSPCode=Inventory.cInvCode left join department on ProductStructure.cDepCode=Department.cDepCode where iAutoId = '{" + iautoid + "}'";
using (GetSqlCon.connection = new SqlConnection(GetSqlConInfo.String))
{

GetSqlCon.connection.Open();

SqlCommand com = new SqlCommand();
com.CommandText = QuerySubString;
com.Connection = GetSqlCon.connection;
SqlDataReader sdr = com.ExecuteReader();
sdr.Read();

TreeNode node = new TreeNode();
node.Text = "母件->" + sdr[1].ToString() + "->" + sdr[2].ToString();
treeView1.Nodes.Add(node);

sdr.Close();

GetChildrenNode(node, iautoid, 0);


GetSqlCon.connection.Close();



}
}
public void GetChildrenNode(TreeNode node, string temp, int layer)
{
using (GetSqlCon.connection = new SqlConnection(GetSqlConInfo.String))
{
GetSqlCon.connection.Open();

String QuerySubString = " SELECT productstructures.ipid ,productstructures.childguid as childguid , flag = '', ProductStructures.AutoID,ProductStructures.cSocode as '版本号' ,ProductStructures.cPSPcode,ProductStructures.cPSCode as '子键编码',Inventory.cInvName as '子键名称',Inventory.cInvStd as 'jiliangdanweibianma',ComputationUnit.cComUnitCode as 'ComputationUnit-cCAAComunitCode ',ComputationUnit.cComUnitName as 'ComputationUnit - name',iPSQuantity=NULL ,case when ComputationUnit.cComUnitName IS null then null else ComputationUnit.iChangRate end as 'huansuanlv',Unit1.cComUnitName as '主计量单位' , cast(ProductStructures.iPSQuantity as float) as '基本用量分子',productstructures.tdqtyd as '基本用量分母',ProductStructures.iWasteRate as '损耗率',ProductStructures.cWhCode,Warehouse.cWhName as '仓库名称', ProductStructures.cPersonName as '库管员1',ProductStructures.cPersonName as '库管员2' ,ProductStructures.cDepCode, department.cDepName as '用料车间',ProductStructures.cProcedureName as '工序名称1',ProductStructures.cProcedureName as '工序名称2', ProductStructures.cInsertFirst as '插位1',ProductStructures.cInsertSecond as '插位2',ProductStructures.cInsertThird as '插位3' ,ProductStructures.cFree1, ProductStructures.cFree2 ,ProductStructures.cFree3, ProductStructures.cFree4 ,ProductStructures.cFree5, ProductStructures.cFree6 ,ProductStructures.cFree7, ProductStructures.cFree8 ,ProductStructures.cFree9, ProductStructures.cFree10,case dbo.productstructures.WIPType when 1 then '入库倒冲' when 3 then '正常领料' else '' end as '物料类型',case when dbo.productstructures.ReplaceFlag=1 then '是' else '否' end as '替代标识',Inventory.iInvSCost as '参考售价' FROM ProductStructures Left JOIN (Inventory Left Join ComputationUnit as Unit1 on Unit1.cComunitCode=Inventory.cComunitCode) ON Inventory.cInvCode = ProductStructures.cPSCode Left Join Department on Department.cDepCode=ProductStructures.cDepCode Left Join WareHouse On Warehouse.cWhCode=ProductStructures.cWhCode Left join ComputationUnit ON ComputationUnit.cComunitCode =Inventory.cSTComUnitCode where ipid = '{" + temp + "}' Order by ProductStructures.cPSCode ";
SqlCommand com = new SqlCommand();
com.CommandTimeout = 0;
com.CommandText = QuerySubString;
com.Connection = GetSqlCon.connection;
SqlDataReader sdr = com.ExecuteReader();


if (sdr.HasRows)
{
while (sdr.Read())
{
TreeNode nodeChild = new TreeNode();

String Space = "";
for (int i = 0; i < layer; i++)
{
Space = Space + "|—";

}

nodeChild.Text = Space + "子件->" + sdr["子键编码"].ToString() + "->" + sdr["子键名称"].ToString();
treeView1.Nodes.Add(nodeChild);

if (sdr["childguid"].ToString() != "")
{
temp = sdr["childguid"].ToString();
layer++;
GetChildrenNode(node, temp, layer);
}

}
}

GetSqlCon.connection.Close();

}//using


}

private void tabPage1_Click(object sender, EventArgs e)
{

}
}
}






===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace U6数据导出工具

{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入服务器名称。","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if (textBox2.Text == "")
{
MessageBox.Show("请输入操作员名称。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (textBox4.Text == "")
{
MessageBox.Show("请输入帐套号。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (textBox5.Text == "")
{
MessageBox.Show("请输入会计年度。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
GetSqlConInfo.String = "Data Source=" + textBox1.Text.ToString() + ";Initial Catalog=UFDATA_" + textBox4.Text.ToString() + "_" + textBox5.Text.ToString() + ";User ID=" + textBox2.Text.ToString() + ";Password=" + textBox3.Text.ToString() + ";" + "Integrated Security=false";
using(GetSqlCon.connection = new SqlConnection(GetSqlConInfo.String))
{

try
{
GetSqlCon.connection.Open();

FileStream fs = new FileStream("./LoginInfo.txt", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
String LandInfo = textBox1.Text + "/n" + textBox2.Text + "/n" + textBox3.Text + "/n" + textBox4.Text + "/n" + textBox5.Text;
sw.WriteLine(LandInfo.Replace("/n", Environment.NewLine));
sw.Close();
fs.Close();

Form2 fm2 = new Form2(this);
fm2.ShowDialog();
this.Hide();
}
catch(Exception eform1)
{
MessageBox.Show(eform1.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);

}
}

}
}

private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists("./LoginInfo.txt"))
{
FileStream fs = new FileStream("./LoginInfo.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
textBox1.Text = sr.ReadLine();
textBox2.Text = sr.ReadLine();
textBox3.Text = sr.ReadLine();
textBox4.Text = sr.ReadLine();
textBox5.Text = sr.ReadLine();
sr.Close();
fs.Close();
}
}


}
public class GetSqlConInfo
{

public static String String = null;
}


public class GetSqlCon
{
public static SqlConnection connection = null;
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics