220 likes | 804 Vues
OLE Automation. In Microsoft Windows applications programming, OLE Automation is an inter-process communication mechanism based on Component Object Model (COM) that was intended for use by scripting languages – originally Visual Basic –
E N D
OLE Automation In Microsoft Windows applications programming, OLE Automation is an inter-process communication mechanism based on Component Object Model (COM) that was intended for use by scripting languages – originally Visual Basic – but now are used by languages run on Windows. It provides an infrastructure whereby applications called automation controllers can access and manipulate shared automation objects that are exported by other applications. It supersedes Dynamic Data Exchange (DDE), an older mechanism for applications to control one another. As with DDE, in OLE Automation the automation controller is the "client" and the application exporting the automation objects is the "server".
ExampleOLE DB using System; using System.Data; using System.Data.OleDb; using System.Xml.Serialization; public class MainClass { public static void Main () { // Set Access connection and select strings. // The path to BugTypes.MDB must be changed if you build // the sample from the command line: #if USINGPROJECTSYSTEM string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\BugTypes.MDB"; #else string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB"; #endif string strAccessSelect = "SELECT * FROM Categories"; // Create the dataset and add the Categories table to it: DataSet myDataSet = new DataSet(); OleDbConnection myAccessConn = null; try { myAccessConn = new OleDbConnection(strAccessConn); } catch(Exception ex) { Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message); return; }
try { OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand); myAccessConn.Open(); myDataAdapter.Fill(myDataSet,"Categories"); } catch (Exception ex) { Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message); return; } finally { myAccessConn.Close(); } // A dataset can contain multiple tables, so let's get them // all into an array: DataTableCollection dta = myDataSet.Tables; foreach (DataTable dt in dta) { Console.WriteLine("Found data table {0}", dt.TableName); }
// The next two lines show two different ways you can get the // count of tables in a dataset: Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count); Console.WriteLine("{0} tables in data set", dta.Count); // The next several lines show how to get information on // a specific table by name from the dataset: Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["Categories"].Rows.Count); // The column info is automatically fetched from the database, // so we can read it here: Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["Categories"].Columns.Count); DataColumnCollection drc = myDataSet.Tables["Categories"].Columns; int i = 0; foreach (DataColumn dc in drc) { // Print the column subscript, then the column's name // and its data type: Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType); } DataRowCollection dra = myDataSet.Tables["Categories"].Rows; foreach (DataRow dr in dra) { // Print the CategoryID as a subscript, then the CategoryName: Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]); } } }
Example- Word Replace - Delphi var WordApp:Olevariant; yourDocFileName:String; re:Variant; cnt,i:integer; begin yourDocFileName:=Edit4.text; WordApp := CreateOLEObject('Word.Application'); WordApp.Documents.Open(yourDocFileName); WordApp.Selection.Find.ClearFormatting; WordApp.Selection.Find.Text :='#pNAME#'; WordApp.Selection.Find.Replacement.Text :=Edit1.Text; WordApp.Selection.Find.Execute(Replace := wdReplaceAll); WordApp.ActiveDocument.SaveAs(Edit5.Text); WordApp.ActiveDocument.Close; WordApp.Documents.Open(Edit5.Text); WordApp.Visible := True; end;
ExampleWord - Macros VB Dim myStoryRange As Range For Each myStoryRange In ActiveDocument.StoryRanges With myStoryRange.Find .Text = "#pNAme#" .Replacement.Text = "Petur" .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Do While Not (myStoryRange.NextStoryRange Is Nothing) Set myStoryRange = myStoryRange.NextStoryRange With myStoryRange.Find .Text = "Edit2" .Replacement.Text = "Lisabon" .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Loop Next myStoryRange End Sub
Example Excel Създаване на файл Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com"; xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
Excel – Format Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; Excel.Range chartRange ; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //add data xlWorkSheet.Cells[4, 2] = ""; xlWorkSheet.Cells[4, 3] = "Student1"; xlWorkSheet.Cells[4, 4] = "Student2"; xlWorkSheet.Cells[4, 5] = "Student3"; xlWorkSheet.Cells[5, 2] = "Term1"; xlWorkSheet.Cells[5, 3] = "80"; xlWorkSheet.Cells[5, 4] = "65"; xlWorkSheet.Cells[5, 5] = "45"; xlWorkSheet.Cells[6, 2] = "Term2"; xlWorkSheet.Cells[6, 3] = "78"; xlWorkSheet.Cells[6, 4] = "72"; xlWorkSheet.Cells[6, 5] = "60";
… ……………….. xlWorkSheet.get_Range("b2", "e3").Merge(false); chartRange = xlWorkSheet.get_Range("b2", "e3"); chartRange.FormulaR1C1 = "MARK LIST"; chartRange.HorizontalAlignment = 3; chartRange.VerticalAlignment = 3; chartRange = xlWorkSheet.get_Range("b4", "e4"); chartRange.Font.Bold = true; chartRange = xlWorkSheet.get_Range("b9", "e9"); chartRange.Font.Bold = true; chartRange = xlWorkSheet.get_Range("b2", "e9"); chartRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight. xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel. XlColorIndex.xlColorIndexAutomatic); xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat. xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode. xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit();
Insert Picture Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //add some text xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com"; xlWorkSheet.Cells[2, 1] = "Adding picture in Excel File"; xlWorkSheet.Shapes.AddPicture("C:\\csharp-xl-picture.JPG", Microsoft.Office. Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45); xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat. xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode. xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlApp); releaseObject(xlWorkBook); releaseObject(xlWorkSheet); MessageBox.Show ("File created !");
Graphic Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //add data xlWorkSheet.Cells[1, 1] = ""; xlWorkSheet.Cells[1, 2] = "Student1"; xlWorkSheet.Cells[1, 3] = "Student2"; xlWorkSheet.Cells[1, 4] = "Student3"; xlWorkSheet.Cells[2, 1] = "Term1"; xlWorkSheet.Cells[2, 2] = "80"; xlWorkSheet.Cells[2, 3] = "65"; xlWorkSheet.Cells[2, 4] = "45"; xlWorkSheet.Cells[3, 1] = "Term2"; xlWorkSheet.Cells[3, 2] = "78"; xlWorkSheet.Cells[3, 3] = "72"; xlWorkSheet.Cells[3, 4] = "60"; xlWorkSheet.Cells[4, 1] = "Term3"; xlWorkSheet.Cells[4, 2] = "82"; xlWorkSheet.Cells[4, 3] = "80"; xlWorkSheet.Cells[4, 4] = "65"; xlWorkSheet.Cells[5, 1] = "Term4"; xlWorkSheet.Cells[5, 2] = "75"; xlWorkSheet.Cells[5, 3] = "82"; xlWorkSheet.Cells[5, 4] = "68"; Excel.Range chartRange ; Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250); Excel.Chart chartPage = myChart.Chart; chartRange = xlWorkSheet.get_Range("A1", "d5"); chartPage.SetSourceData(chartRange, misValue); chartPage.ChartType = Excel.XlChartType.xlColumnClustered; xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat. xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode. xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");