示例#1
0
        public void CreateReport(IWordReport report)
        {
            var templateFullpath = Path.Combine(Environment.CurrentDirectory, templateFilename);

            if (!File.Exists(templateFullpath))
            {
                throw new ApplicationException($"Файл-шаблон отчета не существует или не задан ({templateFullpath})");
            }

            try
            {
                using (var application = new NetOffice.WordApi.Application())
                {
                    using (var document = application.Documents.Add(templateFullpath))
                    {
                        report.ExportToDocument(document);
                    }

                    application.Visible = true;
                    application.Activate();
                }
            }
            catch (Exception ex)
            {
                throw new SystemException("Ошибка экспорта в документ Word", ex);
            }
        }
示例#2
0
        private void button9_Click_1(object sender, EventArgs e)
        {
            var templateFileName = Path.Combine(Environment.CurrentDirectory, "меню.doc");

            if (!File.Exists(templateFileName))
            {
                throw new FileNotFoundException("File not found.", templateFileName);
            }
            Dishesmenu Du = new Dishesmenu();

            using (var application = new NetOffice.WordApi.Application {
                Visible = true
            })
            {
                using (var document = application.Documents.Add(templateFileName))
                {
                    List <HelpCategory> cats = new List <HelpCategory>();

                    for (int i = 0; i < dataGridView3.RowCount; i++)
                    {
                        HelpCategory temp = new HelpCategory();
                        temp.ves      = (int)dataGridView3.Rows[i].Cells[3].Value;
                        temp.name     = (string)dataGridView3.Rows[i].Cells[2].Value;
                        temp.price    = (decimal)dataGridView3.Rows[i].Cells[4].Value;
                        temp.category = (string)dataGridView3.Rows[i].Cells[6].Value;

                        cats.Add(temp);
                    }


                    var coordinatesTable = document.Bookmarks["Coordinates"].Range.Tables[1];


                    //

                    List <string> uniqueCategory = cats.Select(x => x.category).Distinct().ToList();



                    foreach (string strCat in uniqueCategory)
                    {
                        var tempWord = coordinatesTable.Rows.Add();

                        tempWord.Cells[2].Range.Bold = 3;
                        tempWord.Cells[2].Range.Text = strCat;


                        foreach (HelpCategory cat in cats)
                        {
                            if (cat.category == strCat)
                            {
                                var tempRow = coordinatesTable.Rows.Add();
                                tempRow.Cells[2].Range.Bold = 0;
                                tempRow.Cells[1].Range.Text = cat.ves.ToString();
                                tempRow.Cells[2].Range.Text = cat.name;
                                tempRow.Cells[3].Range.Text = cat.price.ToString("# руб");
                            }
                        }
                    }

                    coordinatesTable.Rows[2].Delete();

                    var MenuTable = document.Bookmarks["data"].Range;
                    int n         = dataGridView2.SelectedRows[0].Index;
                    MenuTable.Text = dataGridView2.Rows[n].Cells[2].Value.ToString();
                    //var MenuTable = document.Bookmarks["Menu"].Range.Text;

                    ////var coordinateRow = Menu.Add();
                }
                application.Activate();
            }
        }