Ejemplo n.º 1
0
        private void btnExportXls_Click(object sender, EventArgs e)
        {
            string absPath = Path.GetFullPath("./PassengerList.xls");

            FolderBrowserDialog sfd = new FolderBrowserDialog();

            sfd.SelectedPath = Properties.Settings.Default.ExportDestinationPath;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                Excel.Application excelApllication = null;
                Excel.Workbook    excelWorkBook    = null;
                Excel.Worksheet   excelWorkSheet   = null;
                excelApllication = new Excel.Application();
                System.Threading.Thread.Sleep(2000);
                string destDirPath = Directory.CreateDirectory(sfd.SelectedPath + "\\" + user.UserName).FullName;

                foreach (DataGridViewRow item in dgvShifts.SelectedRows)
                {
                    int         shiftID       = (int)item.Cells["colID"].Value;
                    RemoteShift selectedShift = ctx.RemoteShifts.Where(s => s.ID == shiftID).First <RemoteShift>();

                    excelWorkBook  = excelApllication.Workbooks.Open(absPath, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    excelWorkSheet = (Excel.Worksheet)excelWorkBook.Worksheets.get_Item(1);

                    int i = 1;
                    foreach (RemoteRequest r in selectedShift.RemoteRequests)
                    {
                        excelWorkSheet.Cells[i + 7, 8].Value  = r.FullName;
                        excelWorkSheet.Cells[i + 7, 7].Value  = r.PassportNum;
                        excelWorkSheet.Cells[i + 7, 6].Value2 = (byte)r.Gender == 0 ? "ذکر" : "انثی";

                        if (r.BornDate.HasValue)
                        {
                            excelWorkSheet.Cells[i + 7, 5].Value2 = r.BornDate;
                            excelWorkSheet.Cells[i + 7, 4].Value2 = r.IssueDate;
                            excelWorkSheet.Cells[i + 7, 3].Value2 = r.ExpiryDate;
                        } //if
                        i++;
                    }     //foreach

                    string fileName = selectedShift.Date.ToString("yyyy-MM-dd") + string.Format(" ({0:00})", selectedShift.ShiftNum);
                    excelWorkBook.SaveAs(destDirPath + "\\" + fileName, Excel.XlFileFormat.xlWorkbookNormal);
                    selectedShift.Exported = true;
                }//foreach

                excelWorkBook.Close();
                excelApllication.Quit();
                Marshal.FinalReleaseComObject(excelWorkSheet);
                Marshal.FinalReleaseComObject(excelWorkBook);
                Marshal.FinalReleaseComObject(excelApllication);
                excelApllication = null;
                excelWorkSheet   = null;

                //Opens the created Excel file
                Process.Start(destDirPath);
                ctx.SaveChanges();
            }//if
        }
Ejemplo n.º 2
0
        private void btnExportPDF_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog sfd = new FolderBrowserDialog();

            sfd.SelectedPath = Properties.Settings.Default.ExportDestinationPath;

            Boolean       xlsToo = MessageBox.Show("آیا مایلید فایل اکسل هم تولید شود؟", "تولید همزمان اکسل", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading) == DialogResult.Yes;
            List <string> files  = new List <string>();

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string destDirPath = Directory.CreateDirectory(sfd.SelectedPath + "\\" + user.UserName).FullName;

                foreach (DataGridViewRow row in dgvShifts.SelectedRows)
                {
                    string tempDirPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\VisaX";
                    Directory.CreateDirectory(tempDirPath);
                    foreach (string tempFile in Directory.GetFiles(tempDirPath))
                    {
                        File.Delete(tempFile);
                    }

                    int                  shiftID       = (int)row.Cells["colID"].Value;
                    RemoteShift          selectedShift = ctx.RemoteShifts.Where(s => s.ID == shiftID).First <RemoteShift>();
                    List <RemoteRequest> reqList       = selectedShift.RemoteRequests.ToList();

                    for (int i = 0; i < reqList.Count; i++)
                    {
                        files.Add(generatePdf(reqList[i], i + 1));
                    }

                    string fileName = destDirPath + "\\" + selectedShift.Date.ToString("yyyy-MM-dd") + string.Format(" ({0:00})", selectedShift.ShiftNum) + ".pdf";
                    MergePDFs(files, fileName);
                }//foreach
                Process.Start(destDirPath);
                if (xlsToo)
                {
                    btnExportXls_Click(null, null);
                }
                this.refreshGrid();
            } //if
        }     //btnExportPDF_Click