this dwg file data reading with c#?
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;
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))); } } } } } }
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.
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
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.
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.
Norman Yuan
I have to exe because
information that is contained in the excel file dwg
I need to MSSQL database record.
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.
Norman Yuan
Can't find what you're looking for? Ask the community or share your knowledge.