private void ExportButton_Click(object sender, EventArgs e)
        {
            if (Utility.GetWordVersion() < 0)
            {
                MessageBox.Show("Word was not installed !");
                return;
            }

            WordExportSettingForm exportSettingForm = new WordExportSettingForm();
            if (exportSettingForm.ShowDialog() == DialogResult.Cancel)
                return;

            string reportTemplateFile;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Select report file name";

            if (exportSettingForm.ExportOffice2003)
            {
                reportTemplateFile = System.AppDomain.CurrentDomain.BaseDirectory
                 + "MalaysiaMaxisTemplate.doc";
                saveFileDialog.Filter = "report file(*.doc)|*.doc";
            }
            else
            {
                reportTemplateFile = System.AppDomain.CurrentDomain.BaseDirectory
                 + "MalaysiaMaxisTemplate.docx";
                saveFileDialog.Filter = "report file(*.docx)|*.docx";
            }

            do
            {
                if (DialogResult.Cancel == saveFileDialog.ShowDialog())
                    return;

                if (saveFileDialog.FileName.Equals(reportTemplateFile,
                    StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show("Can't select report template file !");
                    continue;
                }

                try
                {
                    File.Copy(reportTemplateFile, saveFileDialog.FileName, true);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save file " + saveFileDialog.FileName
                        + "!\r\n" + "Select another file name for report.");
                    continue;
                }

                mExportFileName = saveFileDialog.FileName;
                break;

            } while (true);

            Hide();
            mExportStatusForm.Show();
            mCancelExport = false;

            mExportThread = new Thread(delegate()
            {
                bool isReportSucceed = false;

                System.Globalization.CultureInfo Oldci = null;
                object oMissing = System.Reflection.Missing.Value;
                Word._Application app = null;
                Word._Document doc;
                try
                {
                    Oldci = System.Threading.Thread.CurrentThread.CurrentCulture;
                    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

                    app = new Word.Application();
                    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

                    UpdateStatus("Open word document ...");
                    object objFileName = mExportFileName;
                    doc = app.Documents.Open(ref objFileName,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                    UpdateStatus("Calculate graph table count ...");

                    //Remove picture page
                    int removePageCount = (8 - mAllBandChannels.Count) * 5;
                    for (int i = 0; i < removePageCount; i++)
                        RemovePage(doc, 32);

                    //Remove table page
                    removePageCount = 10 - mAllBandChannels.Count;
                    for (int i = 0; i < removePageCount; i++)
                        RemovePage(doc, 18);

                    int j = 0;
                    List<ChannelSetting> channelSettings;
                    ChannelPower channelPower;
                    foreach (KeyValuePair<string, List<ChannelSetting>> pair in mAllBandChannels)
                    {
                        channelSettings = pair.Value;

                        //Band Name
                        doc.Tables[19 + j * 2].Cell(1, 1).Range.Text = pair.Key;

                        //Frequency data table
                        for (int i = 0; i < channelSettings.Count; i++)
                        {
                            //low band
                            doc.Tables[20 + j * 2].Cell(3 + i, 1).Range.Text = (i + 1).ToString();
                            doc.Tables[20 + j * 2].Cell(3 + i, 2).Range.Text = channelSettings[i].CenterFreq.ToString();

                            //high band
                            doc.Tables[20 + j * 2].Cell(20 + i, 1).Range.Text = (i + 1).ToString();
                            doc.Tables[20 + j * 2].Cell(20 + i, 2).Range.Text = channelSettings[i].Pair.CenterFreq.ToString();

                            for (int k = 0; k < mAzimuths.Count; k++)
                            {
                                channelPower = new ChannelPower(mEmi.SA_RBW, channelSettings[i], mLimitSetting,
                                    mEmiDataMgr.AllChannelSamples[mAzimuths[k]][channelSettings[i]]);

                                //low band
                                doc.Tables[20 + j * 2].Cell(3 + i, 2 + k * 2 + 1).Range.Text
                                    = channelPower.IsValidVPower ? "" : "X";
                                doc.Tables[20 + j * 2].Cell(3 + i, 2 + k * 2 + 2).Range.Text
                                    = channelPower.IsValidHPower ? "" : "X";

                                //high band
                                doc.Tables[20 + j * 2].Cell(20 + i, 2 + k * 2 + 1).Range.Text
                                    = channelPower.IsValidVPairPower ? "" : "X";
                                doc.Tables[20 + j * 2].Cell(20 + i, 2 + k * 2 + 2).Range.Text
                                    = channelPower.IsValidHPairPower ? "" : "X";
                            }
                        }

                        j++;
                    }

                    //Graph table
                    object linkToFile = false;
                    object saveWithDocument = true;
                    object anchor; ;
                    Word.InlineShape shape;
                    int graphStartTableIndex = 18 + mAllBandChannels.Count * 2 + 2;
                    j = 0;
                    foreach (KeyValuePair<string, List<ChannelSetting>> pair in mAllBandChannels)
                    {
                        //band title
                        doc.Tables[graphStartTableIndex + j * 11].Cell(1, 1).Range.Text
                            = "FREQUENCY SCANNING SCREEN SHOT FOR " + pair.Key + " MDEF - LAXI";

                        for (int i = 0; i < 5; i++)
                        {
                            //vertical ...
                            //low band
                            doc.Tables[graphStartTableIndex + j * 11 + i * 2 + 1].Cell(1, 1).Select();
                            anchor = doc.Application.Selection.Range;
                            shape = doc.Application.ActiveDocument.InlineShapes.AddPicture(mBitmaps[i][pair.Key][0].BmpFile1,
                                ref linkToFile, ref saveWithDocument, ref anchor);
                            shape.Width = GRAPH_WIDTH;
                            shape.Height = GRAPH_HEIGHT;

                            //high band
                            doc.Tables[graphStartTableIndex + j * 11 + i * 2 + 1].Cell(1, 3).Select();
                            anchor = doc.Application.Selection.Range;
                            shape = doc.Application.ActiveDocument.InlineShapes.AddPicture(mBitmaps[i][pair.Key][1].BmpFile1,
                                ref linkToFile, ref saveWithDocument, ref anchor);
                            shape.Width = GRAPH_WIDTH;
                            shape.Height = GRAPH_HEIGHT;

                            //horizontal ...
                            //low band
                            doc.Tables[graphStartTableIndex + j * 11 + i * 2 + 2].Cell(1, 1).Select();
                            anchor = doc.Application.Selection.Range;
                            shape = doc.Application.ActiveDocument.InlineShapes.AddPicture(mBitmaps[i][pair.Key][0].BmpFile2,
                                ref linkToFile, ref saveWithDocument, ref anchor);
                            shape.Width = GRAPH_WIDTH;
                            shape.Height = GRAPH_HEIGHT;

                            //high band
                            doc.Tables[graphStartTableIndex + j * 11 + i * 2 + 2].Cell(1, 3).Select();
                            anchor = doc.Application.Selection.Range;
                            shape = doc.Application.ActiveDocument.InlineShapes.AddPicture(mBitmaps[i][pair.Key][1].BmpFile2,
                                ref linkToFile, ref saveWithDocument, ref anchor);
                            shape.Width = GRAPH_WIDTH;
                            shape.Height = GRAPH_HEIGHT;
                        }

                        j++;
                    }

                    isReportSucceed = true;
                    UpdateStatus("Save export file, please wait ...");
                    doc.Save();

                    UpdateStatus("Export succeed !");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Create report failed !\r\nException: " + ex.Message);
                }
                finally
                {
                    if (Oldci != null)
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture = Oldci;
                    }

                    if (app != null)
                        app.Quit(ref oMissing, ref oMissing, ref oMissing);
                }

                if (isReportSucceed)
                {
                    try
                    {
                        UpdateStatus("Open word ...");

                        Process process = Process.Start("winword", "\"" + mExportFileName + "\"");
                        process.Close();
                    }
                    catch (System.Exception ex)
                    {

                    }
                }

                UpdateStatus("Finished");
            }
            );

            mExportThread.Start();
        }
Beispiel #2
0
        private void ExportButton_Click(object sender, EventArgs e)
        {
            if (Utility.GetWordVersion() < 0)
            {
                MessageBox.Show("Word was not installed !");
                return;
            }

            WordExportSettingForm exportSettingForm = new WordExportSettingForm();
            if (exportSettingForm.ShowDialog() == DialogResult.Cancel)
                return;

            string reportTemplateFile;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Select report file name";

            if (exportSettingForm.ExportOffice2003)
            {
                reportTemplateFile = System.AppDomain.CurrentDomain.BaseDirectory
                 + "MalaysiaTemplate.doc";
                saveFileDialog.Filter = "report file(*.doc)|*.doc";
            }
            else
            {
                reportTemplateFile = System.AppDomain.CurrentDomain.BaseDirectory
                 + "MalaysiaTemplate.docx";
                saveFileDialog.Filter = "report file(*.docx)|*.docx";
            }

            do
            {
                if (DialogResult.Cancel == saveFileDialog.ShowDialog())
                    return;

                if (saveFileDialog.FileName.Equals(reportTemplateFile,
                    StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show("Can't select report template file !");
                    continue;
                }

                try
                {
                    File.Copy(reportTemplateFile, saveFileDialog.FileName, true);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Can't save file " + saveFileDialog.FileName
                        + "!\r\n" + "Select another file name for report.");
                    continue;
                }

                mExportFileName = saveFileDialog.FileName;
                break;

            } while (true);

            Hide();
            mExportStatusForm.Show();
            mCancelExport = false;

            mExportThread = new Thread(delegate()
            {
                bool isReportSucceed = false;

                System.Globalization.CultureInfo Oldci = null;
                object oMissing = System.Reflection.Missing.Value;
                Word._Application app = null;
                Word._Document doc;
                try
                {
                    Oldci = System.Threading.Thread.CurrentThread.CurrentCulture;
                    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

                    app = new Word.Application();
                    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

                    UpdateStatus("Open word document ...");
                    object objFileName = mExportFileName;
                    doc = app.Documents.Open(ref objFileName,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                    UpdateStatus("Calculate graph table count ...");

                    int graphTableCountA = (int)Math.Ceiling(mAzimuthsA.Count / 2.0);
                    int graphTableCountB = (int)Math.Ceiling(mAzimuthsB.Count / 2.0);
                    int removePageCount = 10 - graphTableCountB;
                    for (int i = 0; i < removePageCount; i++)
                        RemovePage(doc, 15);
                    removePageCount = 10 - graphTableCountA;
                    for (int i = 0; i < removePageCount; i++)
                        RemovePage(doc, 4);

                    UpdateStatus("Export Site A ...");
                    ExportSiteReport(0, doc, true);

                    int detailTableCountA = (int)Math.Ceiling(mAzimuthsA.Count / 6.0);
                    int offset = graphTableCountA + 1 + detailTableCountA;

                    UpdateStatus("Export Site B ...");
                    ExportSiteReport(offset, doc, false);

                    isReportSucceed = true;
                    UpdateStatus("Save export file, please wait ...");
                    doc.Save();

                    UpdateStatus("Export succeed !");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Create report failed !\r\nException: " + ex.Message);
                }
                finally
                {
                    if (Oldci != null)
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture = Oldci;
                    }

                    if (app != null)
                        app.Quit(ref oMissing, ref oMissing, ref oMissing);
                }

                if (isReportSucceed)
                {
                    try
                    {
                        UpdateStatus("Open word ...");

                        Process process = Process.Start("winword", "\"" + mExportFileName + "\"");
                        process.Close();
                    }
                    catch (System.Exception ex)
                    {

                    }
                }

                UpdateStatus("Finished");
            }
            );

            mExportThread.Start();
        }