Ejemplo n.º 1
0
        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 intMeasureType     = clsCommon.CnvNullToInt(dgvMeasure.Rows[dgvMeasure.CurrentCell.RowIndex].Cells[MeasureType.Index].Value);
            var emType             = intMeasureType == (int)emMeasureType.AlarmTest ? emMeasureType.AlarmTest : emMeasureType.WalkingTest;
            var pathFolder         = clsCommon.CnvNullToString(dgvMeasure.Rows[dgvMeasure.CurrentCell.RowIndex].Cells[MeasureID.Index].Value) + @"\";
            var fileNameReport     = (intMeasureType == (int)emMeasureType.AlarmTest ? REPORT_NAME_ALARM : REPORT_NAME_WALKING);
            var pathReport         = Path.GetTempPath() + @"\" + DateTime.Now.ToString(cstrDateTimeFormatNoMiliSecond2) + ".xlsx";
            var pathReportTemplate = Config.PathReportTemplate + fileNameReport;

            if (!Directory.Exists(pathFolder))
            {
                ShowMsg(MessageBoxIcon.Warning, "Data not found.");
                return;
            }

            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;
                }

                var searchPattern = clsConfig.FILE_NAME_DETAIL + @"*.csv";
                var arrData       = Directory.GetFiles(pathFolder, searchPattern);
                var cnn           = 0;

                try
                {
                    Array.Sort(arrData);

                    var name = Path.GetFileName(pathFolder.TrimEnd('\\'));
                    var arr  = name.Split('_');

                    if (arr.Length < 8)
                    {
                        ShowMsg(MessageBoxIcon.Error, MSG_ERR_PROCESS);
                        return;
                    }

                    File.Copy(pathReportTemplate, pathReport, true);

                    using (var objExport = new clsExportReport(pathReport))
                    {
                        var intType    = clsCommon.CnvNullToString(arr[0]) == ALARM_TEST_KEY ? (int)emMeasureType.AlarmTest : (int)emMeasureType.WalkingTest;
                        var intResult  = clsCommon.CnvNullToInt(arr[7]);
                        var dtMeasureS = DateTime.MinValue;
                        var dtMeasureE = DateTime.MaxValue;

                        DateTime.TryParseExact(arr[5], cstrDateTimeFormatNoMiliSecond2, null, DateTimeStyles.None, out dtMeasureS);
                        DateTime.TryParseExact(arr[6], cstrDateTimeFormatNoMiliSecond2, null, DateTimeStyles.None, out dtMeasureE);

                        objExport.WriteMeasureInfo(new MeasureInfo
                        {
                            ReportDate    = dtMeasureS,
                            MeasureStart  = dtMeasureS,
                            MeasureEnd    = dtMeasureE,
                            MeasureType   = emType,
                            MeasureResult = clsCommon.MeasureResultDisplay(intResult),
                            AlarmValue    = clsCommon.CnvNullToInt(arr[2]),
                            FailLevel     = clsCommon.CnvNullToInt(arr[3]),
                            Period        = clsCommon.CnvNullToInt(arr[4]),
                            DeviceName    = clsCommon.CnvNullToInt(arr[1]) == 1 ? Settings.Default.DeviceName1 : Settings.Default.DeviceName2
                        });

                        var rowStart = (emType == emMeasureType.AlarmTest ? clsExportReport.ROW_START_ALARM : clsExportReport.ROW_START_WALKING);

                        foreach (var p in arrData)
                        {
                            if (!File.Exists(p))
                            {
                                continue;
                            }

                            using (var reader = new StreamReader(p))
                            {
                                while (!reader.EndOfStream)
                                {
                                    var line   = reader.ReadLine();
                                    var values = line.TrimStart('"').TrimEnd('"').Split(new string[] { "\",\"" }, StringSplitOptions.None);

                                    if (objExport.WriteMeasureDetail(rowStart, new MeasureDetail
                                    {
                                        No = ++cnn,
                                        Time = clsCommon.CnvStringToDateTimeNull(values[0], null, cstrDateTimeFormatMiliSecond),
                                        Value = clsCommon.CnvNullToInt(values[4]),
                                        Result = clsCommon.MeasureResultDisplay(clsCommon.CnvNullToInt(values[5]))
                                    }, emType))
                                    {
                                        rowStart++;
                                    }
                                }
                            }
                        }

                        if (emType == emMeasureType.WalkingTest)
                        {
                            searchPattern = clsConfig.FILE_NAME_LIMIT + @"*.csv";
                            arrData       = Directory.GetFiles(pathFolder, searchPattern);
                            cnn           = 0;
                            Array.Sort(arrData);
                            rowStart = clsExportReport.ROW_START_WALKING_LIMIT;

                            foreach (var p in arrData)
                            {
                                if (!File.Exists(p))
                                {
                                    continue;
                                }

                                using (var reader = new StreamReader(p))
                                {
                                    while (!reader.EndOfStream)
                                    {
                                        var line   = reader.ReadLine();
                                        var values = line.TrimStart('"').TrimEnd('"').Split(new string[] { "\",\"" }, StringSplitOptions.None);

                                        if (objExport.WriteMeasureDetail(rowStart, new MeasureDetail
                                        {
                                            No = ++cnn,
                                            Time = clsCommon.CnvStringToDateTimeNull(values[0], null, cstrDateTimeFormatMiliSecond),
                                            Value = clsCommon.CnvNullToInt(values[4]),
                                            Result = clsCommon.MeasureResultDisplay(clsCommon.CnvNullToInt(values[5]))
                                        }, emType))
                                        {
                                            rowStart++;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    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;
                    }
                }
                catch (Exception ex)
                {
                    ShowMsg(MessageBoxIcon.Error, MSG_ERR_PROCESS);
                }
            }
        }
Ejemplo n.º 2
0
        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;
                }
            }
        }
Ejemplo n.º 3
0
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dgvMeasure.Rows.Count == 0)
            {
                ShowMsg(MessageBoxIcon.Warning, LanguageHelper.GetValueOf("MSG_WAR_DATA"));
                return;
            }

            if (dgvMeasure.CurrentCell == null)
            {
                ShowMsg(MessageBoxIcon.Warning, LanguageHelper.GetValueOf("MSG_WAR_EXCEL"));
                return;
            }

            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 intSensorId    = ConvertHelper.CnvNullToInt(dgvMeasure.CurrentRow.Cells[SensorID.Index].Value);
            var intMeasureType = ConvertHelper.CnvNullToInt(dgvMeasure.CurrentRow.Cells[MeasureType.Index].Value);
            var intAlarmValue  = ConvertHelper.CnvNullToInt(dgvMeasure.CurrentRow.Cells[AlarmValue.Index].Value);
            var intResult      = ConvertHelper.CnvNullToInt(dgvMeasure.CurrentRow.Cells[Result.Index].Value);
            var intMeasureId   = ConvertHelper.CnvNullToInt(dgvMeasure.CurrentRow.Cells[MeasureID.Index].Value);
            var strUserName    = ConvertHelper.CnvNullToString(dgvMeasure.CurrentRow.Cells[User.Index].Value);
            var strDeviceName  = ConvertHelper.CnvNullToString(dgvMeasure.CurrentRow.Cells[SensorName.Index].Value);
            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);
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

            fileNameReport = currentCulture + "_" + fileNameReport;
            var pathReportTemplate = AppManager.PathReportTemplate + fileNameReport;
            var cnn = 0;

            if (!File.Exists(pathReportTemplate))
            {
                pathReportTemplate = AppManager.PathReportTemplate + REPORT_NAME_ALARM;
                //ShowMsg(MessageBoxIcon.Warning, LanguageHelper.GetValueOf("MSG_WAR_EXCELTEMPLATE"));
                //return;
            }

            var threadExport = new Thread(() =>
            {
                this.SetModeWaiting();
                try
                {
                    using (var saveFileDialog = clsCommon.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,
                                DeviceName    = strDeviceName
                            });

                            // Detail
                            var keyDbName     = DBManagerChild.GetDBName(intSensorId);
                            var dataDetail    = AppManager.GetDBChildConnection(keyDbName).GetMeasureDetail(intMeasureId.ToString(), false, dtMeasureS, dtMeasureE);
                            var rowStarDetail = (emType == emMeasureType.AlarmTest ? clsExportReport.ROW_START_ALARM : clsExportReport.ROW_START_WALKING);

                            dataDetail.Select((measure) =>
                            {
                                if (objExport.WriteMeasureDetail(rowStarDetail, new clsConst.MeasureDetailExport
                                {
                                    No = ++cnn,
                                    Time = ConvertHelper.CnvStringToDateTimeNull(measure.Samples_time),
                                    Value = ConvertHelper.CnvNullToInt(measure.Actual_Value),
                                    Result = clsCommon.MeasureResultDisplay(measure.Detail_Result),
                                }, emType))
                                {
                                    rowStarDetail++;
                                }
                                return(measure);
                            }).ToList();
                        }

                        File.Copy(pathReport, saveFileDialog.FileName, true);

                        if (File.Exists(saveFileDialog.FileName))
                        {
                            if (!ComfirmMsg(LanguageHelper.GetValueOf("MSG_COMFIRM_EXCEL")))
                            {
                                return;
                            }

                            Process.Start(saveFileDialog.FileName);
                            return;
                        }
                        else
                        {
                            ShowMsg(MessageBoxIcon.Error, LanguageHelper.GetValueOf("MSG_ERR_EXCEL"), Text);
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error export to excel: " + ex.Message);
                }
                finally
                {
                    this.SetModeWaiting(false);
                }
            });

            threadExport.SetApartmentState(ApartmentState.STA);
            threadExport.Start();
        }