Пример #1
0
        private void exportAsDocxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewDocumentSearchResult.SelectedRows;

            if (selectedRows.Count > 0)
            {
                try
                {
                    SaveFileDialog saveFile = new SaveFileDialog()
                    {
                        FileName   = "FileFromSearch.docx",
                        Filter     = "Document | *.docx",
                        DefaultExt = "docx"
                    };

                    if (DialogResult.OK == saveFile.ShowDialog())
                    {
                        string saveFileName = saveFile.FileName;
                        if (!Path.GetExtension(saveFile.FileName).Equals("." + saveFile.DefaultExt, StringComparison.InvariantCultureIgnoreCase))
                        {
                            saveFileName = saveFile.FileName + "." + saveFile.DefaultExt;
                        }
                        using (var fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write))
                        {
                            XWPFDocument newDocument = new XWPFDocument();

                            for (int i = selectedRows.Count - 1; i >= 0; i--)
                            {
                                object fileId = selectedRows[i].Cells["ColumnID"].Value;


                                string query = @"SELECT DI.Title, DI.Body, DI.CreatedDate AS EnglishDate, AI.Name as Author
                                FROM tbl_DocumentInfo DI WITH (NOLOCK),
	                                tbl_AuthorInfo AI WITH(NOLOCK) 
                                WHERE DI.Author = AI.Id
									AND DI.Id = @Id"                                    ;

                                using (SqlCommand command = new SqlCommand(query))
                                {
                                    command.Parameters.Add("@Id", SqlDbType.BigInt).Value = fileId;
                                    DataTable dt = DBSupport.ExecuteQueryAndGetDataTable(command);
                                    if (dt != null)
                                    {
                                        foreach (DataRow dr in dt.Rows)
                                        {
                                            DateTime       engDate    = DateTime.Parse(dr["EnglishDate"].ToString());
                                            NepaliDateTime nepaliDate = DateConverter.EnglishToNepali(engDate);
                                            AppendInfoToDocument(newDocument, dr["Title"].ToString(), dr["Author"].ToString(), dr["Body"].ToString()
                                                                 , nepaliDate.ToString());
                                        }
                                    }
                                }
                            }

                            newDocument.Write(fs);
                        }
                        ShowMessageBox("Document created successfully !");
                    }
                }
                catch (Exception ex)
                {
                    ShowMessageBox("Could not save file. Error : " + ex.Message, MessageBoxIcon.Error);
                }
            }
            else
            {
            }
        }
Пример #2
0
        private void exportSelectedFilesAsSeparateDocxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewDocumentSearchResult.SelectedRows;

            if (selectedRows.Count > 0)
            {
                int progress   = 0;
                int totalCount = selectedRows.Count;
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                try
                {
                    if (DialogResult.OK == folderBrowserDialog.ShowDialog())
                    {
                        progressBarExport.Show();
                        string today        = NepaliDateTime.Now.ToDateTimeString();
                        string saveFileName = string.Empty;
                        for (int i = selectedRows.Count - 1; i >= 0; i--)
                        {
                            try
                            {
                                object fileId = selectedRows[i].Cells["ColumnID"].Value;

                                string query = @"SELECT DI.Title, DI.Body, DI.CreatedDate AS EnglishDate, AI.Name as Author
                                FROM tbl_DocumentInfo DI WITH (NOLOCK),
	                                tbl_AuthorInfo AI WITH(NOLOCK) 
                                WHERE DI.Author = AI.Id
									AND DI.Id = @Id"                                    ;

                                using (SqlCommand command = new SqlCommand(query))
                                {
                                    command.Parameters.Add("@Id", SqlDbType.BigInt).Value = fileId;
                                    DataTable dt = DBSupport.ExecuteQueryAndGetDataTable(command);
                                    if (dt != null)
                                    {
                                        string folderPath = Path.Combine(folderBrowserDialog.SelectedPath, today);
                                        if (!Directory.Exists(folderPath))
                                        {
                                            Directory.CreateDirectory(folderPath);
                                        }
                                        DateTime       engDate    = DateTime.Parse(dt.Rows[0]["EnglishDate"].ToString());
                                        NepaliDateTime nepaliDate = DateConverter.EnglishToNepali(engDate);
                                        string         filename   = nepaliDate.ToString() + "_" + dt.Rows[0]["Title"].ToString() + "_" + Guid.NewGuid().ToString().Replace("-", "");
                                        filename     = string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));
                                        saveFileName = Path.Combine(folderPath, filename + ".docx");
                                        using (var fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write))
                                        {
                                            XWPFDocument newDocument = new XWPFDocument();
                                            AppendInfoToDocument(newDocument, dt.Rows[0]["Title"].ToString(), dt.Rows[0]["Author"].ToString()
                                                                 , dt.Rows[0]["Body"].ToString(), nepaliDate.ToString());
                                            newDocument.Write(fs);
                                        }
                                    }
                                }
                                progressBarExport.Value = ++progress * 100 / totalCount;
                                progressBarExport.Update();
                            }
                            catch (Exception ex)
                            {
                                ShowMessageBox("Could not save file: " + saveFileName + Environment.NewLine + " Error : " + ex.Message
                                               , MessageBoxIcon.Error);
                            }
                        }
                        ShowMessageBox("Documents created successfully !");
                    }
                }
                catch (Exception ex)
                {
                    ShowMessageBox("Could not save files." + Environment.NewLine + " Error : " + ex.Message
                                   , MessageBoxIcon.Error);
                }
                finally
                {
                    progressBarExport.Value = 0;
                    progressBarExport.Hide();
                }
            }
        }
Пример #3
0
        private void Search()
        {
            long publisher    = (long)comboBoxPublisherScope.SelectedValue;
            long documentType = (long)comboBoxDocumentTypeScope.SelectedValue;
            long author       = (long)comboBoxSearchAuthor.SelectedValue;

            string whereClause = string.Empty;

            if (publisher != 0)
            {
                whereClause = " Publisher = " + publisher;
            }

            if (documentType != 0)
            {
                if (!string.IsNullOrWhiteSpace(whereClause))
                {
                    whereClause += " AND DocumentType =" + documentType;
                }
                else
                {
                    whereClause = " DocumentType = " + documentType;
                }
            }
            if (author != 0)
            {
                if (!string.IsNullOrWhiteSpace(whereClause))
                {
                    whereClause += " AND Author =" + author;
                }
                else
                {
                    whereClause = " Author = " + author;
                }
            }
            highlightTerms = new List <string>();

            if (checkBoxSearchTitle.Checked || checkBoxSearchSummary.Checked || checkBoxSearchBody.Checked)
            {
                string searchText   = GetSearchText(ref highlightTerms);
                string stringClause = string.Empty;
                if (checkBoxSearchTitle.Checked)
                {
                    stringClause = " CONTAINS(Title, N'" + searchText + "') ";
                }

                if (checkBoxSearchSummary.Checked)
                {
                    if (string.IsNullOrWhiteSpace(stringClause))
                    {
                        stringClause = " CONTAINS(Summary,  N'" + searchText + "') ";
                    }
                    else
                    {
                        stringClause += " OR CONTAINS(Summary, N'" + searchText + "') ";
                    }
                }

                if (checkBoxSearchBody.Checked)
                {
                    if (string.IsNullOrWhiteSpace(stringClause))
                    {
                        stringClause = " CONTAINS(Body, N'" + searchText + "') ";
                    }
                    else
                    {
                        stringClause += " OR CONTAINS(Body, N'" + searchText + "') ";
                    }
                }

                if (string.IsNullOrWhiteSpace(whereClause))
                {
                    whereClause = stringClause;
                }
                else if (!string.IsNullOrWhiteSpace(stringClause))
                {
                    whereClause += " AND (" + stringClause + ")";
                }
            }
            if (!string.IsNullOrWhiteSpace(whereClause))
            {
                whereClause = " WHERE " + whereClause;
            }

            string query = @"SELECT ROW_NUMBER() OVER (ORDER BY DI.CreatedDate DESC, DI.Id DESC) AS SN, DI.Id, AI.Name AS Author, Title, DT.Name as DocumentType,  '' AS NepaliDate, CreatedDate AS EnglishDate, DateUncertain
                                FROM tbl_DocumentInfo DI WITH (NOLOCK)
	                                 LEFT JOIN tbl_PublisherInfo PI WITH (NOLOCK) ON DI.Publisher = PI.Id
	                                 LEFT JOIN tbl_DocumentType DT WITH (NOLOCK) ON DI.DocumentType = DT.Id
	                                 LEFT JOIN tbl_AuthorInfo AI WITH (NOLOCK) ON AI.Id = DI.Author
                                 " + whereClause;

            DataTable dt = DBSupport.GetDataTable(query);

            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DateTime       engDate     = DateTime.Parse(dr["EnglishDate"].ToString());
                    NepaliDateTime nepaliDate  = DateConverter.EnglishToNepali(engDate);
                    bool           isUncertain = bool.Parse(dr["DateUncertain"].ToString());
                    dr["NepaliDate"] = nepaliDate.ToString() + (isUncertain ? " *" : "");
                }
                labelResultCount.Text = dt.Rows.Count.ToString();
            }
            else
            {
                labelResultCount.Text = "0";
            }

            dataGridViewDocumentSearchResult.DataSource = dt;
            dataGridViewDocumentSearchResult.AutoResizeColumns();
        }