/// <summary>
        /// This method was called from the host instance after selection changed.
        /// </summary>
        /// <param name="selectedRange">active selection</param>
        private void Application_SheetSelectionChangeEvent(NetOffice.COMObject sh, Excel.Range selectedRange)
        {
            try
            {
                // we check for auto translation and skip selections with more than 256 cells for this simple example
                if (checkBoxAutoTranslate.Checked && selectedRange.Count <= 256)
                {
                    ClearError();
                    textBoxTranslation.Text = string.Empty;
                    foreach (var item in selectedRange)
                    {
                        string requestedText = item.Text as string;
                        if (!String.IsNullOrWhiteSpace(requestedText))
                        {
                            string translatedText = Client.DataService.Translate(
                                comboBoxSourceLanguage.SelectedItem as string,
                                comboBoxTargetLanguage.SelectedItem as string,
                                requestedText);
                            textBoxTranslation.Text += translatedText + " ";
                        }
                    }
                }

                sh.Dispose();
                selectedRange.Dispose();
            }
            catch (Exception exception)
            {
                ShowError(string.Format("An errror occured. Details: {0}", exception.Message));
            }
        }
Example #2
0
        private void listViewSearchResults_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (listViewSearchResults.SelectedItems.Count > 0)
                {
                    Excel.Worksheet activeSheet = Addin.Application.ActiveSheet as Excel.Worksheet;
                    Excel.Range     activeCell  = Addin.Application.ActiveCell;
                    if (null != activeCell)
                    {
                        int rowIndex    = activeCell.Row;
                        int columnIndex = activeCell.Column;

                        string targetRangeAddress = CalculateRangeArea(rowIndex, columnIndex, 7);

                        Customer selectedCustomer = listViewSearchResults.SelectedItems[0].Tag as Customer;

                        Excel.Range targetRange = activeSheet.Range(targetRangeAddress);
                        targetRange.Value2 = ToStringArray(selectedCustomer);
                        targetRange.HorizontalAlignment = XlHAlign.xlHAlignLeft;
                        activeSheet.Columns[targetRange.Column].AutoFit();

                        activeCell.Dispose();
                        activeSheet.Dispose();
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(this, exception.Message, "An error occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Write 1 million cells in excel.");

            NetOffice.Settings.Default.PerformanceTrace.Alert += new NetOffice.PerformanceTrace.PerformanceAlertEventHandler(PerformanceTrace_Alert);
            NetOffice.Settings.Default.PerformanceTrace["ExcelApi"].Enabled    = true;
            NetOffice.Settings.Default.PerformanceTrace["ExcelApi"].IntervalMS = 20;

            Excel.Application application = new Excel.ApplicationClass();

            application.DisplayAlerts  = false;
            application.Interactive    = false;
            application.ScreenUpdating = false;

            application.Workbooks.Add();

            Excel.Worksheet workSheet  = (Excel.Worksheet)application.Workbooks[1].Worksheets[1];
            Excel.Range     rangeCells = workSheet.Cells;

            // row
            int      counter   = 0;
            DateTime startTime = DateTime.Now;

            for (int i = 1; i <= 10000; i++)
            {
                // column
                for (int y = 1; y <= 100; y++)
                {
                    Excel.Range range = rangeCells[i, y];
                    range.Value = "TestValue";
                    range.Dispose();
                    counter++;
                }
                if (i % 100 == 0)
                {
                    Console.WriteLine("{0} Cells written. Time elapsed: {1}", counter, DateTime.Now - startTime);
                }
            }

            // quit and dispose
            application.Quit();
            application.Dispose();

            Console.WriteLine("Done!");
        }