private void btnExport_Click(object sender, EventArgs e) { if (dgvMeasure.Rows.Count == 0) { ShowMsg(MessageBoxIcon.Warning, "Data not found."); return; } if (dgvMeasure.CurrentCell == null) { ShowMsg(MessageBoxIcon.Warning, "Please select 1 row data to export excel."); return; } var strSDate = dgvMeasure.CurrentRow.Cells[StartTime.Index].Value.ToString(); var strEDate = dgvMeasure.CurrentRow.Cells[EndTime.Index].Value.ToString(); int intUser = clsCommon.CnvNullToInt(cmbUser.SelectedValue); int intType = clsCommon.CnvNullToInt(cmbType.SelectedValue); //int intResult = clsCommon.CnvNullToInt(cmbResult.SelectedValue); var objMeasureS = dgvMeasure.CurrentRow.Cells[StartTime.Index].Value; var objMeasureE = dgvMeasure.CurrentRow.Cells[EndTime.Index].Value; var dtMeasureS = objMeasureS != null && objMeasureS != DBNull.Value ? (DateTime?)objMeasureS : null; var dtMeasureE = objMeasureE != null && objMeasureE != DBNull.Value ? (DateTime?)objMeasureE : null; var intMeasureType = clsCommon.CnvNullToInt(dgvMeasure.CurrentRow.Cells[MeasureType.Index].Value); var intAlarmValue = clsCommon.CnvNullToInt(dgvMeasure.CurrentRow.Cells[AlarmValue.Index].Value); var intFailLevel = clsCommon.CnvNullToInt(dgvMeasure.CurrentRow.Cells[FailLevel.Index].Value); var intPeriod = clsCommon.CnvNullToInt(dgvMeasure.CurrentRow.Cells[colPeriod.Index].Value); var intResult = clsCommon.CnvNullToInt(dgvMeasure.CurrentRow.Cells[Result.Index].Value); var intMeasureId = clsCommon.CnvNullToInt(dgvMeasure.CurrentRow.Cells[MeasureID.Index].Value); var strUserName = clsCommon.CnvNullToString(dgvMeasure.CurrentRow.Cells[User.Index].Value); var strDeviceName = clsCommon.CnvNullToString(dgvMeasure.CurrentRow.Cells[DeviceName.Index].Value); var strType = intMeasureType == (int)clsDBUltity.emMeasureType.AlarmTest ? ALARM_TEST : WALKING_TEST; var emType = intMeasureType == (int)emMeasureType.AlarmTest ? emMeasureType.AlarmTest : emMeasureType.WalkingTest; var pathReport = Path.GetTempPath() + @"\" + DateTime.Now.ToString(cstrDateTimeFormatNoMiliSecond2) + "xlsx"; var fileNameReport = (intMeasureType == (int)emMeasureType.AlarmTest ? REPORT_NAME_ALARM : REPORT_NAME_WALKING); var pathReportTemplate = Config.PathReportTemplate + fileNameReport; var cnn = 0; if (!File.Exists(pathReportTemplate)) { ShowMsg(MessageBoxIcon.Warning, "Excel template not found."); return; } using (var saveFileDialog = SaveExcelDialog(DateTime.Now.ToString(cstrDateTimeFormatNoMiliSecond2) + "_report")) { if (saveFileDialog.ShowDialog() != DialogResult.OK) { return; } File.Copy(pathReportTemplate, pathReport, true); using (var objExport = new clsExportReport(pathReport)) { // Header objExport.WriteMeasureInfo(new MeasureInfo { MeasureId = intMeasureId, UserName = strUserName, ReportDate = dtMeasureS, MeasureStart = dtMeasureS, MeasureEnd = dtMeasureE, MeasureType = emType, MeasureResult = clsCommon.MeasureResultDisplay(intResult), AlarmValue = intAlarmValue, FailLevel = intFailLevel, Period = intPeriod, DeviceName = strDeviceName }, false); // Limit if (emType == emMeasureType.WalkingTest) { var dataLimit = _objDB.GetTBLMeasureDetail(intMeasureId.ToString(), true); var rowStartLimit = clsExportReport.ROW_START_WALKING_LIMIT + 1; cnn = 0; foreach (DataRow row in dataLimit.Rows) { if (objExport.WriteMeasureDetail(rowStartLimit, new MeasureDetail { No = ++cnn, Time = clsCommon.CnvStringToDateTimeNull(row["samples_time"]), Value = clsCommon.CnvNullToInt(row["actual_delegate"]), Result = clsCommon.MeasureResultDisplay(clsCommon.CnvNullToInt(row["result"])), }, emType)) { rowStartLimit++; } } } // Detail var dataDetail = _objDB.GetTBLMeasureDetail(intMeasureId.ToString(), false); var rowStarDetail = (emType == emMeasureType.AlarmTest ? clsExportReport.ROW_START_ALARM : clsExportReport.ROW_START_WALKING) + 1; cnn = 0; foreach (DataRow row in dataDetail.Rows) { if (objExport.WriteMeasureDetail(rowStarDetail, new MeasureDetail { No = ++cnn, Time = clsCommon.CnvStringToDateTimeNull(row["samples_time"]), Value = clsCommon.CnvNullToInt(row["actual_delegate"]), Result = clsCommon.MeasureResultDisplay(clsCommon.CnvNullToInt(row["result"])), }, emType)) { rowStarDetail++; } } } File.Copy(pathReport, saveFileDialog.FileName, true); if (File.Exists(saveFileDialog.FileName)) { if (!ComfirmMsg("Do you want open file report?")) { return; } Process.Start(saveFileDialog.FileName); return; } else { ShowMsg(MessageBoxIcon.Error, "Export Excel erors.", Text); return; } } }