Beispiel #1
0
        private void bgW_luuexel_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Thread.Sleep(100);
            ThamsoKetqua kq = (ThamsoKetqua)e.Result;

            if (kq.err)
            {
                try
                {
                    Process pro = new Process();
                    pro.StartInfo = new ProcessStartInfo(kq.filename);
                    pro.Start();
                }
                catch
                {
                }
            }
            else
            {
                MessageBox.Show("Lỗi trong khi xuất file excel!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.Cursor                 = Cursors.Arrow;
            dataGridView1.Cursor        = Cursors.Arrow;
            toolStripProgressBar1.Value = 0;
        }
Beispiel #2
0
        private void bgW_luuexel_DoWork(object sender, DoWorkEventArgs e)
        {
            string filename = e.Argument.ToString();

            System.Globalization.CultureInfo oldcul = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            Excel.Application exapp = new Excel.Application();
            ThamsoKetqua      kq    = new ThamsoKetqua();

            bgW_luuexel.ReportProgress(0);
            try
            {
                exapp.Workbooks.Add(Missing.Value);
                Excel.Workbook wkb = exapp.Workbooks.get_Item(1);
                //wkb. = Excel.XlFileFormat.xlExcel9795;
                Excel.Worksheet wks       = (Excel.Worksheet)wkb.Worksheets.get_Item(1);
                int             offsetrow = 3;
                //int offsetcol = 1;
                int colnum = 2;
                ((Excel.Range)wks.Cells[offsetrow - 1, 1]).Value2 = "Stt";
                string colname;
                foreach (DataColumn dc in datasetDanhsachtontamung1.CHITIETTAMUNG.Columns)
                {
                    if (dc.ColumnName == "maql" || dc.ColumnName == "mavaovien")
                    {
                        continue;
                    }
                    switch (dc.ColumnName)
                    {
                    case "mabn":
                        colname = "Mã BN";
                        break;

                    case "hoten":
                        colname = "Họ Tên";
                        break;

                    case "quyenso":
                        colname = "Quyển sổ";
                        break;

                    case "sobienlai":
                        colname = "Số biên lai";
                        break;

                    case "tiendong":
                        colname = "Tạm ứng";
                        break;

                    case "ngaydong":
                        colname = "Ngày đóng";
                        break;

                    case "hoantra":
                        colname = "Hoàn trả";
                        break;

                    case "ngaytra":
                        colname = "Ngày trả";
                        break;

                    case "done":
                        colname = "Hoàn tất";
                        break;

                    case "tongvienphi":
                        colname = "Viện phí";
                        break;

                    case "ngayravien":
                        colname = "Ngày ra viện";
                        break;

                    case "nguoithu":
                        colname = "Người thu";
                        break;

                    case "tenkp":
                        colname = "Khoa";
                        break;

                    default:
                        colname = dc.ColumnName;
                        break;
                    }
                    ((Excel.Range)wks.Cells[offsetrow - 1, colnum]).Value2 = colname;
                    colnum++;
                }
                long numrow = 1;
                foreach (DataRow dr in datasetDanhsachtontamung1.CHITIETTAMUNG.Rows)
                {
                    colnum = 2;
                    foreach (DataColumn dc in datasetDanhsachtontamung1.CHITIETTAMUNG.Columns)
                    {
                        numrow = datasetDanhsachtontamung1.CHITIETTAMUNG.Rows.IndexOf(dr) + offsetrow;
                        ((Excel.Range)wks.Cells[numrow, 1]).Value2 = numrow - offsetrow + 1;
                        if (dc.ColumnName == "maql" || dc.ColumnName == "mavaovien")
                        {
                            continue;
                        }
                        if (dr[dc].GetType() == typeof(DateTime))
                        {
                            ((Excel.Range)wks.Cells[numrow, colnum]).Value2 = ((DateTime)dr[dc]).ToString("dd/MM/yyyy");
                        }
                        else
                        if (dr[dc].GetType() == typeof(decimal) && ((decimal)dr[dc]) > 1000000000000)
                        {
                            ((Excel.Range)wks.Cells[numrow, colnum]).Value2 = "'" + dr[dc].ToString();
                        }
                        else
                        {
                            if (dc.ColumnName == "done")
                            {
                                if ((decimal)dr[dc] == 0)
                                {
                                    ((Excel.Range)wks.Cells[numrow, colnum]).Value2 = "NO";
                                }
                                else
                                {
                                    ((Excel.Range)wks.Cells[numrow, colnum]).Value2 = "YES";
                                }
                            }
                            else
                            {
                                ((Excel.Range)wks.Cells[numrow, colnum]).Value2 = dr[dc];
                            }
                        }
                        colnum++;
                    }
                    int per = (int)((float)(numrow - offsetrow) / (float)datasetDanhsachtontamung1.CHITIETTAMUNG.Rows.Count * 90);
                    if (per % 5 == 0)
                    {
                        bgW_luuexel.ReportProgress(per);
                    }
                }
                ((Excel.Range)wks.Cells[numrow + 2, 1]).Value2 = "Tổng tạm ứng:"; ((Excel.Range)wks.Cells[numrow + 2, 2]).Value2 = lb_tongcong.Text;
                ((Excel.Range)wks.Cells[numrow + 2, 4]).Value2 = "Tổng tồn:"; ((Excel.Range)wks.Cells[numrow + 2, 5]).Value2 = lb_tongtu.Text;
                ((Excel.Range)wks.Cells[numrow + 2, 7]).Value2 = "Tổng hoàn:"; ((Excel.Range)wks.Cells[numrow + 2, 8]).Value2 = lb_tonghoan.Text;
                wkb.SaveAs(filename.Substring(0, filename.Length - 4), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                bgW_luuexel.ReportProgress(100);
                exapp.Quit();
                kq.err      = true;
                kq.filename = wkb.Path;
            }
            catch
            {
                kq.err = false;
            }
            finally
            {
                //if(exapp)
                exapp.Quit();
                Thread.CurrentThread.CurrentCulture = oldcul;
                e.Result = kq;
            }
        }