Guest User

Untitled

a guest
Jan 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Excel = Microsoft.Office.Interop.Excel;
  10.  
  11. namespace Excel_Beispiel
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             // Variablen deklarieren
  23.             Excel.Application myExcelApplication;
  24.             Excel.Workbook myExcelWorkbook;
  25.             Excel.Worksheet myExcelWorkSheet;
  26.             myExcelApplication = null;
  27.             try
  28.             {
  29.                 // First Contact: Excel Prozess initialisieren
  30.                 myExcelApplication = new Excel.Application();
  31.                 myExcelApplication.Visible = true;
  32.                 myExcelApplication.ScreenUpdating = true;
  33.                 // Excel Datei anlegen: Workbook
  34.                 var myCount = myExcelApplication.Workbooks.Count;
  35.                 myExcelWorkbook = (Excel.Workbook)(myExcelApplication.Workbooks.Add(System.Reflection.Missing.Value));
  36.                 myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
  37.                 // Überschriften eingeben
  38.                 myExcelWorkSheet.Cells[2, 2] = "Hamburg"; // Zelle B2
  39.                 myExcelWorkSheet.Cells[2, 3] = "Nürnberg"; // Zelle C2
  40.                 myExcelWorkSheet.Cells[2, 4] = "Hamburg"; // Zelle D2
  41.  
  42.                 // Formatieren der Überschrift
  43.                 Excel.Range myRangeHeadline;
  44.                 myRangeHeadline = myExcelWorkSheet.get_Range("B2", "D2");
  45.                 myRangeHeadline.Font.Bold = true;
  46.                 myRangeHeadline.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  47.                 myRangeHeadline.Borders.Weight = Excel.XlBorderWeight.xlThick;
  48.  
  49.                 // Daten eingeben
  50.                 myExcelWorkSheet.Cells[3, 2] = "18";
  51.                 myExcelWorkSheet.Cells[3, 3] = "21";
  52.                 myExcelWorkSheet.Cells[3, 4] = "11";
  53.                 myExcelWorkSheet.Cells[4, 2] = "21";
  54.                 myExcelWorkSheet.Cells[4, 3] = "32";
  55.                 myExcelWorkSheet.Cells[4, 4] = "22";
  56.                 myExcelWorkSheet.Cells[5, 2] = "12";
  57.                 myExcelWorkSheet.Cells[5, 3] = "56";
  58.                 myExcelWorkSheet.Cells[5, 4] = "14";
  59.                 myExcelWorkSheet.Name = "Kunden";
  60.  
  61.  
  62.                 // Chart erzeugen
  63.                 Excel.Range myRangeValues;
  64.                 myRangeValues = myExcelWorkSheet.get_Range("B3", "D5");
  65.  
  66.                 Excel.Chart myChart = (Excel.Chart)myExcelWorkbook.Charts.Add(
  67.                 System.Reflection.Missing.Value,
  68.                 System.Reflection.Missing.Value,
  69.                 System.Reflection.Missing.Value,
  70.                 System.Reflection.Missing.Value);
  71.  
  72.                 myChart.ChartWizard(
  73.                 myRangeValues,
  74.                 Excel.XlChartType.xl3DColumn,
  75.                 System.Reflection.Missing.Value,
  76.                 Excel.XlRowCol.xlRows,
  77.                 System.Reflection.Missing.Value,
  78.                 System.Reflection.Missing.Value,
  79.                 System.Reflection.Missing.Value,
  80.                 "Titel",
  81.                 "Kunden",
  82.                 "Anzahl",
  83.                 System.Reflection.Missing.Value);
  84.  
  85.                 myChart.CopyPicture(Excel.XlPictureAppearance.xlScreen,
  86.                 Excel.XlCopyPictureFormat.xlBitmap,
  87.                 Excel.XlPictureAppearance.xlScreen);
  88.                 myChart.Location(Excel.XlChartLocation.xlLocationAsObject, myExcelWorkSheet.Name);
  89.  
  90.                 // Excel Datei abspeichern
  91.                 // wenn die Datei vorher vorhanden ist, kommt in Excel eine Fehlermeldung.
  92.                 myExcelWorkbook.Close(true, "C:\\kunden.xls", System.Reflection.Missing.Value);
  93.             }
  94.             catch (Exception ex)
  95.             {
  96.                 String myErrorString = ex.Message;
  97.                 MessageBox.Show(myErrorString);
  98.             }
  99.             finally
  100.             {
  101.                 // Excel beenden
  102.                 if (myExcelApplication != null)
  103.                 {
  104.                     myExcelApplication.Quit();
  105.                 }
  106.  
  107.  
  108.             }
  109.         }
  110.     }
  111. }
Add Comment
Please, Sign In to add comment