Пример #1
0
        private void Browse_Source()
        {
            // reset the status label
            labelStatus.Text = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ExcelBibliographicReader read = new ExcelBibliographicReader();
                List <string>            tables;

                try
                {
                    // Set form controls to default state
                    ResetFormControls();

                    // Write the filename to the text box first
                    fileTextBox.Text = openFileDialog1.FileName;
                    filename         = openFileDialog1.FileName;


                    // Try getting the worksheet names from the selected workbook
                    bool readFlag = true;

                    if ((true) || (filename.ToUpper().IndexOf(".XLS") > 0))
                    {
                        while (readFlag)
                        {
                            try
                            {
                                // Get the sheet names
                                read   = new ExcelBibliographicReader();
                                tables = read.GetExcelSheetNames(openFileDialog1.FileName);


                                if (tables == null)
                                {
                                    ResetFormControls();
                                    return;
                                }
                                else
                                {
                                    readFlag = false;

                                    // Populate the combo box
                                    sheetComboBox.Enabled = true;
                                    foreach (string thisSheetName in tables)
                                    {
                                        sheetComboBox.Items.Add(thisSheetName);
                                    }
                                    sheetComboBox.Enabled = true;
                                    sheetLabel.ForeColor  = SystemColors.WindowText;

                                    // show step 2 instructions
                                    show_step_2();
                                }
                            }
                            catch (Exception ex)
                            {
                                ErrorMessageBox.Show(ex.Message, "Unexpected Error", ex);
                            }
                        }
                    }
                    else
                    {
                        // show step 2 instructions
                        show_step_2();

                        sheetComboBox.Items.Clear();
                        sheetComboBox.Enabled = false;
                        sheetLabel.ForeColor  = SystemColors.GrayText;
                    }
                }
                catch (Exception ex)
                {
                    ErrorMessageBox.Show(ex.Message, "Unexpected Error", ex);
                }
                finally
                {
                    // Close the reader
                    read.Close();
                }
            }
            else
            {
                // reset the form
                ResetFormControls();

                // Move to STEP 1
                show_step_1();
            }
        }
Пример #2
0
        private void sheetComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExcelBibliographicReader read = new ExcelBibliographicReader();

            try
            {
                // Make sure there is a filename and a sheet name
                if ((fileTextBox.Text.Length > 0) && (sheetComboBox.SelectedIndex >= 0))
                {
                    // reset the status label
                    labelStatus.Text = "";

                    read.Sheet    = sheetComboBox.Text;
                    read.Filename = fileTextBox.Text;

                    // Declare constant fields
                    Constant_Fields constants = new Constant_Fields();

                    columnNamePanel.Controls.Clear();
                    column_map_inputs.Clear();

                    columnNamePanel.Enabled = true;
                    pnlConstants.Enabled    = true;

                    // Display an hourglass cursor:
                    Cursor = Cursors.WaitCursor;


                    // Try reading data from the selected Excel Worksheet
                    bool readFlag = true;

                    while (readFlag)
                    {
                        try
                        {
                            if (!read.Check_Source())
                            {
                                ResetFormControls();
                                return;
                            }
                            else
                            {
                                readFlag = false;
                            }
                        }
                        catch (Exception ex)
                        {
                            ErrorMessageBox.Show(ex.Message, "Unexpected Error", ex);
                        }
                    }


                    // change cursor back to default
                    Cursor = Cursors.Default;

                    excelDataTbl = read.excelData;

                    if (read.excelData.Rows.Count > 0 || read.excelData.Columns.Count > 0)
                    {
                        int column_counter = 1;
                        foreach (DataColumn thisColumn in excelDataTbl.Columns)
                        {
                            // Create the column mapping custom control
                            Column_Assignment_Control thisColControl = new Column_Assignment_Control();

                            // Get the column name
                            string thisColumnName = thisColumn.ColumnName;
                            thisColControl.Column_Name = thisColumnName;
                            if (thisColumnName == "F" + column_counter)
                            {
                                thisColControl.Empty = true;
                            }

                            thisColControl.Location = new Point(10, 10 + ((column_counter - 1) * 30));
                            columnNamePanel.Controls.Add(thisColControl);
                            column_map_inputs.Add(thisColControl);

                            // Select value in list control that matches to a Column Name
                            thisColControl.Select_List_Item(thisColumnName);

                            // Increment for the next column
                            column_counter++;
                        }

                        // Move to STEP 3
                        show_step_3();

                        if (column_map_inputs.Count > 0)
                        {
                            // Move to STEP 4
                            show_step_4();
                        }
                    }


                    // Close the reader
                    read.Close();
                }
            }
            catch (Exception ex)
            {
                ErrorMessageBox.Show(ex.Message, "Unexpected Error", ex);
            }
            finally
            {
                // change cursor back to default
                Cursor = Cursors.Default;

                // Close the reader
                read.Close();
            }
        }