コード例 #1
0
 public static void Add(DataGridView dgv, InfoVo info)
 {
     MainForm.dataBindings.Add(info);
 }
コード例 #2
0
        public static void ImportExcel2DGV(DataGridView dgv)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title  = "选择待导入的数据文件";
            ofd.Filter = "Excel Workbook 97-2003|*.xls|Excel Workbook|*.xlsx";
            string filePath;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filePath = Path.GetFullPath(ofd.FileName);
                FileStream fs;
                try
                {
                    fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    NPOI.HSSF.UserModel.HSSFWorkbook book =
                        new NPOI.HSSF.UserModel.HSSFWorkbook(fs);

                    int sheetCount = book.NumberOfSheets;

                    for (int index = 0; index < sheetCount; index++)
                    {
                        NPOI.SS.UserModel.ISheet sheet = book.GetSheetAt(index);
                        if (sheet == null)
                        {
                            continue;
                        }

                        NPOI.SS.UserModel.IRow row = sheet.GetRow(0);

                        if (row == null)
                        {
                            continue;
                        }



                        int firstCellNum = row.FirstCellNum;
                        int lastCellNum  = row.LastCellNum;


                        if (firstCellNum == lastCellNum || lastCellNum - firstCellNum < 2)
                        {
                            continue;
                        }

                        //MainForm.dataBindings.Clear(); //清空表中数据

                        //确定第一行是否是数据行
                        int count     = 0; //记录表中数据字段的总数
                        int start_row = 0;
                        for (int i = firstCellNum; i < lastCellNum; i++)
                        {
                            //if(row.GetCell(i) != null)
                            //{
                            //row.GetCell(i).SetCellType(NPOI.SS.UserModel.CellType.String);
                            if (GPA_CALC.isNum(row.GetCell(i).StringCellValue))
                            {
                                count++;
                            }
                            //}
                        }
                        if (count == 0)
                        {
                            start_row = 1;
                        }

                        for (int i = start_row; i <= sheet.LastRowNum; i++)
                        {
                            string courseName = sheet.GetRow(i).Cells[0].StringCellValue;
                            //MessageBox.Show(sheet.GetRow(i).Cells.Count.ToString());
                            double credit;
                            double grade;
                            try
                            {
                                sheet.GetRow(i).Cells[1].SetCellType(NPOI.SS.UserModel.CellType.String);
                                sheet.GetRow(i).Cells[2].SetCellType(NPOI.SS.UserModel.CellType.String);
                                credit = Convert.ToDouble(sheet.GetRow(i).Cells[1].StringCellValue);
                                grade  = Convert.ToDouble(sheet.GetRow(i).Cells[2].StringCellValue);
                            }
                            catch (Exception e)
                            {
                                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                credit = 0;
                                grade  = 0;
                            }
                            //MessageBox.Show(sheet.LastRowNum.ToString());
                            InfoVo info = new InfoVo()
                            {
                                CourseName = courseName, Credit = credit, Grade = grade
                            };

                            //原创的处理InvalidOperationException的方法,当选中新行时会触发使得dataBinding添加异常的一行
                            if (dgv.CurrentRow != null && dgv.CurrentRow.IsNewRow)//别缺少前面的条件了,否则NRE
                            {
                                MainForm.dataBindings.RemoveAt(MainForm.dataBindings.Count - 1);
                            }
                            //dgv.ClearSelection();

                            MainForm.dataBindings.Add(info);
                        }
                    }
                    fs.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }