private void toCSVFileToolStripMenuItem_Click(object sender, EventArgs e) { _ = Task.Run(() => { try { IsRunning = true; if (!btnCorrect.InvokeRequired) { button2.Enabled = false; } else { button2.Invoke(new Action(() => { button2.Enabled = false; })); } string fileName = "EDocs_" + Guid.NewGuid().ToString("N").Substring(0, 4) + ".csv"; string location = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string customExcelSavingPath = location + "\\" + fileName; using (StreamWriter sw = File.CreateText(customExcelSavingPath)) { sw.WriteLine(EDoc.ToStringColumnsCSV()); for (int i = 0; i < edocs.Count; i++) { sw.WriteLine(edocs[i].ToStringCSV()); } } if (!btnCorrect.InvokeRequired) { button2.Enabled = true; } else { button2.Invoke(new Action(() => { button2.Enabled = true; })); } IsRunning = false; MessageBox.Show("Fayl: " + customExcelSavingPath + "", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex) { MessageBox.Show(ex?.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }); }
private void fromDBToolStripMenuItem_Click(object sender, EventArgs e) { if (this.tbYear.Text.Trim().Length != 4 || this.cbeMonth.SelectedIndex < 0) { this.panel1.Visible = true; return; } int year = Convert.ToInt32(this.tbYear.Text); int month = this.cbeMonth.SelectedIndex; try { string sql = $@"SELECT p.*, (SELECT TOP 1 Ad FROM Voen WITH(NOLOCK) WHERE [Voen] = p.Voen) AS Name FROM [Evhf_paid] p WITH(NOLOCK) WHERE p.Il = {year} AND p.Tip = 0 AND p.AyRub = {month} AND p.Evezlesen = N'Y';"; using (var connection = sqlCon) { connection.Open(); List <dynamic> list = connection.Query <dynamic>(sql).ToList(); foreach (var item in list) { EDoc edoc = new EDoc(); edoc.TIN = item.Voen; edoc.Name = item.Name; edoc.Date = item.Tarix; edoc.Serial = item.Evhf_sr; edoc.Number = item.Evhf_no; edoc.RowCode = item.Setir_kod; edoc.PayMain = item.Edvsiz; edoc.PayVAT = item.Edv; edoc.PaySum = edoc.PayMain + edoc.PayVAT; //list.Il; //list.Tip; //list.AyRub; //list.Evezlesen; //list.Line_id; //list.LastEditDateTime; //list.LastEditCompName; //list.LastEditUserName; edocs.Add(edoc); } gcEDocs.DataSource = edocs; gcEDocs.RefreshDataSource(); lblCount.Text = "Sətir sayı: " + edocs.Count.ToString(); this.nudSum.Value = Math.Round(edocs.Sum(x => x.PayVAT) / 0.18M, 2); this.button1.Enabled = true; IsRunning = false; MessageBox.Show(this, Text + " yüklənməsi sona çatdı!", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show(this, ex?.Message + "\n" + ex?.InnerException?.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; throw; } finally { System.GC.Collect(); } }
private void toolStripMenuItem2_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "Excel files (*.xls, *.xlsx, *.xlsm)|*.xls; *.xlsx; *.xlsm|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { IsRunning = true; button1.Enabled = false; try { string filePath = openFileDialog.FileName; using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) { int counter = 0; edocs = new List <EDoc>(); while (reader.Read()) { if (++counter == 1) { continue; } EDoc edoc = new EDoc(); if (reader.FieldCount >= 1) { edoc.TIN = reader.GetValue(0)?.ToString(); } if (edoc == null || edoc.TIN == null || edoc.TIN.Length == 0) { continue; } if (reader.FieldCount >= 2) { edoc.Name = reader.GetValue(1)?.ToString(); edoc.Name = edoc.Name.Replace("”", ""); } if (reader.FieldCount >= 3) { edoc.Date = Convert.ToDateTime(reader.GetValue(2)?.ToString()); } if (reader.FieldCount >= 4) { edoc.Serial = reader.GetValue(3)?.ToString(); } if (reader.FieldCount >= 5) { edoc.Number = reader.GetValue(4)?.ToString(); } if (reader.FieldCount >= 6) { edoc.RowCode = reader.GetValue(5)?.ToString(); } if (reader.FieldCount >= 7) { edoc.DocMain = Convert.ToDecimal(reader.GetValue(6)?.ToString()); } if (reader.FieldCount >= 8) { edoc.DocVAT = Convert.ToDecimal(reader.GetValue(7)?.ToString()); } if (reader.FieldCount >= 9) { edoc.DocSum = Convert.ToDecimal(reader.GetValue(8)?.ToString()); } if (reader.FieldCount >= 10) { edoc.PayMain = Convert.ToDecimal(reader.GetValue(9)?.ToString()); } if (reader.FieldCount >= 11) { edoc.PayVAT = Convert.ToDecimal(reader.GetValue(10)?.ToString()); } if (reader.FieldCount >= 12) { edoc.PaySum = Convert.ToDecimal(reader.GetValue(11)?.ToString()); } edocs.Add(edoc); } } } gcEDocs.DataSource = edocs; gcEDocs.RefreshDataSource(); lblCount.Text = "Sətir sayı: " + edocs.Count.ToString(); this.nudSum.Value = Math.Round(edocs.Sum(x => x.PayVAT) / 0.18M, 2); this.button1.Enabled = true; IsRunning = false; MessageBox.Show(this, Text + " yüklənməsi sona çatdı!", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch (Exception ex) { MessageBox.Show(this, ex?.Message + "\n" + ex?.InnerException?.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; throw; } finally { System.GC.Collect(); } } } }