Ejemplo n.º 1
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                WordDocument doc = new WordDocument(WordDocumentFormat.A4_Horizontal);

                Font timesBold = new Font("Times", 14, FontStyle.Bold);
                Font timesBigBold = new Font("Times", 18, FontStyle.Bold);
                Font timesRegular = new Font("Times", 14, FontStyle.Regular);
                doc.SetTextAlign(WordTextAlign.Center);
                doc.SetFont(timesBold);

                WordTable wt = doc.NewTable(timesRegular, Color.Black, ShowMe.Rows.Count + 1, ShowMe.Columns.Count, 0);
                foreach (WordCell rc in wt.Cells) rc.SetBorders(Color.Black, 1, true, true, true, true);
                wt.SetContentAlignment(ContentAlignment.TopCenter);
                wt.SetFont(timesBold);

                for (int i = 0; i < ShowMe.Columns.Count; i++)
                {
                    wt.Rows[0][i].WriteControlWord(Encode(ShowMe.Columns[i].Caption));
                }

                wt.SetFont(timesRegular);
                for (int row = 0; row < ShowMe.Rows.Count; row++)
                {
                    for (int col = 0; col < ShowMe.Columns.Count; col++)
                    {
                        string temp = Encode(ShowMe.Rows[row].ItemArray[col].ToString());
                        wt.Rows[row + 1][col].WriteControlWord(temp);
                    }
                }

                wt.SaveToDocument(14500, 0);
                doc.FooterStart();
                doc.SetTextAlign(WordTextAlign.Center);
                doc.SetFont(new Font("Verdana", 8, FontStyle.Regular));
                doc.Write("Copyright © 2010 by NIRS Project, KubSTU Dev Team");
                doc.FooterEnd();

                string pathToFile = txtSaveFileName.Text;

                try
                {
                    doc.SaveToFile(pathToFile);
                }
                catch (NullReferenceException ex)
                {
                    Logs.WriteLine(ex.ToString());
                    MessageBox.Show("Не удается записать в указанный файл");
                    return;
                }

                if (checkBoxOpenFileAfterExport.Checked)
                    System.Diagnostics.Process.Start(pathToFile);

                this.Close();
            }
            catch (Exception ex)
            {
                Logs.WriteLine(ex.ToString());
                MessageBox.Show(ex.Message);
            }
        }