/// <summary> /// 打开excel文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuOpenExcel_Click(object sender, EventArgs e) { ExcelReadForm frmExcel = new ExcelReadForm(); frmExcel.MdiParent = this; OpenFileDialog filedialog = new OpenFileDialog(); string FileName = ""; if (filedialog.ShowDialog() == DialogResult.OK) { FileName = filedialog.FileName; //先判断工作表中有几个sheet System.Data.DataTable dt = GetExcelDataTable(FileName); if (dt.Rows.Count > 1) { string str = string.Format("您所打开的工作簿中共有{0}张表,我们为您打开了第一张", dt.Rows.Count); MessageBox.Show(str, "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information); } frmExcel.fileName = FileName; frmExcel.isHaveManySheet = true; frmExcel.sheetNum = dt.Rows.Count; //打开excel文件 OpenExcel(frmExcel, FileName, "[Sheet1$]"); frmExcel.Show(); } }
/// <summary> /// 新建一个excel文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuNewExcel_Click(object sender, EventArgs e) { ExcelReadForm frmExceel = new ExcelReadForm(5); frmExceel.MdiParent = this; frmExceel.Show(); }
/// <summary> /// 打开Excel文件 /// </summary> /// <param name="frmExcel">数据显示到哪个对话框?????</param> private void OpenExcel(ExcelReadForm frmExcel, string fileName, string sheetNum) { //"[Sheet1$]" DataSet ds = GetExcelData(fileName, sheetNum); frmExcel.dGViewExcel.DataSource = ds; frmExcel.dGViewExcel.DataMember = sheetNum; #region MyRegion //for (int count = 0; (count <= (frmExcel.dGViewExcel.Rows.Count - 1)); count++) //{ // frmExcel.dGViewExcel.Rows[count].HeaderCell.Value = (count + 1).ToString(); //} #endregion }