Elemental
2004-06-16, 16:42:31
Hat hier schon jemand Erfahrung mit der Automation von Excel mittels C#?
Wie man eine Tabelle erzeugt habt ich jetzt schon geschafft:
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "Tag";
oSheet.Cells[1, 2] = "Laufend";
oSheet.Cells[1, 3] = "Wartend";
oSheet.Cells[1, 4] = "Blockiert";
oSheet.Cells[1, 5] = "Unterbrechung";
oSheet.Cells[1, 6] = "Störung";
//Format A1:F1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "F1").Font.Bold = true;
oSheet.get_Range("A1", "F1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
string[,] saNames = new string[5,1];
saNames[0,0] = "Mo";
saNames[1,0] = "Di";
saNames[2,0] = "Mi";
saNames[3,0] = "Do";
saNames[4,0] = "Fr";
//Fill A2:A6 with an array of values (First and Last Names).
oSheet.get_Range("A2", "A6").Value2 = saNames;
// Create an array to multiple values at once.
int[,] saValues = new int[5,5];
saValues[ 0, 0] = 81;
saValues[ 0, 1] = 4;
saValues[ 0, 2] = 3;
saValues[ 0, 3] = 6;
saValues[ 0, 4] = 6;
saValues[ 1, 0] = 92;
saValues[ 1, 1] = 4;
saValues[ 1, 2] = 2;
saValues[ 1, 3] = 1;
saValues[ 1, 4] = 1;
saValues[ 2, 0] = 96;
saValues[ 2, 1] = 1;
saValues[ 2, 2] = 1;
saValues[ 2, 3] = 1;
saValues[ 2, 4] = 1;
saValues[ 3, 0] = 20;
saValues[ 3, 1] = 20;
saValues[ 3, 2] = 20;
saValues[ 3, 3] = 20;
saValues[ 3, 4] = 20;
saValues[ 4, 0] = 20;
saValues[ 4, 1] = 20;
saValues[ 4, 2] = 20;
saValues[ 4, 3] = 20;
saValues[ 4, 4] = 20;
//Fill B2:F6 with an array of values (First and Last Names).
oSheet.get_Range("B2", "F6").Value2 = saValues;
//AutoFit columns A:F.
oRng = oSheet.get_Range("A1", "F1");
oRng.EntireColumn.AutoFit();
//Now insert the chart
//InsertChartIntoSheet(oSheet);
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
Aber ich krieg es nicht hin daraus jetzt noch eine Stacked Chart zu erstellen.
Hab mir das mal als Makro in Excel2003 aufgezeichnet:
Charts.Add
ActiveChart.ChartType = xlColumnStacked100
ActiveChart.SetSourceData Source:=Sheets("Tabelle1").Range("A1:F6"), PlotBy _
:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Tabelle1"
ActiveChart.HasDataTable = False
Kann mir jemand weiterhelfen?
Gruss
Bernd
Wie man eine Tabelle erzeugt habt ich jetzt schon geschafft:
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "Tag";
oSheet.Cells[1, 2] = "Laufend";
oSheet.Cells[1, 3] = "Wartend";
oSheet.Cells[1, 4] = "Blockiert";
oSheet.Cells[1, 5] = "Unterbrechung";
oSheet.Cells[1, 6] = "Störung";
//Format A1:F1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "F1").Font.Bold = true;
oSheet.get_Range("A1", "F1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
string[,] saNames = new string[5,1];
saNames[0,0] = "Mo";
saNames[1,0] = "Di";
saNames[2,0] = "Mi";
saNames[3,0] = "Do";
saNames[4,0] = "Fr";
//Fill A2:A6 with an array of values (First and Last Names).
oSheet.get_Range("A2", "A6").Value2 = saNames;
// Create an array to multiple values at once.
int[,] saValues = new int[5,5];
saValues[ 0, 0] = 81;
saValues[ 0, 1] = 4;
saValues[ 0, 2] = 3;
saValues[ 0, 3] = 6;
saValues[ 0, 4] = 6;
saValues[ 1, 0] = 92;
saValues[ 1, 1] = 4;
saValues[ 1, 2] = 2;
saValues[ 1, 3] = 1;
saValues[ 1, 4] = 1;
saValues[ 2, 0] = 96;
saValues[ 2, 1] = 1;
saValues[ 2, 2] = 1;
saValues[ 2, 3] = 1;
saValues[ 2, 4] = 1;
saValues[ 3, 0] = 20;
saValues[ 3, 1] = 20;
saValues[ 3, 2] = 20;
saValues[ 3, 3] = 20;
saValues[ 3, 4] = 20;
saValues[ 4, 0] = 20;
saValues[ 4, 1] = 20;
saValues[ 4, 2] = 20;
saValues[ 4, 3] = 20;
saValues[ 4, 4] = 20;
//Fill B2:F6 with an array of values (First and Last Names).
oSheet.get_Range("B2", "F6").Value2 = saValues;
//AutoFit columns A:F.
oRng = oSheet.get_Range("A1", "F1");
oRng.EntireColumn.AutoFit();
//Now insert the chart
//InsertChartIntoSheet(oSheet);
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
Aber ich krieg es nicht hin daraus jetzt noch eine Stacked Chart zu erstellen.
Hab mir das mal als Makro in Excel2003 aufgezeichnet:
Charts.Add
ActiveChart.ChartType = xlColumnStacked100
ActiveChart.SetSourceData Source:=Sheets("Tabelle1").Range("A1:F6"), PlotBy _
:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Tabelle1"
ActiveChart.HasDataTable = False
Kann mir jemand weiterhelfen?
Gruss
Bernd