コード例 #1
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();

            fdlg.Title            = "Оберіть файл";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter           = "Excel files (*.xls*)|*.xls*|Excel files (*.xls*)|*.xls*";
            fdlg.FilterIndex      = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                addProductsExcelFile = fdlg.FileName;
            }
            Console.WriteLine(addProductsExcelFile);

            Microsoft.Office.Interop.Excel.Application xlApp     = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook    excelBook = xlApp.Workbooks.Open(addProductsExcelFile);

            String[] excelSheets = new String[excelBook.Worksheets.Count];
            int      i           = 0;

            foreach (Microsoft.Office.Interop.Excel.Worksheet wSheet in excelBook.Worksheets)
            {
                excelSheets[i] = wSheet.Name;
                i++;
            }

            DataTable excelDataTable = ExcelToDataTable(addProductsExcelFile, excelSheets[0]);

            string[] columnNames = excelDataTable.Columns.Cast <DataColumn>()
                                   .Select(x => x.ColumnName)
                                   .ToArray();

            WorkWithDatabase workWithDatabase = new WorkWithDatabase();

            if (workWithDatabase.isConnection())
            {
                foreach (DataRow row in excelDataTable.Rows)
                {
                    string code = row[columnNames[0]].ToString();

                    string name = row[columnNames[1]].ToString();

                    string amount = row[columnNames[2]].ToString();

                    string size         = "";
                    string manufacturer = "";
                    string composition  = "";
                    string additional   = "";

                    for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                    {
                        if (dataGridView1.Rows[rows].Cells[1].Value.ToString().Equals(name))
                        {
                            //for (int col = 0; col < dataGridView1.Rows[rows].Cells.Count; col++)
                            //{
                            size = dataGridView1.Rows[rows].Cells[2].Value.ToString();

                            manufacturer = dataGridView1.Rows[rows].Cells[3].Value.ToString();

                            composition = dataGridView1.Rows[rows].Cells[4].Value.ToString();

                            additional = dataGridView1.Rows[rows].Cells[5].Value.ToString();
                            //}
                        }
                    }

                    printMark(name, size, manufacturer, composition, additional, amount, code);
                }
            }
            else
            {
                MessageBox.Show("Disconnected from database");
            }

            if (MessageBox.Show("Починаю друкувати. Вставте етикетки розміром 3х5", "Друк", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                foreach (string stringToPrint in arrayOne)
                {
                    Print(stringToPrint);
                }
            }

            Thread.Sleep(2000);

            if (MessageBox.Show("Починаю друкувати. Вставте етикетки розміром 4х5", "Друк", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                foreach (string stringToPrint in arrayTwo)
                {
                    Print(stringToPrint);
                }
            }

            Thread.Sleep(2000);

            if (MessageBox.Show("Починаю друкувати. Вставте етикетки розміром 5х5", "Друк", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                foreach (string stringToPrint in arrayThree)
                {
                    Print(stringToPrint);
                }
            }
        }
コード例 #2
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();

            fdlg.Title            = "Оберіть файл";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter           = "Excel files (*.xls*)|*.xls*|Excel files (*.xls*)|*.xls*";
            fdlg.FilterIndex      = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                addProductsExcelFile = fdlg.FileName;
            }
            Console.WriteLine(addProductsExcelFile);

            Microsoft.Office.Interop.Excel.Application xlApp     = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook    excelBook = xlApp.Workbooks.Open(addProductsExcelFile);

            String[] excelSheets = new String[excelBook.Worksheets.Count];
            int      i           = 0;

            foreach (Microsoft.Office.Interop.Excel.Worksheet wSheet in excelBook.Worksheets)
            {
                excelSheets[i] = wSheet.Name;
                i++;
            }

            DataTable excelDataTable = ExcelToDataTable(addProductsExcelFile, excelSheets[0]);

            string[] columnNames = excelDataTable.Columns.Cast <DataColumn>()
                                   .Select(x => x.ColumnName)
                                   .ToArray();

            WorkWithDatabase workWithDatabase = new WorkWithDatabase();

            if (workWithDatabase.isConnection())
            {
                foreach (DataRow row in excelDataTable.Rows)
                {
                    //назва, товару, розмір етикетки, виробник, склад, додаткова інформація
                    string name = row[columnNames[0]].ToString();
                    Console.Write(name);

                    string size = row[columnNames[1]].ToString();
                    Console.Write(" " + size);

                    string manufacturer = row[columnNames[2]].ToString();
                    Console.Write(" " + manufacturer);

                    string consistance = row[columnNames[3]].ToString();
                    Console.Write(" " + consistance);

                    string additional = row[columnNames[4]].ToString();
                    Console.WriteLine(" " + additional);

                    workWithDatabase.insertData(name, size, manufacturer, consistance, additional);
                }
            }
            else
            {
                MessageBox.Show("Disconnected from database");
            }

            //this.Hide();

            //Form1 form = new Form1();
            //form.ShowDialog();
            //this.Close();

            DataTable dt = workWithDatabase.fillDataTable();

            BindingSource bs = goodsBindingSource;

            bs.DataSource = dt;

            dataGridView1.DataSource = bs;
        }