.NET

Reply
ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 11 of 15 (253 Views)

Re: Read Embedded Excel sheet

02-15-2013 08:25 AM in reply to: Alexander.Rivilis

Hey Alexander

 

I agree with you... Although, I think the work involed to PInvoke this is more than the work to simply create a C++ application to deal with it.





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 12 of 15 (252 Views)

Re: Read Embedded Excel sheet

02-15-2013 09:02 AM in reply to: fenton.webb

fenton.webb wrote:

Hey Alexander

 

I agree with you... Although, I think the work involed to PInvoke this is more than the work to simply create a C++ application to deal with it.


I totally agree with you. But I do not know whether the author knows how to create program in C++.

[offtopic]

I often meet people who know C#, but can not even understand the code in C++. It's very sad when you consider that these languages are similar in syntax.

[/offtopic]


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 13 of 15 (249 Views)

Re: Read Embedded Excel sheet

02-15-2013 09:08 AM in reply to: Alexander.Rivilis

If someone is going to PInvoke COleClientItem then he sure is going to need to know C++!!! :-)





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Moderator
Alexander.Rivilis
Posts: 1,446
Registered: ‎04-09-2008
Message 14 of 15 (245 Views)

Re: Read Embedded Excel sheet

02-15-2013 09:53 AM in reply to: fenton.webb

fenton.webb wrote:

If someone is going to PInvoke COleClientItem then he sure is going to need to know C++!!! :-)


Oh! I think that's where you're wrong. There are volunteers to help write the function signature for P/Invoke (and I am one of them).


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
Ertqwa
Posts: 99
Registered: ‎10-03-2011
Message 15 of 15 (228 Views)

Re: Read Embedded Excel sheet

02-17-2013 10:34 AM in reply to: Ertqwa

Hello,

 

I've got it working. Thought I post it here, if anyone is looking for something similar:

 

[CommandMethod("TestExcel")]
public void TestExcel()
{
    // using Excel = Microsoft.Office.Interop.Excel;
    Excel.Workbook objWorkBook = null;
    Excel.Worksheet objWorksheet = null;
    Excel.Range objRange = null;
    Document objDocument = null;
    Database objDatabase = null;
    Editor objEditor = null;
    Transaction objTransaction = null;
    PromptEntityResult objPromptEntityResult = null;
    Object oidExcel;
    Ole2Frame objOle2Frame;
    string strCell;
    int intRow = 0;
    int intColumn = 0;

    try
    {
        // Get active document, database and editor.
        objDocument = CADS.Application.DocumentManager.MdiActiveDocument;
        objDatabase = objDocument.Database;
        objEditor = objDocument.Editor;
        // Ask user to select the excel tabel.
        objPromptEntityResult = objEditor.GetEntity("\nSelect the excel table: ");
        if (objPromptEntityResult.Status != PromptStatus.OK)
        { return; }
        // Get id of selected object.
        oidExcel = objPromptEntityResult.ObjectId;
        // Start transaction.
        using (objTransaction = objDatabase.TransactionManager.StartTransaction())
        {
            // Get Ole2Frame object.
            objOle2Frame = objTransaction.GetObject(objPromptEntityResult.ObjectId, OpenMode.ForRead) as Ole2Frame;
            // The Ole2Frame will be an object of type Microsoft.Office.Interop.Excel.Workbook.
            objWorkBook = objOle2Frame.OleObject as Excel.Workbook;
            // Get the first sheet (it is assumed the table has one).
            objWorksheet = (Excel.Worksheet)objWorkBook.Worksheets.get_Item(1);
            // Get the range of cells in the sheet.
            objRange = objWorksheet.UsedRange;
            // Loop the range and write contents to the editor.
            for (intRow = 1; intRow <= objRange.Rows.Count; intRow++)
            {
                for (intColumn = 1; intColumn <= objRange.Columns.Count; intColumn++)
                {
                    try
                    {
                        strCell = Convert.ToString((objRange.Cells[intRow, intColumn] as Excel.Range).Value2);
                        objDocument.Editor.WriteMessage("\n{0}.", strCell);
                    }
                    catch
                    {
                        objDocument.Editor.WriteMessage("\nCould not convert cell {0},{1} to a string.", intRow.ToString(), intColumn.ToString());
                    }
                }
            }
        }
    }
    catch (System.Exception Ex)
    {
        CADS.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Error retreiving data from embedded excel file: " + Ex.ToString());
    }
}

 

Thank you VERY much, all who responded to my question.

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!