OLE Automation II OLE DB ADO OLE DB

OLE Automation II

OLE DB и ADO

Пример за използване на OLE DB using System; using System. Data. Ole. Db; using System. Xml. Serialization; public class Main. Class { public static void Main () { // Set Access connection and select strings. // The path to Bug. Types. MDB must be changed if you build // the sample from the command line: #if USINGPROJECTSYSTEM string str. Access. Conn = "Provider=Microsoft. Jet. OLEDB. 4. 0; Data Source=. . \Bug. Types. MDB"; #else string str. Access. Conn = "Provider=Microsoft. Jet. OLEDB. 4. 0; Data Source=Bug. Types. MDB"; #endif string str. Access. Select = "SELECT * FROM Categories"; // Create the dataset and add the Categories table to it: Data. Set my. Data. Set = new Data. Set(); Ole. Db. Connection my. Access. Conn = null; try { my. Access. Conn = new Ole. Db. Connection(str. Access. Conn); } catch(Exception ex) { Console. Write. Line("Error: Failed to create a database connection. n{0}", ex. Message); return; }

try { Ole. Db. Command my. Access. Command = new Ole. Db. Command(str. Access. Select, my. Access. Conn); Ole. Db. Data. Adapter my. Data. Adapter = new Ole. Db. Data. Adapter(my. Access. Command); my. Access. Conn. Open(); my. Data. Adapter. Fill(my. Data. Set, "Categories"); } catch (Exception ex) { Console. Write. Line("Error: Failed to retrieve the required data from the Data. Base. n{0}", ex. Message); return; } finally { my. Access. Conn. Close(); } // A dataset can contain multiple tables, so let's get them // all into an array: Data. Table. Collection dta = my. Data. Set. Tables; foreach (Data. Table dt in dta) { Console. Write. Line("Found data table {0}", dt. Table. Name); }

// The next two lines show two different ways you can get the // count of tables in a dataset: Console. Write. Line("{0} tables in data set", my. Data. Set. Tables. Count); Console. Write. Line("{0} tables in data set", dta. Count); // The next several lines show to get information on // a specific table by name from the dataset: Console. Write. Line("{0} rows in Categories table", my. Data. Set. Tables["Categories"]. Rows. Count); // The column info is automatically fetched from the database, // so we can read it here: Console. Write. Line("{0} columns in Categories table", my. Data. Set. Tables["Categories"]. Columns. Count); Data. Column. Collection drc = my. Data. Set. Tables["Categories"]. Columns; int i = 0; foreach (Data. Column dc in drc) { // Print the column subscript, then the column's name // and its data type: Console. Write. Line("Column name[{0}] is {1}, of type {2}", i++ , dc. Column. Name, dc. Data. Type); } Data. Row. Collection dra = my. Data. Set. Tables["Categories"]. Rows; foreach (Data. Row dr in dra) { // Print the Category. ID as a subscript, then the Category. Name: Console. Write. Line("Category. Name[{0}] is {1}", dr[0], dr[1]); } } }

OLE & Office

Word Object Model

Пример - Word Replace - Delphi var Word. App: Olevariant; your. Doc. File. Name: String; re: Variant; cnt, i: integer; begin your. Doc. File. Name: =Edit 4. text; Word. App : = Create. OLEObject('Word. Application'); Word. App. Documents. Open(your. Doc. File. Name); Word. App. Selection. Find. Clear. Formatting; Word. App. Selection. Find. Text : ='#p. NAME#'; Word. App. Selection. Find. Replacement. Text : =Edit 1. Text; Word. App. Selection. Find. Execute(Replace : = wd. Replace. All); Word. App. Active. Document. Save. As(Edit 5. Text); Word. App. Active. Document. Close; Word. App. Documents. Open(Edit 5. Text); Word. App. Visible : = True; end;

Пример Word - Macros VB Dim my. Story. Range As Range For Each my. Story. Range In Active. Document. Story. Ranges With my. Story. Range. Find. Text = "#p. NAme#". Replacement. Text = "Petur". Wrap = wd. Find. Continue. Execute Replace: =wd. Replace. All End With Do While Not (my. Story. Range. Next. Story. Range Is Nothing) Set my. Story. Range = my. Story. Range. Next. Story. Range With my. Story. Range. Find. Text = "Edit 2". Replacement. Text = "Lisabon". Wrap = wd. Find. Continue. Execute Replace: =wd. Replace. All End With Loop Next my. Story. Range End Sub

Excel Object Model

Outlook

Пример Excel Създаване на файл Excel. Application xl. App ; Excel. Workbook xl. Work. Book ; Excel. Worksheet xl. Work. Sheet ; object mis. Value = System. Reflection. Missing. Value; xl. App = new Excel. Application. Class(); xl. Work. Book = xl. App. Workbooks. Add(mis. Value); xl. Work. Sheet = (Excel. Worksheet)xl. Work. Book. Worksheets. get_Item(1); xl. Work. Sheet. Cells[1, 1] = "http: //csharp. net-informations. com"; xl. Work. Book. Save. As("csharp-Excel. xls", Excel. Xl. File. Format. xl. Workbook. Normal, mis. Value, Excel. Xl. Save. As. Access. Mode. xl. Exclusive, mis. Value, mis. Value); xl. Work. Book. Close(true, mis. Value); xl. App. Quit(); release. Object(xl. Work. Sheet); release. Object(xl. Work. Book); release. Object(xl. App); Message. Box. Show("Excel file created , you can find the file c: \csharp-Excel. xls");

форматиране Excel. Application xl. App ; Excel. Workbook xl. Work. Book ; Excel. Worksheet xl. Work. Sheet ; object mis. Value = System. Reflection. Missing. Value; Excel. Range chart. Range ; xl. App = new Excel. Application. Class(); xl. Work. Book = xl. App. Workbooks. Add(mis. Value); xl. Work. Sheet = (Excel. Worksheet)xl. Work. Book. Worksheets. get_Item(1); //add data xl. Work. Sheet. Cells[4, 2] = ""; xl. Work. Sheet. Cells[4, 3] = "Student 1"; xl. Work. Sheet. Cells[4, 4] = "Student 2"; xl. Work. Sheet. Cells[4, 5] = "Student 3"; xl. Work. Sheet. Cells[5, 2] = "Term 1"; xl. Work. Sheet. Cells[5, 3] = "80"; xl. Work. Sheet. Cells[5, 4] = "65"; xl. Work. Sheet. Cells[5, 5] = "45"; xl. Work. Sheet. Cells[6, 2] = "Term 2"; xl. Work. Sheet. Cells[6, 3] = "78"; xl. Work. Sheet. Cells[6, 4] = "72"; xl. Work. Sheet. Cells[6, 5] = "60";

… ………………. . xl. Work. Sheet. get_Range("b 2", "e 3"). Merge(false); chart. Range = xl. Work. Sheet. get_Range("b 2", "e 3"); chart. Range. Formula. R 1 C 1 = "MARK LIST"; chart. Range. Horizontal. Alignment = 3; chart. Range. Vertical. Alignment = 3; chart. Range = xl. Work. Sheet. get_Range("b 4", "e 4"); chart. Range. Font. Bold = true; chart. Range = xl. Work. Sheet. get_Range("b 9", "e 9"); chart. Range. Font. Bold = true; chart. Range = xl. Work. Sheet. get_Range("b 2", "e 9"); chart. Range. Border. Around(Excel. Xl. Line. Style. xl. Continuous, Excel. Xl. Border. Weight. xl. Medium, Excel. Xl. Color. Index. xl. Color. Index. Automatic); xl. Work. Book. Save. As("csharp. net-informations. xls", Excel. Xl. File. Format. xl. Workbook. Normal, mis. Value, Excel. Xl. Save. As. Access. Mode. xl. Exclusive, mis. Value, mis. Value); xl. Work. Book. Close(true, mis. Value); xl. App. Quit();

Вмъкване на изображение Excel. Application xl. App ; Excel. Workbook xl. Work. Book ; Excel. Worksheet xl. Work. Sheet ; object mis. Value = System. Reflection. Missing. Value; xl. App = new Excel. Application. Class(); xl. Work. Book = xl. App. Workbooks. Add(mis. Value); xl. Work. Sheet = (Excel. Worksheet)xl. Work. Book. Worksheets. get_Item(1); //add some text xl. Work. Sheet. Cells[1, 1] = "http: //csharp. net-informations. com"; xl. Work. Sheet. Cells[2, 1] = "Adding picture in Excel File"; xl. Work. Sheet. Shapes. Add. Picture("C: \csharp-xl-picture. JPG", Microsoft. Office. Core. Mso. Tri. State. mso. False, Microsoft. Office. Core. Mso. Tri. State. mso. CTrue, 50, 300, 45); xl. Work. Book. Save. As("csharp. net-informations. xls", Excel. Xl. File. Format. xl. Workbook. Normal, mis. Value, Excel. Xl. Save. As. Access. Mode. xl. Exclusive, mis. Value, mis. Value); xl. Work. Book. Close(true, mis. Value); xl. App. Quit(); release. Object(xl. App); release. Object(xl. Work. Book); release. Object(xl. Work. Sheet); Message. Box. Show ("File created !");

Създаване на графика Excel. Application xl. App ; Excel. Workbook xl. Work. Book ; Excel. Worksheet xl. Work. Sheet ; object mis. Value = System. Reflection. Missing. Value; xl. App = new Excel. Application. Class(); xl. Work. Book = xl. App. Workbooks. Add(mis. Value); xl. Work. Sheet = (Excel. Worksheet)xl. Work. Book. Worksheets. get_Item(1); //add data xl. Work. Sheet. Cells[1, 1] = ""; xl. Work. Sheet. Cells[1, 2] = "Student 1"; xl. Work. Sheet. Cells[1, 3] = "Student 2"; xl. Work. Sheet. Cells[1, 4] = "Student 3"; Excel. Range chart. Range ; xl. Work. Sheet. Cells[2, 1] = "Term 1"; xl. Work. Sheet. Cells[2, 2] = "80"; xl. Work. Sheet. Cells[2, 3] = "65"; xl. Work. Sheet. Cells[2, 4] = "45"; Excel. Chart. Objects xl. Charts = (Excel. Chart. Objects)xl. Work. Sheet. Chart. Objects(Type. Missing); Excel. Chart. Object my. Chart = (Excel. Chart. Object)xl. Charts. Add(10, 80, 300, 250); Excel. Chart chart. Page = my. Chart; xl. Work. Sheet. Cells[3, 1] = "Term 2"; xl. Work. Sheet. Cells[3, 2] = "78"; xl. Work. Sheet. Cells[3, 3] = "72"; xl. Work. Sheet. Cells[3, 4] = "60"; chart. Range = xl. Work. Sheet. get_Range("A 1", "d 5"); chart. Page. Set. Source. Data(chart. Range, mis. Value); chart. Page. Chart. Type = Excel. Xl. Chart. Type. xl. Column. Clustered; xl. Work. Sheet. Cells[4, 1] = "Term 3"; xl. Work. Sheet. Cells[4, 2] = "82"; xl. Work. Sheet. Cells[4, 3] = "80"; xl. Work. Sheet. Cells[4, 4] = "65"; xl. Work. Sheet. Cells[5, 1] = "Term 4"; xl. Work. Sheet. Cells[5, 2] = "75"; xl. Work. Sheet. Cells[5, 3] = "82"; xl. Work. Sheet. Cells[5, 4] = "68"; xl. Work. Book. Save. As("csharp. net-informations. xls", Excel. Xl. File. Format. xl. Workbook. Normal, mis. Value, Excel. Xl. Save. As. Access. Mode. xl. Exclusive, mis. Value, mis. Value); xl. Work. Book. Close(true, mis. Value); xl. App. Quit(); release. Object(xl. Work. Sheet); release. Object(xl. Work. Book); release. Object(xl. App); Message. Box. Show("Excel file created , you can find the file c: \csharp. net-informations. xls");

VM & Ole Automation
- Slides: 17