using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Excel = Microsoft.Office.Interop.Excel; using Word = Microsoft.Office.Interop.Word; // using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Win32;// Colors.yellow namespace Office1 { /// /// Interaktionslogik für MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void bnExcel1_Click(object sender, RoutedEventArgs e) { var xl = new Excel.Application(); xl.Workbooks.Add(); xl.Visible = true; xl.Cells[1, 1] = "Zahl1";//Row,Column xl.Cells[1, 2] = "Zahl2"; for (int row = 2; row <= 101; row++) { xl.Cells[row, 1] = (100 + row).ToString(); xl.Cells[row, 2] = (123 - row).ToString(); } xl.Cells[4, 1].Select(); for (int row = 2; row < 101; row++) { xl.Cells[row, 1].Select(); xl.ActiveCell.Interior.Color = System.Drawing.Color.Yellow; // winforms: Color xl.ActiveCell.Font.Color = System.Drawing.Color.Red; xl.ActiveCell.Font.Name = "Times New Roman"; xl.ActiveCell.Font.Size = 14; xl.ActiveCell.Font.Bold = true; } xl.Range["B8:B12"].Select(); xl.Selection.Font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleSingle; /* * Selection.Font.Underline = * xlUnderlineStyleSingle * */ } private void bnExcel2_Click(object sender, RoutedEventArgs e) { var xl = new Excel.Application(); xl.Workbooks.Add(); xl.Visible = true; for (int col = 1; col <= 10; col++) { xl.Cells[1, col] = "Zahl" + col;//Row,Column xl.Cells[1, col].Select(); xl.ActiveCell.Interior.Color = System.Drawing.Color.Yellow; xl.ActiveCell.Font.Color = System.Drawing.Color.Red; xl.ActiveCell.Font.Name = "Times New Roman"; xl.ActiveCell.Font.Size = 14; xl.ActiveCell.Font.Bold = true; } for (int col = 1; col <= 10; col++) { for (int row = 2; row <= 201; row++) { xl.Cells[row, col] = row;//Row,Column } } for (int col = 1; col <= 10; col++) { for (int row = 2; row <= 201; row++) { if ((row + col) % 50 == 0) { xl.Cells[row, col].Interior.Color = System.Drawing.Color.Red; } } } } private void bnWord1_Click(object sender, RoutedEventArgs e) { var word = new Word.Application(); word.Visible = true; Word.Document doc = word.Documents.Add(); word.Selection.TypeText("Hello World"); // Eingabetext word.Selection.TypeParagraph(); word.Selection.Font.Color = Word.WdColor.wdColorRed; word.Selection.Font.Name = "Times New Roman"; word.Selection.Font.Size = 14; word.Selection.Font.Bold = 1; word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); // Eingabetext mit Tab´s und Zeilenumbruch word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.TypeText("C Sharp\tist\t\r\ntoll\r\n"); word.Selection.Font.Color = Word.WdColor.wdColorBlack; /* .WidowControl = True .KeepWithNext = True .KeepTogether = True */ word.Selection.ParagraphFormat.WidowControl = -1; word.Selection.ParagraphFormat.KeepWithNext = -1; word.Selection.ParagraphFormat.KeepTogether = -1; word.Selection.TypeText("POS. 2: Gewässer 114\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.TypeText("BK 0 990 5.00 990.00 lfdm\r\n"); word.Selection.ParagraphFormat.WidowControl = 0; word.Selection.ParagraphFormat.KeepWithNext = 0; word.Selection.ParagraphFormat.KeepTogether = 0; word.Selection.TypeText("\r\n"); word.Selection.TypeText("POS. 3: Gewässer 116\r\n"); word.Selection.TypeText("BK 0 1445 6.00 1445.00 lfdm\r\n"); word.Selection.TypeText("SK2 0 1445 1.50 1445.00 lfdm\r\n"); word.Selection.TypeText("BK 1555 2724 5.00 1169.00 lfdm\r\n"); word.Selection.TypeText("SK2 1555 2048 1.50 493.00 lfdm\r\n"); word.Selection.TypeText("SK2 2048 2724 1.00 676.00 lfdm\r\n"); word.Selection.TypeText("\r\n"); Word.Range tableLocation = doc.Range(word.Selection.Start, word.Selection.Start); Microsoft.Office.Interop.Word.Table table = doc.Tables.Add(Range: tableLocation, NumRows: 50, NumColumns: 4); //table.Borders.Enable = 1; table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleDouble; table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleEmboss3D; // wdUnderlineDouble /* * .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor */ // Überschrift-Format for (int col = 1; col <= 4; col++) { Word.Cell cell = table.Cell(1, col); cell.Range.Font.Bold = 1; cell.Range.Font.Size = 14; cell.Range.Font.Color = Word.WdColor.wdColorBlue; cell.Range.Text = "Col " + col; } for (int row = 2; row <= 50; row++) { for (int col = 1; col <= 4; col++) { Word.Cell cell = table.Cell(row, col); cell.Range.Text = (row * 100 + col).ToString(); } } //Selection.MoveDown Unit:=wdLine, Count:=3 //Selection.HomeKey Unit:=wdLine //Selection.MoveRight Unit:=wdCharacter, Count:=9, Extend:=wdExtend //Selection.Rows.HeadingFormat = True // Die Tabelleüberschrift wird auf die nächsten Seite übertragen table.Rows[1].HeadingFormat = -1; } private void bnExcelFormel_Click(object sender, RoutedEventArgs e) { var xl = new Excel.Application(); xl.Workbooks.Add(); xl.Visible = true; xl.Cells[1, 1] = "1. Zahlen"; xl.Cells[1, 2] = "2. Zahlen"; xl.Cells[1, 3] = "Summe"; int n = 10; int start = 2; for (int row = start; row < start + n; row++) { xl.Cells[row, 1] = row + 3; xl.Cells[row, 2] = row + 4; } // xl.Cells[start+n, 1].Select(); // xl.Cells[start+n, 1].Formula = "=SUM(A"+start+":B"+(start+n-1)+")"; // fehler xl.ActiveCell.FormulaR1C1 = "=SUM(R[\"A"+start+":A"+(start+n-1).ToString()+"\"])"; xl.Cells[start + n, 1].FormulaR1C1 = "=SUM(R[-" + n + "]C:R[-1]C)"; for (int row = start; row < start + n; row++) { xl.Cells[row, 3].Formula = "=SUM(A" + row + ":B" + (row) + ")"; } } } }