using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using GeneralLib; namespace SunVoteARSPPT { public partial class FrmImportCandidate : Form { //数据开始行 private int StarRowIndex = 1; //候选人编号列 private int IDColIndex = 0; //候选人名称列 private int NameColIndex = 1; private DataGridView dgvVoterList = null; ExcelOper exl = new ExcelOper(); public FrmImportCandidate(DataGridView dgv) { InitializeComponent(); dgvVoterList = dgv; } private void lblCandidateName_Click(object sender, EventArgs e) { } private void btnOK_Click(object sender, EventArgs e) { if (txtFileName.Text == "") { MessageBox.Show("请选择导入文件", GlobalInfo.GetAppName()); btnFile.Focus() ; return; } if (cobCandidateID.Text == "") { MessageBox.Show("请选择候选人编号对应的列", GlobalInfo.GetAppName()); cobCandidateID.Focus(); return; } if (cobCandidateName.Text == "") { MessageBox.Show("请选择候选人名称对应的列", GlobalInfo.GetAppName()); cobCandidateName.Focus(); return; } string fileName = ""; string[,] PollList; //openFileDialog.InitialDirectory = "C:\\"; //openFileDialog.Filter = "Execl files (*.xls)|*.xls|Excel files (*.xlsx)|*.xlsx"; //if (openFileDialog.ShowDialog() == DialogResult.OK) //{ fileName = openFileDialog.FileName; exl.OpenExcel(fileName); PollList = exl.ImportToSheetBySheetNum(cobSheet.SelectedIndex + 1); int iRowCount = PollList.GetLength(0); int iColCount = PollList.GetLength(1); dgvVoterList.RowCount = iRowCount; for (int i = 0; i < iRowCount - 1; i++) { dgvVoterList.Rows[i].Cells[0].Value = PollList[i + StarRowIndex, IDColIndex]; dgvVoterList.Rows[i].Cells[1].Value = PollList[i + StarRowIndex, NameColIndex]; } ControlOper.SetGridRowH(dgvVoterList);//杨斌 2016-03-04 //} Close(); } private void btnFile_Click(object sender, EventArgs e) { openFileDialog.InitialDirectory = "C:\\"; //openFileDialog.Filter = "Execl files (*.xls)|*.xls|Excel files (*.xlsx)|*.xlsx"; openFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx";//杨斌 2018-12-18。不兼容2003格式 if (openFileDialog.ShowDialog() == DialogResult.OK) { string fileName = openFileDialog.FileName; txtFileName.Text = fileName; exl.OpenExcel(fileName); string [] sheetList= exl.ExcelSheetList(); ConvertToComboBox(cobSheet, sheetList); cobSheet.SelectedIndex = 0; // string[,] PollList = exl.ImportToSheetBySheetNum(1); } } private void cobSheet_SelectedIndexChanged(object sender, EventArgs e) { string[] ColList=null; if (cobSheet.Text != "") { ColList = exl.GetColList(cobSheet.Text); ConvertToComboBox(cobCandidateID, ColList); cobCandidateID.SelectedIndex = 0; ConvertToComboBox(cobCandidateName, ColList); cobCandidateName.SelectedIndex = 1; } } private void ConvertToComboBox(ComboBox cb, string[] slist) { cb.Items.Clear(); for (int i = 0; i < slist.Length; i++) { cb.Items.Add(slist[i]); } } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void cobCandidateID_SelectedIndexChanged(object sender, EventArgs e) { IDColIndex = cobCandidateID.SelectedIndex ; } private void cobCandidateName_SelectedIndexChanged(object sender, EventArgs e) { NameColIndex = cobCandidateName.SelectedIndex; } private void FrmImportCandidate_Load(object sender, EventArgs e) { } } }