Example #1
0
        /// <summary>
        /// 异步读取csv
        /// </summary>
        /// <returns></returns>
        private async Task ReadData()
        {
            if (!string.IsNullOrWhiteSpace(tb_FileName.Text))
            {
                ModelCsvData = null;
                Stopwatch     sc  = null;
                CsvReadHelper csv = null;

                try
                {
                    sc = Stopwatch.StartNew();
                    CsvFlag flag = new CsvFlag(Convert.ToChar(cob_separator.Text), Convert.ToChar(cob_FieldEnclosed.Text));
                    csv = new CsvReadHelper(tb_FileName.Text, Encoding.UTF8, flag, CancelSource.Token, !Convert.ToBoolean(cob_FirstIsHead.SelectedIndex), 40960);

                    await csv.ReadAsync(k =>
                    {
                        Sync.Post(f =>
                        {
                            var eve = f as CsvReadProgressInfo <TestProductModel>;

                            if (eve.CurrentRowsData != null)
                            {
                                if (ModelCsvData == null)
                                {
                                    InitDataGridView(eve.ColumnNames);
                                    ModelCsvData = eve.CurrentRowsData;
                                }
                                else
                                {
                                    ModelCsvData.AddRange(eve.CurrentRowsData);
                                }

                                if (eve.ProgressValue - pb_Progress.Value >= 10 || eve.ProgressValue == 100)
                                {
                                    RefreshDataGridView(ModelCsvData.Count);
                                }
                            }

                            pb_Progress.Value = Convert.ToInt32(eve.ProgressValue);
                        }, k);
                    }, f => ConvertCsvRowToTestProductData(f), 1000);
                }
                finally
                {
                    csv?.Close();
                    sc?.Stop();
                    Sync.Post(k =>
                    {
                        tb_Times.Text = k.ToString();
                    }, sc.Elapsed.TotalSeconds.ToString());
                }
            }
        }
        private void LoadExcel()
        {
            try
            {
                statusMain.Invoke(new MethodInvoker(delegate
                {
                    lbOperation.Text = "Load data...";
                }));
                isStop          = false;
                dataLoad        = new List <PreclaimMatchingViewModel>();
                CurrentDataView = new List <PreclaimMatchingViewModel>();
                dgvMain.Invoke(new MethodInvoker(delegate
                {
                    dgvMain.DataSource = CurrentDataView;
                }));
                DateTime    starttime   = DateTime.Now;
                ExcelHelper excelHelper = new ExcelHelper();
                //dataLoad = excelHelper.ReadExcelImportPreClaimMatching(filepath);
                //dataLoad = CsvReadHelper.ReadCSVPreClaimMatching(filepath);
                dataLoad = null;
                GC.Collect();
                dataLoad    = CsvReadHelper.ReadUnicodePreClaimMatching(filepath);
                excelHelper = null;
                DateTime endtime          = DateTime.Now;
                DateTime starttimeConvert = DateTime.Now;
                DateTime endtimeConvert   = DateTime.Now;
                if (dataLoad != null)
                {
                    #region Convert to unicode
                    if (isConverCompositeToUnicode)
                    {
                        ConvertToUnicode(dataLoad);
                        endtimeConvert = DateTime.Now;
                    }
                    #endregion
                    currentPage = 1;
                    if (dataLoad.Count % Core.LimitDisplayDGV == 0)
                    {
                        totalPage = dataLoad.Count / Core.LimitDisplayDGV;
                    }
                    else
                    {
                        totalPage = dataLoad.Count / Core.LimitDisplayDGV + 1;
                    }
                    lbTotalPage.Invoke(new MethodInvoker(delegate
                    {
                        lbTotalPage.Text = totalPage.ToString();
                    }));
                    txtPageCurrent.Invoke(new MethodInvoker(delegate
                    {
                        txtPageCurrent.Value = currentPage;
                    }));

                    dataLoad        = dataLoad.OrderBy(p => p.SerialNo).ToList();
                    CurrentDataView = dataLoad.Skip((currentPage - 1) * Core.LimitDisplayDGV).Take(Core.LimitDisplayDGV).ToList();
                    EnablePagging(currentPage, totalPage);
                    dgvMain.Invoke(new MethodInvoker(delegate
                    {
                        dgvMain.DataSource = CurrentDataView;
                    }));
                    lbLoad.Invoke(new MethodInvoker(delegate
                    {
                        lbLoad.Text = $"Total time load: {(endtime - starttime).TotalSeconds}(s), total record(s): {dataLoad.Count}";
                        if (isConverCompositeToUnicode)
                        {
                            lbLoad.Text += $", total time convert to Unicode: {(endtimeConvert - starttimeConvert).TotalSeconds}(s)";
                        }
                    }));
                    statusMain.Invoke(new MethodInvoker(delegate
                    {
                        lbOperation.Text = "Load data is finish";
                    }));
                    btnMatching.Invoke(new MethodInvoker(delegate
                    {
                        btnMatching.Enabled = true;
                    }));
                    btnResetMatching.Invoke(new MethodInvoker(delegate
                    {
                        btnResetMatching.Enabled = true;
                    }));
                    toolMain.Invoke(new MethodInvoker(delegate
                    {
                        btnExport.Enabled = true;
                    }));
                }
                else
                {
                    lbLoad.Invoke(new MethodInvoker(delegate
                    {
                        lbLoad.Text = $"Load data is error!";
                    }));
                    btnMatching.Invoke(new MethodInvoker(delegate
                    {
                        btnMatching.Enabled = false;
                    }));
                    btnResetMatching.Invoke(new MethodInvoker(delegate
                    {
                        btnResetMatching.Enabled = false;
                    }));
                    toolMain.Invoke(new MethodInvoker(delegate
                    {
                        btnExport.Enabled = false;
                    }));
                    statusMain.Invoke(new MethodInvoker(delegate
                    {
                        lbOperation.Text = "Load data is finish";
                    }));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Load data is error: {ex.ToString()}");
            }
        }
Example #3
0
        /// <summary>
        /// 异步读取csv
        /// </summary>
        /// <returns></returns>
        private async Task ReadData()
        {
            if (!string.IsNullOrWhiteSpace(this.tb_FileName.Text))
            {
                Stopwatch     sc  = null;
                CsvReadHelper csv = null;

                try
                {
                    this.modelCsvData = null;
                    CsvFlag flag = new CsvFlag(Convert.ToChar(this.cob_separator.Text), Convert.ToChar(this.cob_FieldEnclosed.Text));
                    csv = new CsvReadHelper(this.tb_FileName.Text, Encoding.UTF8, flag, !Convert.ToBoolean(this.cob_FirstIsHead.SelectedIndex), 40960);

                    Progress <CsvReadProgressInfo <TestProductModel> > prog = new Progress <CsvReadProgressInfo <TestProductModel> >(e =>
                    {
                        this.sync.Post(f =>
                        {
                            var eve = f as CsvReadProgressInfo <TestProductModel>;

                            if (eve.CurrentRowsData != null)
                            {
                                if (this.modelCsvData == null)
                                {
                                    InitDataGridView(eve.ColumnNames);
                                    this.modelCsvData = eve.CurrentRowsData;
                                }
                                else
                                {
                                    this.modelCsvData.AddRange(eve.CurrentRowsData);
                                }

                                this.RefreshDataGridView(this.modelCsvData.Count);
                            }

                            this.pb_Progress.Value = Convert.ToInt32(eve.ProgressValue);
                        }, e);
                    });

                    sc = Stopwatch.StartNew();
                    //因为 ui 线程同步执行 ReadAsync 中的部分代码, 所以用 Task.Run 在其它线程中执行, 避免 ui 阻塞.
                    await Task.Run(async() =>
                    {
                        await csv.ReadAsync(prog, f =>
                        {
                            return(ConvertCsvRowToTestProductData(f));
                        }, this.cancelSource.Token, 1000);
                    }, this.cancelSource.Token);
                }
                finally
                {
                    if (csv != null)
                    {
                        csv.Close();
                    }

                    if (sc != null)
                    {
                        sc.Stop();
                    }

                    this.sync.Post(k =>
                    {
                        this.tb_Times.Text = k.ToString();
                    }, sc.Elapsed.TotalSeconds.ToString());
                }
            }
        }
        private void LoadExcel()
        {
            try
            {
                statusMain.Invoke(new MethodInvoker(delegate
                {
                    lbOperation.Text = "Load data...";
                }));
                //isStop = false;
                CurrentDataView = new List <WorkTXT>();
                DateTime starttime = DateTime.Now;
                dataLoad = null;
                listErr.Clear();
                GC.Collect();
                WorkTXTRead workTXTRead = null;
                if (reportType == 0)
                {
                    //mau tong hop thong thuong
                    workTXTRead = CsvReadHelper.ReadUnicodeWorkTXTComon(filepath);
                }
                else if (reportType == 1)
                {
                    //mau an do nuoc ngoai(mau cu)
                    //workTXTRead = CsvReadHelper.ReadUnicodeWorkTXT(filepath);
                    //mau moi
                    workTXTRead = CsvReadHelper.ReadUnicodeWorkTXT3(filepath);
                }
                else
                {
                    //mau tac gia tieng viet, an do dua sang
                    workTXTRead = CsvReadHelper.ReadUnicodeWorkTXT2(filepath);
                }
                if (workTXTRead != null)
                {
                    dataLoad = workTXTRead.SuccessList;
                    listErr  = workTXTRead.FailList;
                    string dataerr = "";
                    for (int i = 0; i < listErr.Count; i++)
                    {
                        dataerr += $"{listErr[i]}{Environment.NewLine}";
                    }
                    rbFailList.Invoke(new MethodInvoker(delegate
                    {
                        rbFailList.Text = dataerr;
                    }));
                    lbFail.Invoke(new MethodInvoker(delegate
                    {
                        lbFail.Text = listErr.Count.ToString();
                    }));
                }
                DateTime endtime          = DateTime.Now;
                DateTime starttimeConvert = DateTime.Now;
                DateTime endtimeConvert   = DateTime.Now;
                if (dataLoad != null)
                {
                    currentPage = 1;
                    if (dataLoad.Count % Core.LimitDisplayDGV == 0)
                    {
                        totalPage = dataLoad.Count / Core.LimitDisplayDGV;
                    }
                    else
                    {
                        totalPage = dataLoad.Count / Core.LimitDisplayDGV + 1;
                    }
                    lbTotalPage.Invoke(new MethodInvoker(delegate
                    {
                        lbTotalPage.Text = totalPage.ToString();
                    }));
                    txtPageCurrent.Invoke(new MethodInvoker(delegate
                    {
                        txtPageCurrent.Value = currentPage;
                    }));

                    dataLoad        = dataLoad.OrderBy(p => p.SerialNo).ToList();
                    CurrentDataView = dataLoad.Skip((currentPage - 1) * Core.LimitDisplayDGV).Take(Core.LimitDisplayDGV).ToList();
                    EnablePagging(currentPage, totalPage);
                    dgvMain.Invoke(new MethodInvoker(delegate
                    {
                        dgvMain.DataSource = CurrentDataView;
                    }));
                    lbLoad.Invoke(new MethodInvoker(delegate
                    {
                        lbLoad.Text = $"Total time load: {(endtime - starttime).TotalSeconds}(s), total record(s): {dataLoad.Count}";
                    }));
                    statusMain.Invoke(new MethodInvoker(delegate
                    {
                        lbOperation.Text = "Load data is finish";
                    }));
                    btnSysToWork.Invoke(new MethodInvoker(delegate
                    {
                        btnSysToWork.Enabled = true;
                    }));
                }
                else
                {
                    dataLoad = new List <WorkTXT>();
                    lbLoad.Invoke(new MethodInvoker(delegate
                    {
                        lbLoad.Text = $"Load data is error!";
                    }));
                    btnSysToWork.Invoke(new MethodInvoker(delegate
                    {
                        btnSysToWork.Enabled = false;
                    }));
                    statusMain.Invoke(new MethodInvoker(delegate
                    {
                        lbOperation.Text = "Load data is finish";
                    }));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Load data is error: {ex.ToString()}");
            }
        }