MySqlConnection conn = new MySqlConnection(GetConn.connection); //连接对象 private void button1_Click_1(object sender, EventArgs e) //添加样机按钮 { string PhoneStatus = "在库"; string PhoneCode = this.txtPhoneCode.Text; //PhoneID作为主键,可以在程序中自动生成,此处无需添加 string PhoneName = this.txtPhoneName.Text; string PhoneStage = this.cbbPhoneStage.Text; string PhoneNum = this.txtPhoneNum.Value.ToString(); string PhoneOwner = this.cbbPhoneOwner.Text; string PhoneInf1 = this.cbbInf1.Text; string PhoneInf2 = this.cbbInf2.Text; string PhoneInf3 = this.cbbInf3.Text; string PhoneInf4 = this.cbbInf4.Text; string PhoneNote = PhoneInf1 + "_" + PhoneInf2 + "_" + PhoneInf3 + "_" + PhoneInf4; PhoneBLL phonebll = new PhoneBLL(); //建立对象 if (this.txtPhoneName.Text == "" || this.cbbPhoneStage.Text == "" || this.txtPhoneNum.Value.ToString() == "" || this.cbbPhoneOwner.Text == "") { MessageBox.Show("样机名称,样机阶段,接口人信息必填", "提示"); return; } else if (phonebll.AddPhone(PhoneCode, PhoneName, PhoneStage, PhoneNum, PhoneStatus, PhoneNote, PhoneOwner, LoginForm.usrName)) { MessageBox.Show("数据上传成功!"); } phones = phonebll.getAllPhones(); //刷新选项内容 PhoneDataGrid.DataSource = phones; this.txtPhoneCode.Clear(); }
private void 样机输入ToolStripMenuItem_Click(object sender, EventArgs e) { string excelFilePath = ""; //excelFilePath = @"C:\Users\user\Desktop\EXCEL\TRY.xlsx"; OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = "请选择文件"; dialog.Filter = "Excel(*.xlsm)|*.xlsm|Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls"; dialog.Multiselect = false; //确定是否可以选择多个文件 //dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //dialog.InitialDirectory = Application.StartupPath ; dialog.InitialDirectory = @"\\version1\测试二所\样机管理系统\样机信息"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { excelFilePath = dialog.FileName; } else { return; } MExcel.Application app = new MExcel.Application(); MExcel.Sheets sheets; MExcel.Workbook workbook = null; object oMissing = System.Reflection.Missing.Value; //app.Visible = true; DataTable dt = new DataTable(); try { workbook = app.Workbooks.Open(excelFilePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); sheets = workbook.Worksheets; MExcel.Worksheet worksheet = (MExcel.Worksheet)sheets.get_Item(1); //用的是 worksheets 这一大类的 //MExcel.Worksheet worksheet = (MExcel.Worksheet)workbook.Worksheets[1]; int iRowCount = worksheet.get_Range("B65535", oMissing).get_End(MExcel.XlDirection.xlUp).Row; int iColCount = worksheet.UsedRange.Columns.Count; if (iRowCount < 4) { MessageBox.Show("请在文档中输入样机信息!"); return; } string PhoneCode; //PhoneID作为主键,可以在程序中自动生成,此处无需添加 string PhoneName; string PhoneStage; string PhoneStagePrint; string PhoneNum; string PhoneStatus = "在库"; //string PhoneNoteAll; string PhoneNote; string PhoneNote1; string PhoneNote2; string PhoneNote3; string PhoneNote4; string PhoneOwner; string log1, log2, log3, log4; MExcel.Range rngName = (MExcel.Range)worksheet.Cells[1, 2]; MExcel.Range rngStage = (MExcel.Range)worksheet.Cells[2, 2]; //MExcel.Range rngNoteAll = (MExcel.Range)worksheet.Cells[3, 2]; PhoneName = Convert.ToString(rngName.Value2); //STRING 用来将内容变为字符串 PhoneStage = Convert.ToString(rngStage.Value2); //PhoneNoteAll = Convert.ToString(rngNoteAll.Value2); MExcel.Range log1R = (MExcel.Range)worksheet.Cells[3, 1]; MExcel.Range log2R = (MExcel.Range)worksheet.Cells[3, 5]; MExcel.Range log3R = (MExcel.Range)worksheet.Cells[3, 6]; MExcel.Range log4R = (MExcel.Range)worksheet.Cells[3, 7]; log1 = Convert.ToString(log1R.Value2); //STRING 用来将内容变为字符串 log2 = Convert.ToString(log2R.Value2); log3 = Convert.ToString(log3R.Value2); log4 = Convert.ToString(log4R.Value2); if (PhoneName == null || PhoneStage == null || log1 == null || log2 == null || log3 == null || log4 == null) { MessageBox.Show("'型号' 与 '测试阶段' 是必填内容 或 使用最新模板"); return; } PhoneBLL phonebll = new PhoneBLL(); for (int i = 4; i <= iRowCount; i++) { MExcel.Range rngCode = (MExcel.Range)worksheet.Cells[i, 1]; MExcel.Range rngNum = (MExcel.Range)worksheet.Cells[i, 2]; //MExcel.Range rngNote = (MExcel.Range)worksheet.Cells[i, 4]; MExcel.Range rngOwner = (MExcel.Range)worksheet.Cells[i, 3]; MExcel.Range rngNote1 = (MExcel.Range)worksheet.Cells[i, 4]; MExcel.Range rngNote2 = (MExcel.Range)worksheet.Cells[i, 5]; MExcel.Range rngNote3 = (MExcel.Range)worksheet.Cells[i, 6]; MExcel.Range rngNote4 = (MExcel.Range)worksheet.Cells[i, 7]; PhoneCode = Convert.ToString(rngCode.Value2) + ""; //入库使用,PHONEID PhoneNum = Convert.ToString(rngNum.Value2) + ""; PhoneOwner = Convert.ToString(rngOwner.Value2) + ""; //入库使用,接口人 PhoneNote1 = Convert.ToString(rngNote1.Value2) + ""; //打印信息使用 PhoneNote2 = Convert.ToString(rngNote2.Value2) + ""; //打印信息使用 PhoneNote3 = Convert.ToString(rngNote3.Value2) + ""; //打印信息使用 PhoneNote4 = Convert.ToString(rngNote4.Value2) + ""; //打印信息使用 PhoneNote = PhoneNote1 + "_" + PhoneNote2 + "_" + PhoneNote3 + "_" + PhoneNote4; //入库使用,备注信息 PhoneStagePrint = PhoneStage + "_" + PhoneNum; //打印信息使用 ////打印开始****************************************************************************** ////打开打印端口 //TSCLIB_DLL.openport("Deli DL-888F(NEW)"); ////设计打印纸的大小与间距 //TSCLIB_DLL.setup("30", "20", "3", "10", "0", "2", "0"); //宽度、高度、速度寸/秒、浓度0-15、。 长度30 20 OK,倒数间距2 OK ////清空下缓存 //TSCLIB_DLL.clearbuffer(); ////打印文字 //TSCLIB_DLL.windowsfont(1, 15, 23, 0, 0, 0, "標楷體", PhoneName); //用windowsTTF字型列印文字 X、Y、字体高度、角度、字体外形、有无底线、字体名称、打印内容 //TSCLIB_DLL.windowsfont(1, 40, 23, 0, 0, 0, "標楷體", PhoneStagePrint); //用windowsTTF字型列印文字 X、Y、字体高度、角度、字体外形、有无底线、字体名称、打印内容 //TSCLIB_DLL.windowsfont(1, 65, 23, 0, 0, 0, "標楷體", PhoneNote1); //用windowsTTF字型列印文字 X、Y、字体高度、角度、字体外形、有无底线、字体名称、打印内容 //TSCLIB_DLL.windowsfont(1, 90, 23, 0, 0, 0, "標楷體", PhoneNote2); //用windowsTTF字型列印文字 X、Y、字体高度、角度、字体外形、有无底线、字体名称、打印内容 //TSCLIB_DLL.windowsfont(1, 115, 23, 0, 0, 0, "標楷體", PhoneNote3); //用windowsTTF字型列印文字 X、Y、字体高度、角度、字体外形、有无底线、字体名称、打印内容 //TSCLIB_DLL.windowsfont(1, 138, 23, 0, 0, 0, "標楷體", PhoneNote4); //用windowsTTF字型列印文字 X、Y、字体高度、角度、字体外形、有无底线、字体名称、打印内容 ////打印份数 //TSCLIB_DLL.printlabel("1", "1"); ////关闭打印端口 //TSCLIB_DLL.closeport(); ////打印结束******************************************************************************** if (phonebll.AddPhone(PhoneCode, PhoneName, PhoneStage, PhoneNum, PhoneStatus, PhoneNote, PhoneOwner, LoginForm.usrName)) { //MessageBox.Show("ok"); } } MessageBox.Show("样机信息导入成功^_^"); } catch (Exception excep) { MessageBox.Show(excep.ToString()); } finally { workbook.Close(false, oMissing, oMissing); //关闭 工作表 System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); workbook = null; app.Workbooks.Close(); //关闭应用 app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; } //COM组件方式读取 EXCEL中的数值 }