Exemple #1
0
        /*Создание отчёта в Word*/
        private void saveReportToWord(string fileName)
        {
            // Create a Wordprocessing document.
            using (WordprocessingDocument myDoc =
                   WordprocessingDocument.Create(fileName,
                                 WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
                mainPart.Document = new Document();
                Body body = new Body();

                Table resultTable = new Table();

                Table table = new Table();

                TableProperties tblPr = new TableProperties();
                TableBorders tblBorders = new TableBorders();
                tblBorders.TopBorder = new TopBorder();
                tblBorders.TopBorder.Val = BorderValues.Single;
                tblBorders.BottomBorder = new BottomBorder();
                tblBorders.BottomBorder.Val = BorderValues.Single;
                tblBorders.LeftBorder = new LeftBorder();
                tblBorders.LeftBorder.Val = BorderValues.Single;
                tblBorders.RightBorder = new RightBorder();
                tblBorders.RightBorder.Val = BorderValues.Single;
                tblBorders.InsideHorizontalBorder = new InsideHorizontalBorder();
                tblBorders.InsideHorizontalBorder.Val = BorderValues.Single;
                tblBorders.InsideVerticalBorder = new InsideVerticalBorder();
                tblBorders.InsideVerticalBorder.Val = BorderValues.Single;
                tblPr.Append(tblBorders);

                table.Append(tblPr);

                TableRow tr;
                TableCell tc;
                tr = new TableRow();

                body.Append(new Paragraph(new Run(new Text("Эксперимент: " + Experiment.Name.ToString() + "\n"))));
                body.Append(new Paragraph(new Run(new Text("Отклик: " + dgwExperiment.Columns[dgwExperiment.Columns.Count - 1].HeaderCell.Value.ToString() + "\n"))));
                if (dgwExperiment.Columns.Count == 2)
                {
                body.Append(new Paragraph(new Run(new Text("Фактор: "))));
                }
                else
                {
                body.Append(new Paragraph(new Run(new Text("Факторы: "))));
                }
                for (int col = 0; col < dgwExperiment.Columns.Count - 1; col++)
                {
                    body.Append(new Paragraph(new Run(new Text(dgwExperiment.Columns[col].HeaderCell.Value.ToString() + "\n"))));
                }

                body.Append(new Paragraph(new Run(new Text("\n\n"))));

                tc = new TableCell(new Paragraph(new Run(
                                   new Text("Экспериментальные данные"))));
                TableCellProperties tcp = new TableCellProperties();
                GridSpan gridSpan = new GridSpan();
                gridSpan.Val = 11;
                tcp.Append(gridSpan);
                tc.Append(tcp);
                tr.Append(tc);
                table.Append(tr);
                tr = new TableRow();
                tc = new TableCell();

                tc.Append(new Paragraph(new Run(new Text("*"))));
                tr.Append(tc);
                for (int i = 0; i < dgwExperiment.Columns.Count; i++)
                {
                    tr.Append(new TableCell(new Paragraph(new Run(new Text(dgwExperiment.Columns[i].HeaderCell.Value.ToString())))));
                }
                table.Append(tr);
                for (int i = 0; i < dgwExperiment.Rows.Count; i++)
                {
                    tr = new TableRow();
                    tr.Append(new TableCell(new Paragraph(new Run(new Text(dgwExperiment.Rows[i].HeaderCell.Value.ToString())))));
                    for (int j = 0; j < dgwExperiment.Columns.Count; j++)
                    {
                        tr.Append(new TableCell(new Paragraph(new Run(new Text(  dgwExperiment.Rows[i].Cells[j].Value.ToString())))));
                    }
                    table.Append(tr);
                }
                body.Append(table);

                body.Append(new Paragraph(new Run(new Text("\n\n"))));

                body.Append(new Paragraph(new Run(new Text("Результаты синтеза модели\n"))));
                for (int i = 0; i < txtCalcResult.Lines.Length; i++)
                {
                    body.Append(new Paragraph(new Run(new Text(txtCalcResult.Lines[i].ToString()))));
                }

                body.Append(new Paragraph(new Run(new Text("\n\n"))));

                /*
                TableRowHeight a = new TableRowHeight();
                a.HeightType = HeightRuleValues.Auto;
                */
                TableProperties tblPr2 = new TableProperties();
                TableBorders tblBorders2 = new TableBorders();
                tblBorders2.TopBorder = new TopBorder();
                tblBorders2.TopBorder.Val = BorderValues.Single;
                tblBorders2.BottomBorder = new BottomBorder();
                tblBorders2.BottomBorder.Val = BorderValues.Single;
                tblBorders2.LeftBorder = new LeftBorder();
                tblBorders2.LeftBorder.Val = BorderValues.Single;
                tblBorders2.RightBorder = new RightBorder();
                tblBorders2.RightBorder.Val = BorderValues.Single;
                tblBorders2.InsideHorizontalBorder = new InsideHorizontalBorder();
                tblBorders2.InsideHorizontalBorder.Val = BorderValues.Single;
                tblBorders2.InsideVerticalBorder = new InsideVerticalBorder();
                tblBorders2.InsideVerticalBorder.Val = BorderValues.Single;
                tblPr2.Append(tblBorders2);

                resultTable.Append(tblPr2);

                TableRow tr2;
                TableCell tc2;
                tr2 = new TableRow();
                tc2 = new TableCell(new Paragraph(new Run(
                                   new Text("Результаты расчёта по модели"))));
                TableCellProperties tcp2 = new TableCellProperties();
                GridSpan gridSpan2 = new GridSpan();
                gridSpan2.Val = 11;
                tcp2.Append(gridSpan2);
                tc2.Append(tcp2);
                tr2.Append(tc2);
                resultTable.Append(tr2);
                tr2 = new TableRow();
                tc2 = new TableCell();

                tc2.Append(new Paragraph(new Run(new Text(((dgwExperiment.ColumnCount == 2) ? "x1" : "x2  \\  x1")))));
                tr2.Append(tc2);
                for (int i = 0; i < resultForm.dataGridView1.ColumnCount - 1; i++)
                {
                    tr2.Append(new TableCell(new Paragraph(new Run(new Text(" " + resultForm.dataGridView1.Columns[i].HeaderCell.Value.ToString() + " ")))));
                }
                resultTable.Append(tr2);
                for (int i = 0; i < resultForm.dataGridView1.Rows.Count - 1; i++)
                {
                    tr2 = new TableRow();
                    tr2.Append(new TableCell(new Paragraph(new Run(new Text(resultForm.dataGridView1.Rows[i].HeaderCell.Value.ToString())))));
                    for (int j = 0; j < resultForm.dataGridView1.ColumnCount - 1; j++)
                    {
                        tr2.Append(new TableCell(new Paragraph(new Run(new Text(resultForm.dataGridView1.Rows[i].Cells[j].Value.ToString())))));
                    }
                    resultTable.Append(tr2);
                }

                body.Append(new Paragraph(new Run(resultTable)));

                nChartControl1.ImageExporter.SaveToFile(Path.GetDirectoryName(fileName) + "\\" + Experiment.Name.ToString() + ".bmp", new NSize(nChartControl1.Width, nChartControl1.Height), NResolution.ScreenResolution, new NBitmapImageFormat());

                mainPart.Document.Append(body);
                mainPart.Document.Save();
                saveResultToExcel(Path.GetDirectoryName(fileName) + "\\" + Experiment.Name.ToString() + ".xlsx");
            }
        }