private void ExcelImport_Click(object sender, EventArgs e) { try { Dictionary<string, string> PartFieldDic = new Dictionary<string, string>();//配件列表的列名 for (int i = 1; i < gvPartsMsgList.Columns.Count; i++) {//添加配件列字段名和标题文本 PartFieldDic.Add(gvPartsMsgList.Columns[i].Name.ToString(), gvPartsMsgList.Columns[i].HeaderText.ToString()); } //弹出导入Excel对话框 FrmExcelImport frmExcel = new FrmExcelImport(PartFieldDic, ImportTitle); DialogResult DgResult = frmExcel.ShowDialog(); if (DgResult == DialogResult.OK) { string PNumExcelField = string.Empty; string PNamExcelField = string.Empty; string PSpcExcelField = string.Empty; string PDrwExcelField = string.Empty; string PUntExcelField = string.Empty; string PBrdExcelField = string.Empty; string PCfcExcelField = string.Empty; string PBrcExcelField = string.Empty; string PCntExcelField = string.Empty; string PMupExcelField = string.Empty; string PMprExcelField = string.Empty; string PMapExcelField = string.Empty; string PCmyExcelField = string.Empty; string PRmkExcelField = string.Empty; //获取与单据列名匹配的Excel表格列名 foreach (DictionaryEntry DicEty in frmExcel.MatchFieldHTable) { string BillField = DicEty.Key.ToString();//获取匹配单据的列名 string ExcelField = DicEty.Value.ToString();//获取匹配Excel表格的列名 switch (BillField) { case PNum: PNumExcelField = ExcelField; break; case PNam: PNamExcelField = ExcelField; break; case PSpc: PSpcExcelField = ExcelField; break; case PDrw: PDrwExcelField = ExcelField; break; case PUnt: PUntExcelField = ExcelField; break; case PBrd: PBrdExcelField = ExcelField; break; case PCfc: PCfcExcelField = ExcelField; break; case PBrc: PBrcExcelField = ExcelField; break; case PCnt: PCntExcelField = ExcelField; break; case PMup: PMupExcelField = ExcelField; break; case PMpr: PMprExcelField = ExcelField; break; case PMap: PMapExcelField = ExcelField; break; case PCmy: PCmyExcelField = ExcelField; break; case PRmk: PRmkExcelField = ExcelField; break; } } if (ValidityCellIsnull(frmExcel.ExcelTable)) { for (int i = 0; i < frmExcel.ExcelTable.Rows.Count; i++) { DataGridViewRow DgRow = gvPartsMsgList.Rows[gvPartsMsgList.Rows.Add()];//创建新行项 DgRow.Cells[PNum].Value = frmExcel.ExcelTable.Rows[i][PNumExcelField].ToString(); DgRow.Cells[PNam].Value = frmExcel.ExcelTable.Rows[i][PNamExcelField].ToString(); DgRow.Cells[PSpc].Value = frmExcel.ExcelTable.Rows[i][PSpcExcelField].ToString(); DgRow.Cells[PDrw].Value = frmExcel.ExcelTable.Rows[i][PDrwExcelField].ToString(); DgRow.Cells[PUnt].Value = frmExcel.ExcelTable.Rows[i][PUntExcelField].ToString(); DgRow.Cells[PBrd].Value = frmExcel.ExcelTable.Rows[i][PBrdExcelField].ToString(); DgRow.Cells[PCfc].Value = frmExcel.ExcelTable.Rows[i][PCfcExcelField].ToString(); DgRow.Cells[PBrc].Value = frmExcel.ExcelTable.Rows[i][PBrcExcelField].ToString(); DgRow.Cells[PCnt].Value = frmExcel.ExcelTable.Rows[i][PCntExcelField].ToString(); DgRow.Cells[PMup].Value = frmExcel.ExcelTable.Rows[i][PMupExcelField].ToString(); DgRow.Cells[PMpr].Value = frmExcel.ExcelTable.Rows[i][PMprExcelField].ToString(); DgRow.Cells[PMap].Value = frmExcel.ExcelTable.Rows[i][PMapExcelField].ToString(); DgRow.Cells[PCmy].Value = frmExcel.ExcelTable.Rows[i][PCmyExcelField].ToString(); DgRow.Cells[PRmk].Value = frmExcel.ExcelTable.Rows[i][PRmkExcelField].ToString(); } } frmExcel.ExcelTable.Clear();//清空所有配件信息 } } catch (Exception ex) { MessageBoxEx.Show(ex.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } }
/// <summary> /// 以Execl文件格式导入配件信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ExcelImport_Click(object sender, EventArgs e) { try { Dictionary<string, string> PartFieldDic = new Dictionary<string, string>();//配件列表的列名 for (int i = 1; i < gvPartsMsgList.Columns.Count; i++) {//添加配件列字段名和标题文本 PartFieldDic.Add(gvPartsMsgList.Columns[i].Name.ToString(), gvPartsMsgList.Columns[i].HeaderText.ToString()); } //弹出导入Excel对话框 FrmExcelImport frmExcel = new FrmExcelImport(PartFieldDic, ImportTitle); DialogResult DgResult = frmExcel.ShowDialog(); if (DgResult == DialogResult.OK) { int SerialNum = 1;//列序号 string PNumExcelField = string.Empty; string PNamExcelField = string.Empty; string PSpcExcelField = string.Empty; string PDrwExcelField = string.Empty; string PUntExcelField = string.Empty; string PBrdExcelField = string.Empty; string PCpcExcelField = string.Empty; string PBrcExcelField = string.Empty; string PCntExcelField = string.Empty; string PClpExcelField = string.Empty; string PTmyExcelField = string.Empty; string PMkdExcelField = string.Empty; string PVldExcelField = string.Empty; string PRmkExcelField = string.Empty; //获取与单据列名匹配的Excel表格列名 foreach (DictionaryEntry DicEty in frmExcel.MatchFieldHTable) { string BillField = DicEty.Key.ToString();//获取匹配单据的列名 string ExcelField = DicEty.Value.ToString();//获取匹配Excel表格的列名 switch (BillField) { case PNum: PNumExcelField = ExcelField; break; case PNam: PNamExcelField = ExcelField; break; case PSpc: PSpcExcelField = ExcelField; break; case PDrw: PDrwExcelField = ExcelField; break; case PUnt: PUntExcelField = ExcelField; break; case PBrd: PBrdExcelField = ExcelField; break; case PCpc: PCpcExcelField = ExcelField; break; case PBrc: PBrcExcelField = ExcelField; break; case PCnt: PCntExcelField = ExcelField; break; case PClp: PClpExcelField = ExcelField; break; case PTmy: PTmyExcelField = ExcelField; break; case PMkd: PMkdExcelField = ExcelField; break; case PVld: PVldExcelField = ExcelField; break; case PRmk: PRmkExcelField = ExcelField; break; } } for (int i = 0; i < frmExcel.ExcelTable.Rows.Count; i++) { for (int j = 0; j < frmExcel.ExcelTable.Columns.Count; j++) { if (string.IsNullOrEmpty(frmExcel.ExcelTable.Rows[i][j].ToString())) { MessageBoxEx.Show("Excel配件信息模板中存在空数据行项!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); gvPartsMsgList.Rows.Clear();//清空之前导入数据 return; } else { DataGridViewRow DgRow = gvPartsMsgList.Rows[gvPartsMsgList.Rows.Add()];//创建新行项 DgRow.Cells[PID].Value = SerialNum; DgRow.Cells[PNum].Value = frmExcel.ExcelTable.Rows[i][PNumExcelField].ToString(); DgRow.Cells[PNam].Value = frmExcel.ExcelTable.Rows[i][PNamExcelField].ToString(); DgRow.Cells[PSpc].Value = frmExcel.ExcelTable.Rows[i][PSpcExcelField].ToString(); DgRow.Cells[PDrw].Value = frmExcel.ExcelTable.Rows[i][PDrwExcelField].ToString(); DgRow.Cells[PUnt].Value = frmExcel.ExcelTable.Rows[i][PUntExcelField].ToString(); DgRow.Cells[PBrd].Value = frmExcel.ExcelTable.Rows[i][PBrdExcelField].ToString(); DgRow.Cells[PCpc].Value = frmExcel.ExcelTable.Rows[i][PCpcExcelField].ToString(); DgRow.Cells[PBrc].Value = frmExcel.ExcelTable.Rows[i][PBrcExcelField].ToString(); DgRow.Cells[PCnt].Value = frmExcel.ExcelTable.Rows[i][PCntExcelField].ToString(); DgRow.Cells[PClp].Value = frmExcel.ExcelTable.Rows[i][PClpExcelField].ToString(); DgRow.Cells[PTmy].Value = frmExcel.ExcelTable.Rows[i][PTmyExcelField].ToString(); DgRow.Cells[PMkd].Value = frmExcel.ExcelTable.Rows[i][PMkdExcelField].ToString(); DgRow.Cells[PVld].Value = frmExcel.ExcelTable.Rows[i][PVldExcelField].ToString(); DgRow.Cells[PRmk].Value = frmExcel.ExcelTable.Rows[i][PRmkExcelField].ToString(); SerialNum++;//序号自动增加 } } } frmExcel.ExcelTable.Clear();//清空所有配件信息 } } catch (Exception ex) { MessageBoxEx.Show(ex.Message, "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Question); } }