private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //从Excel中读取数据 using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { IWorkbook workbook = null; //新建IWorkbook对象 if (path.IndexOf(".xlsx") > 0) // 2007版本 { workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook } else if (path.IndexOf(".xls") > 0) // 2003版本 { workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook } //获取wk中的sheet ISheet sheet = workbook.GetSheetAt(0); IRow currentRow; //新建当前工作表行数据 List <ICell> listCells = new List <ICell>(); //list中保存当前行的所有的单元格内容 //遍历说有行 for (int r = 1; r <= sheet.LastRowNum; r++) { //获取每一行 currentRow = sheet.GetRow(r); well.Terminal_ID = currentRow.GetCell(0) == null ? "" : currentRow.GetCell(0).ToString(); well.Name = currentRow.GetCell(1) == null ? "" : currentRow.GetCell(1).ToString(); well.Terminal_Phone = currentRow.GetCell(2) == null ? "" : currentRow.GetCell(2).ToString(); well.Longitude = currentRow.GetCell(3) == null ? "" : currentRow.GetCell(3).ToString(); well.Latitude = currentRow.GetCell(4) == null ? "" : currentRow.GetCell(4).ToString(); well.Place = currentRow.GetCell(5) == null ? "" : currentRow.GetCell(5).ToString(); operatorInfo.Work_ID = currentRow.GetCell(6).ToString(); well.Operator_ID = user.GetOperatorByWork_ID(operatorInfo.Work_ID).ID; //执行sql语句 wellInfoService.InsertWellInfo(well); wellStateService.InsertWellCurrentStateInfo(well.Terminal_ID, 1); wellInfoService.InsertReportInfo(well.Terminal_ID, 1); worker.ReportProgress(r, sheet.LastRowNum); } } }
private void btnSure_Click(object sender, EventArgs e) { if (txtTerminal_ID.Text.Trim() == "") { UMessageBox.Show("请填写人井编号。", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (txtLongitude.Text.Trim() == "" && txtLatitude.Text.Trim() == "") { UMessageBox.Show("请填写经度纬度。", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (txtTerminal_Phone.Text.Trim() == "") { UMessageBox.Show("请填写终端手机号。", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (cbOperator.SelectedIndex == -1) { UMessageBox.Show("请选择值班人员。", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (tbReportInterval.Text.Trim() == "" && !isNumber(tbReportInterval.Text.Trim())) { UMessageBox.Show("请填写上报时间间隔。", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } this.DialogResult = DialogResult.OK; terminal_ID = wellInfo.Terminal_ID = txtTerminal_ID.Text.Trim(); wellInfo.Name = txtName.Text.Trim(); wellInfo.Longitude = txtLongitude.Text.Trim(); wellInfo.Latitude = txtLatitude.Text.Trim(); wellInfo.Place = txtPlace.Text.Trim(); wellInfo.Terminal_Phone = txtTerminal_Phone.Text.Trim(); wellInfo.Operator_ID = (int)cbOperator.SelectedValue; reportInterval = int.Parse(tbReportInterval.Text.Trim()); if (_isInsert) { if (wellInfoService.GetWellInfoByTerminal_ID(txtTerminal_ID.Text.Trim()) == null && wellInfoService.GetWellInfoByPhone(txtTerminal_Phone.Text.Trim()) == null) { wellInfoService.InsertWellInfo(wellInfo); wellStateService.InsertWellCurrentStateInfo(wellInfo.Terminal_ID, (int)cbState.SelectedValue); wellInfoService.InsertReportInfo(wellInfo.Terminal_ID, reportInterval); userLogHelper.InsertUserLog(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), "添加人井。", CommonClass.UserName, null, null); } else { UMessageBox.Show("当前人井已存在。", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { wellInfoService.UpdateWellInfo(wellInfo); wellStateService.UpdateWellCurrentStateInfo((int)cbState.SelectedValue, wellInfo.Terminal_ID); wellInfoService.UpdateReportInterval(reportInterval, wellInfo.Terminal_ID); userLogHelper.InsertUserLog(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), "更新人井信息。", CommonClass.UserName, null, null); } UMessageBox.Show(this.Text + "成功!", "人井监控管理系统", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }