.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

I want to read data from a DWG excel file using C#.

12 REPLIES 12
Reply
Message 1 of 13
ugur35
5946 Views, 12 Replies

I want to read data from a DWG excel file using C#.

this dwg file data reading with c#?

12 REPLIES 12
Message 2 of 13
Balaji_Ram
in reply to: ugur35

Hello,

 

For an embedded Ole object as in your drawing, you can cast the OleObject as "Microsoft.Office.Interop.Excel.Workbook" and then you can use it appropriately using the Excel API to retrieve the data.

 

Here is a sample code snippet :

 

Ole2Frame oleFrame = tr.GetObject(id, OpenMode.ForRead) as Ole2Frame;

Microsoft.Office.Interop.Excel.Workbook wb = oleFrame.OleObject as Microsoft.Office.Interop.Excel.Workbook;

Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet;

Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 13
ugur35
in reply to: Balaji_Ram

Hello thank you,

but I could not

can you help me please

 

Message 4 of 13
Balaji_Ram
in reply to: ugur35

Here is a sample code that displays all the values read from the embedded Oleobject.

I have attached a screenshot image to show the output.

 

[CommandMethod("Test")]
public static void TestMethod()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        BlockTableRecord bt = Tx.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;

        foreach (ObjectId id in bt)
        {
            if (id.ObjectClass.Name != "AcDbOle2Frame")
                continue;

            Ole2Frame oleFrame = Tx.GetObject(id, OpenMode.ForRead) as Ole2Frame;

            if (! oleFrame.IsLinked)
            {
                Microsoft.Office.Interop.Excel.Workbook wb = oleFrame.OleObject as Microsoft.Office.Interop.Excel.Workbook;

                Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet;

                Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;
                for (int row = 1; row <= 15; row++)
                {
                    for (int col = 1; col <= range.Columns.Count; col++)
                    {
                        ed.WriteMessage(String.Format("{0}{1}{2}-{3}", Environment.NewLine, row, col, Convert.ToString((range.Cells[row, col] as Microsoft.Office.Interop.Excel.Range).Value2)));
                    }
                }
            }
        }
    }
}

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 5 of 13
ugur35
in reply to: Balaji_Ram

thank you very much 

source code was useful 🙂

Message 6 of 13
ugur35
in reply to: ugur35

Thanks for the source code but the transaction did not complete.

I want to do the program .

I have attached a screenshot image to show the output.

 

thank you very much.

Message 7 of 13
ugur35
in reply to: ugur35

hi Balaji Thank you for the source code, but unable to set winform can you help?

Message 8 of 13
Balaji_Ram
in reply to: ugur35

Hello,

 

Can you please explain what you mean by "unable to set winform" ?

 

You can try having a datagrid in a winform and then provide a data table as its source. The data read from the excel can be used to create a data table.

 

Sample code to for this can be found in MSDN help :

http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.datasource.aspx

 

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 9 of 13
ugur35
in reply to: Balaji_Ram

hello balaji

 

application on a Windows form application will be.(C# .NET 4.0)

DO YOU WANT TO PROGRAM

 

source code is being sent to you in advance .

 

[CommandMethod("Test")]

 

public static void TestMethod()

 

{

 

    Document doc = Application.DocumentManager.MdiActiveDocument;

 

    Editor ed = doc.Editor;

 

    Database db = doc.Database;

 

 

 

    using (Transaction Tx = db.TransactionManager.StartTransaction())

 

    {

 

        BlockTableRecord bt = Tx.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;

 

 

 

        foreach (ObjectId id in bt)

 

        {

 

            if (id.ObjectClass.Name != "AcDbOle2Frame")

 

                continue;

 

 

 

            Ole2Frame oleFrame = Tx.GetObject(id, OpenMode.ForRead) as Ole2Frame;

 

 

 

            if (! oleFrame.IsLinked)

 

            {

 

                Microsoft.Office.Interop.Excel.Workbook wb = oleFrame.OleObject as Microsoft.Office.Interop.Excel.Workbook;

 

 

 

                Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet;

 

 

 

                Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;

 

                for (int row = 1; row <= 15; row++)

 

                {

 

                    for (int col = 1; col <= range.Columns.Count; col++)

 

                    {

 

                        ed.WriteMessage(String.Format("{0}{1}{2}-{3}", Environment.NewLine, row, col, Convert.ToString((range.Cells[row, col] as Microsoft.Office.Interop.Excel.Range).Value2)));

 

                    }

 

                }

 

            }

 

        }

 

    }

I can not run this code in windows form application.
I have attached a screenshot image to show the output.

 

Message 10 of 13
norman.yuan
in reply to: ugur35

Of course you CANNOT run the code in Win Form EXE app: the code uses AutoCAD managed (.NET) API, which CAN ONLY be used to run inside AutoCAD.

 

If you HAVE to do a stand-alone EXE app, then you have to use AutoCAD COM API to automate AutoCAD. Or, if you do not mind making things more complicated, you could expose the managed code as COM. But either way, AutoCAD must run.

 

Now, if AutoCAD must run, why do you need the trouble to have an external EXE app? You can just build the same Win form into AutoCAD with the very minor change to the code you showed here. Simply start AutoCAD, enter the command defined in the CommandMethod, off you go.

Message 11 of 13
ugur35
in reply to: norman.yuan

I have to exe because

information that is contained in the excel file dwg

I need to MSSQL database record.

 

Message 12 of 13
norman.yuan
in reply to: ugur35

Well, I am not sure what do you mean by "excel file dwg", never heard of.

 

If you mean a *.dwg file with an Excel worksheet embedded (as your attached "drawing.jpg" shows), the Baliji's code shoud run INSIDE AutoCAD just fine, because it uses AutoCAD .NET API. If your goal is to read data in the worksheet embedded in AutoCAD drawing file, then that code shows how to do it and proves that you DO NOT have to do stand-alone exe app (but you do need Excel installed).

 

As for accessing data in SQL Server database, of course you can access in the same froom inside AutoCAD as you would do in EXE app. Again, it not limit you do use external app.

 

If you insist to use external app, then forget AutoCAD .NET API for now (unless you know it well and know how to expose it to COM), and look into AutoCAD COM API.

 

Without other compelling reasons, I'd use AutoCAD .NET API and go based in Balaji's code and run from inside AutoCAD.

Message 13 of 13
bikithalee
in reply to: ugur35

Check this...Excel Tutorial

 

Lee

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost