Axapta : Sending data to OpenOffice Calc by X++ using COM

การส่งข้อมูลจาก Axapta ไปให้ OpenOffice อีกวิธีก็คือ ใช้ API ที่ OpenOffice เตรียมไว้ให้ บวกกับคุณสมบัติด้าน COM ของ Axapta

ลักษณะโดยทั่วไปของโปรแกรมประเภท Spread Sheet ก็คือ
- 1 Document มีได้หลาย Sheets
- 1 Sheet มีได้หลาย Ranges หลาย Cells
- 1 Ranges สามารถประกอบได้จาก หลาย Cells
- 1 Cell ใส่ข้อมูลได้ 1 ชุด
ตามคู่มือของ OpenOffice ก็นำมาเขียนโดยใช้ X++ ได้ดังนี้

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
static void ooTest(Args _args)
{
    COM OpenOffice;
    COM oDeskTop;
    COM oDocument;
    COM oSheets;
    COM oSheet;
    COM oRange;
    COM BorderStruct;
    COMVariant arg;
    COMVariant byte;
    Array arr = new Array(Types::String);
    str url;
    int i;
;
    OpenOffice = new Com("com.sun.star.ServiceManager");
    oDeskTop = OpenOffice.CreateInstance("com.sun.star.frame.Desktop");

    // create and initialize a COMVariant object
    arg  = COMVariant::createFromArray(arr);

    //Create new document
    oDocument = oDeskTop.LoadComponentFromURL("private:factory/scalc", "_blank", 0, arg);
    oSheets   = oDocument.getSheets(); //object  นี้รับ sheet ทั้งหมด
    oSheet    = oSheets.getByIndex(0); // object นี้รับ sheet แรกมาเพื่อใช้ทำงาน

    BorderStruct = OpenOffice.Bridge_GetStruct('com.sun.star.table.BorderLine');
    BorderStruct.Color(24567057);
    BorderStruct.LineDistance(0);
    BorderStruct.InnerLineWidth(0);
    BorderStruct.OuterLineWidth(1);

    oRange = oSheet.getCellRangeByName("H2:I2");
    oRange.merge(true); //รวม Cells

    oRange.setPropertyValue("CellBackColor", 16764057);  // format cell
    oRange.SetPropertyValue("LeftBorder",BorderStruct);
    oRange.SetPropertyValue("RightBorder",BorderStruct);
    oRange.SetPropertyValue("TopBorder",BorderStruct);
    oRange.SetPropertyValue("BottomBorder",BorderStruct);
//*****************************
    for(i=1; i<10;i++)
        {
        oRange = oSheet.getCellByPosition(0,i);
        oRange.SetValue(i);
        oRange.setPropertyValue("CellStyle", "Result");
        oRange = oSheet.getCellByPosition(1,i);
        oRange.Setstring('????????');
        oRange.setPropertyValue('CharPosture',100);
        oRange = oSheet.getCellByPosition(2,i);
        oRange.SetValue(i*i);
        oRange.setPropertyValue('CharWeight',200);
        oRange.setPropertyValue('CharUnderline',124);
        oRange = oSheet.getCellByPosition(3,i);
        oRange.SetString(date2str(today(),123,2,2,2,2,4));
        oRange.setPropertyValue("CharColor", 500);
        oRange.setPropertyValue('CharShadowed', True);
        oRange = oSheet.getCellByPosition(4,i);
        oRange.SetString(today());
        oRange.setPropertyValue("CellBackColor", 0x99CCFF);
        oRange.setPropertyValue("CharHeight", 12);
        oRange.setPropertyValue("IsTextWrapped", True);
        oRange = oSheet.getCellByPosition(5,i);
        oRange.SetString(today());
        oRange.setPropertyValue("CellStyle", "Date");
        }
}

ลองเล่นดูนะครับ ลองหลายแบบ ๆ ตามคู่มือได้เลย (ตัวอย่างผมได้มาจาก web เป็นภาษารัสเซียครับ http://axforum.info/forums/showthread.php?t=18722&highlight=openoffice)

Written by Komkid on August 13th, 2009 with no comments.
Read more articles on Axapta and OpenOffice and Programming.

Related articles

No comments

There are still no comments on this article.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:

You have to be identified to write a comment.