예제 #1
0
        //Кнопка меню "Печать лимитной карты"
        private void печатьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = Лимитка;

            if (dataGridView3.RowCount == 0)
            {
                MessageBox.Show("Лимитная карта пуста", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string    CountOfLimitCard = "1";
            DataTable dataTable1       = new DataTable();

            object oMissing = System.Reflection.Missing.Value;

            Word.Documents WordDocuments;
            Word.Document  WordDocument;

            try
            {
                //Создаем объект Word - равносильно запуску Word
                WordApp = new Word.Application();
                //Делаем его видимым
                WordApp.Visible = true;

                Object template     = Type.Missing;
                Object newTemplate  = false;
                Object documentType = Word.WdNewDocumentType.wdNewBlankDocument;
                Object visible      = true;

                Object defaultTableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
                Object autoFitBehavior      = Word.WdAutoFitBehavior.wdAutoFitWindow;

                WordDocument = WordApp.Documents.Add(
                    ref template, ref newTemplate, ref documentType, ref visible);

                string ProductName, LimitCardName = "", LimitCardDate = "";
                ProductName = comboBox1.SelectedItem.ToString();
                ProductName = ProductName.Remove(0, ProductName.IndexOf(" ") + 2);
                ProductName = ProductName.Remove(ProductName.IndexOf("[") - 1);

                dataTable1.Clear();

                dbAdapter = new OleDbDataAdapter(
                    @"SELECT Лимитки.Объем, Лимитки.НомерЛимитки, Лимитки.ДатаСоставления 
                        FROM  Лимитки 
                        WHERE Лимитки.КодЛимитки = " + dataGridView2[0, dataGridView2.CurrentRow.Index].Value.ToString() /*[0, i - 2].Value.ToString()*/ + " ;",
                    dbCon);
                dbAdapter.Fill(dataTable1);

                foreach (DataRow Row in dataTable1.Rows)
                {
                    CountOfLimitCard = Row[0].ToString();
                    LimitCardName    = Row[1].ToString();
                    LimitCardDate    = Row[2].ToString();
                }
                LimitCardDate = LimitCardDate.Remove(LimitCardDate.IndexOf(" "));
                LimitCardName = LimitCardName.Replace("/", ".");
                LimitCardName = LimitCardName.Replace("\\", ".");
                LimitCardName = LimitCardName.Replace("|", ".");
                LimitCardName = LimitCardName.Replace("?", ".");
                LimitCardName = LimitCardName.Replace(":", ".");
                LimitCardName = LimitCardName.Replace("*", ".");
                LimitCardName = LimitCardName.Replace("<", ".");
                LimitCardName = LimitCardName.Replace(">", ".");
                LimitCardName = LimitCardName.Replace("\"", "'");

                ProductName = ProductName.Replace("/", ".");
                ProductName = ProductName.Replace("\\", ".");
                ProductName = ProductName.Replace("|", ".");
                ProductName = ProductName.Replace("?", ".");
                ProductName = ProductName.Replace(":", ".");
                ProductName = ProductName.Replace("*", ".");
                ProductName = ProductName.Replace("<", ".");
                ProductName = ProductName.Replace(">", ".");
                ProductName = ProductName.Replace("\"", "'");

                string FN       = ProductName.ToString() + @" лим " + LimitCardName.ToString() + @" " + CountOfLimitCard.ToString() + @".docx";
                object fileName = FN;
                WordDocument.SaveAs(ref fileName, 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);

                WordApp.Selection.TypeParagraph();
                //wordrange.Font.Color = Word.WdColor.wdColorRed;
                WordApp.Selection.Font.Name = "Times New Roman";
                WordApp.Selection.Font.Size = 14;
                WordApp.Selection.Font.Bold = 1;
                WordApp.Selection.TypeText(ProductName + " Лимитка " + LimitCardName + " " + CountOfLimitCard + " - шт. от " + LimitCardDate);
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.Font.Size = 12;
                WordApp.Selection.Font.Bold = 0;
                WordApp.Selection.TypeText("Лимитная ведомость");
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.Font.Size = 10;

                int rowCount    = dataGridView3.RowCount,
                    NumRowCount = 1;
                bool CycleFlag  = true;

                //Добавляем таблицу и получаем объект wordtable
                Word.Table WordTable = WordDocument.Tables.Add(WordApp.Selection.Range, 1 /*rowCount*/, 8,
                                                               ref defaultTableBehavior, ref autoFitBehavior);

                WordApp.Selection.PageSetup.Orientation            = Word.WdOrientation.wdOrientLandscape;
                WordApp.Application.Selection.PageSetup.LeftMargin = 20f;

                //Форматирование документа
                //
                //Настройка ширины столбцов таблицы

                /*
                 * ПЕРЕНЕСЕНО ПОСЛЕ ЗАПОЛНЕНИЯ ТАБЛИЦЫ ДАННЫМИ
                 * WordTable.Columns[1].SetWidth(15.9f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[2].SetWidth(142.2f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[3].SetWidth(233.9f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[4].SetWidth(108, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[5].SetWidth(54, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[6].SetWidth(39, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[7].SetWidth(108, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 * WordTable.Columns[8].SetWidth(83.9f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                 */

                //dbCon.Open();

                Word.Range WordCellRange;

                //WordCellRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

                //Заполнение "шапки" таблицы
                WordCellRange      = WordTable.Cell(1, 1).Range;
                WordCellRange.Text = "№";

                WordCellRange      = WordTable.Cell(1, 2).Range;
                WordCellRange.Text = "Наименование";

                WordCellRange      = WordTable.Cell(1, 3).Range;
                WordCellRange.Text = "Обозначение";

                WordCellRange      = WordTable.Cell(1, 4).Range;
                WordCellRange.Text = "Допустимая замена";

                WordCellRange      = WordTable.Cell(1, 5).Range;
                WordCellRange.Text = "Прим.";

                WordCellRange      = WordTable.Cell(1, 6).Range;
                WordCellRange.Text = "*" + CountOfLimitCard;

                WordCellRange      = WordTable.Cell(1, 7).Range;
                WordCellRange.Text = "Кол/цена";

                WordCellRange      = WordTable.Cell(1, 8).Range;
                WordCellRange.Text = "Дата";
                //
                //
                //WordTable.Rows[1].Range.Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth225pt;

                //Заполнение таблицы данными
                while (CycleFlag)
                {
                    WordTable.Rows.Add(ref oMissing);//Добавление новой строки в таблицу

                    //WordTable.Rows[WordTable.Rows.Count].Range.Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth225pt;

                    WordCellRange      = WordTable.Cell(WordTable.Rows.Count, 1).Range;
                    WordCellRange.Text = NumRowCount.ToString();

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 2).Range;
                    //Наименование
                    WordCellRange.Text = dataGridView3[2, NumRowCount - 1].Value.ToString();

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 3).Range;
                    //Обозначение
                    WordCellRange.Text = dataGridView3[1, NumRowCount - 1].Value.ToString();

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 4).Range;
                    //Допустимая замена
                    //WordCellRange.Text = Change;

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 5).Range;
                    //WordCellRange.Text = "Прим.";
                    WordCellRange.Text = dataGridView3[3, NumRowCount - 1].Value.ToString();

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 6).Range;
                    //WordCellRange.Text = "*";
                    WordCellRange.Text = dataGridView3[4, NumRowCount - 1].Value.ToString();

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 7).Range;
                    //WordCellRange.Text = "Кол/цена";

                    WordCellRange = WordTable.Cell(WordTable.Rows.Count, 8).Range;
                    //WordCellRange.Text = "Дата";

                    dataTable1 = new DataTable();
                    //dataTable1.Clear();

                    dbAdapter = new OleDbDataAdapter(
                        @"SELECT СоставСборки.Элемент, СоставСборки.Объем, ROUND(СоставСборки.Объем * " + CountOfLimitCard + @",2) AS Объем 
                        FROM Элементы INNER JOIN СоставСборки ON Элементы.КодЭлемента = СоставСборки.КодЭлемента 
                        WHERE Элементы.КодЭлемента = " + dataGridView3[0, NumRowCount - 1].Value.ToString() + ";", dbCon);

                    dbAdapter.Fill(dataTable1);

                    foreach (DataRow Row in dataTable1.Rows)
                    {
                        DataTable dataTable2 = new DataTable();
                        dbAdapter = new OleDbDataAdapter(
                            @"SELECT Элементы.ОбозначениепоКД, ТипыЭлементов.Название
                        FROM ТипыЭлементов INNER JOIN Элементы ON ТипыЭлементов.КодТипа = Элементы.КодТипа 
                        WHERE Элементы.КодЭлемента = " + Row[0].ToString() + ";", dbCon);
                        dbAdapter.Fill(dataTable2);

                        foreach (DataRow Row2 in dataTable2.Rows)
                        {
                            WordTable.Rows.Add(ref oMissing);//Добавление новой строки в таблицу

                            WordCellRange = WordTable.Cell(WordTable.Rows.Count, 2).Range;
                            //Наименование
                            WordCellRange.Text = Row2[1].ToString();

                            WordCellRange = WordTable.Cell(WordTable.Rows.Count, 3).Range;
                            //Обозначение
                            WordCellRange.Text = Row2[0].ToString();

                            WordCellRange = WordTable.Cell(WordTable.Rows.Count, 5).Range;
                            //WordCellRange.Text = "Прим.";
                            WordCellRange.Text = Row[1].ToString();

                            WordCellRange = WordTable.Cell(WordTable.Rows.Count, 6).Range;
                            //WordCellRange.Text = "*";
                            WordCellRange.Text = Row[2].ToString();
                        }
                    }

                    NumRowCount++;
                    if (NumRowCount == rowCount + 1)//Проверка на выход из цикла
                    {
                        CycleFlag = false;
                    }
                }


                //Настройка ширины столбцов таблицы
                WordTable.Columns[1].SetWidth(15.9f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[2].SetWidth(142.2f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[3].SetWidth(233.9f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[4].SetWidth(108, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[5].SetWidth(54, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[6].SetWidth(39, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[7].SetWidth(108, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                WordTable.Columns[8].SetWidth(83.9f, Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);

                object rStart = 0;
                object rEnd   = WordDocument.Content.End;
                WordDocument.Range(ref rStart, ref rEnd).InsertAfter("\n0 - расход материалов – “по потребности”");

                //dbCon.Close();
            }
            catch (Exception exception)
            {
                //MessageBox.Show("Невозможно создать документ Word", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(exception.Message + "\nМетод:\n" + exception.TargetSite.ToString(), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }