gile 发表于 2010-2-25 02:38:29

Working with OpenOffice Calc document

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
//require references to all "cli_*.dll's files from:
//C:/Program Files/OpenOffice 3.0/openofficeorg1.cab/
// set Copy Local = false
//set Specific Version = false
using unoidl.com.sun.star.system;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.table;
//See original article from there:
//http://c-programming.suite101.com/article.cfm/creating_an_openoffice_calc_document_with_c
namespace OOfficeExm
{
    public class OOfficeTools
    {
      ///
      ///               * Write data into the existing Calc document *
      ///
      public static void WriteToExistingCalc()
      {
            //file name is with back slashes in the OOffice format only:
            string fileName = @"file:///C:/test.ods";
            //The first step is to use
            //the bootstrap method to start OpenOffice.org (or to access any existing instances):
            XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
            //The next step is to use OpenOffice.org's service manager to create a desktop:
            XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
            XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
            PropertyValue[] propVals = new PropertyValue;
            //And then an existing Calc document is added to the desktop:
            XComponent oDoc = oDesk.loadComponentFromURL(fileName, "_private:stream", 0, propVals);//OK
            XSpreadsheets oSheets = ((XSpreadsheetDocument)oDoc).getSheets();
            XIndexAccess oSheetsIA = (XIndexAccess)oSheets;
            XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value;
            for (int i = 0; i
      ///               * Read data from existing Calc document *
      ///
      public static void ReadExistingCalcRange()
      {
            //file name is with back slashes in the OOffice format only:
            string fileName = @"file:///C:/test.ods";
            string target = "A1:D999";
            //The first step is to use
            //the bootstrap method to start OpenOffice.org (or to access any existing instances):
            XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
            //The next step is to use OpenOffice.org's service manager to create a desktop:
            XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
            //Create loader component
            XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
            PropertyValue[] propVals = new PropertyValue;
            //And then an existing Calc document is added to the desktop:
            XComponent xDoc = oDesk.loadComponentFromURL(fileName, "_private:stream", 0, propVals);
            // Then use the service manager for current document
            XMultiServiceFactory xDocFactory = (XMultiServiceFactory)xDoc;
            // Get document sheets
            XSpreadsheets oSheets = ((XSpreadsheetDocument)xDoc).getSheets();
            // Create indexer
            XIndexAccess oSheetsIA = (XIndexAccess)oSheets;
            // Get first sheet
            XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value;
            // Get desired range
            XCellRange oRange = (XCellRange)oSheet.getCellRangeByName(target);
            // After this line there are few ways for the iteration through cells
            // see help docs here:
            //http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/
            //I'm pretty sure there is an easier way to loop through cells
            //but here is my way to retrive rows and columns from address of range:
            int[] columns = GetColumnsFromString(target);
            int[] rows = GetRowsFromString(target);
            // (row and column indexes are starting from zero)
            int startcol = columns - 1;
            int endcol = columns - 1;
            int startrow = rows - 1;
            int endrow = rows - 1;
            ArrayList arr = new ArrayList();
            for (int r = startrow; r
      ///   *   Get first and last column from range address *
      ///
      ///
      ///
      public static int[] GetColumnsFromString(string address)
      {
            int[] columns = new int;
            string sep = ":";
            string[] target = address.Split(sep.ToCharArray());
            string head = target;
            string tail = target;
            columns = Col_AToI(CutNumeric(head));
            columns = Col_AToI(CutNumeric(tail));
            return columns;
      }
      ///
      ///      *   Get first and last row from range address *
      ///
      ///
      ///
      public static int[] GetRowsFromString(string address)
      {
            int[] rows = new int;
            string sep = ":";
            string[] target = address.Split(sep.ToCharArray());
            string head = target;
            string tail = target;
            rows = int.Parse(CutAlpha(head));
            rows = int.Parse(CutAlpha(tail));
            return rows;
      }
      public static string CutNumeric(string str)
      {
            try
            {
                string pattern = @"";
                Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
                str = reg.Replace(str, "");
                return str;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
      }
      public static string CutAlpha(string str)
      {
            try
            {
                string pattern = @"";
                Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
                str = reg.Replace(str, "");
                return str;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
      }
      ///
      ///         * Change column letter(s) to integer *
      ///
      /// from http://www.codekeep.net/snippets/63b58dcb-ab47-4a75-8016-e771fad706c6.aspx
      ///
      ///
      public static int Col_AToI(string strColumn)
      {
            strColumn = strColumn.ToUpper();
            if (strColumn.Length == 1)
            {
                return Convert.ToByte(Convert.ToChar(strColumn)) - 64;
            }
            else if (strColumn.Length == 2)
            {
                return
                  ((Convert.ToByte(strColumn) - 64) * 26) +
                  (Convert.ToByte(strColumn) - 64);
            }
            else if (strColumn.Length == 3)
            {
                return
                  ((Convert.ToByte(strColumn) - 64) * 26 * 26) +
                  ((Convert.ToByte(strColumn) - 64) * 26) +
                  (Convert.ToByte(strColumn) - 64);
            }
            else
            {
                throw new ApplicationException("Column Length must be between 1 and 3.");
            }
      }
    }
}

Kerry 发表于 2010-3-6 11:31:30

Getting or Setting Xrecord to a dictionary (or extension dictionary)
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace Dictionaries
{
    public class DictSample
    {
      // SetXrecord (overloaded)
      public void SetXrecord(Entity ent, string key, ResultBuffer resbuf)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                ent.UpgradeOpen();
                ent.CreateExtensionDictionary();
                DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite);
                Xrecord xRec = new Xrecord();
                xRec.Data = resbuf;
                xDict.SetAt(key, xRec);
                tr.AddNewlyCreatedDBObject(xRec, true);
                tr.Commit();
            }
      }
      public void SetXrecord(DBDictionary dict, string key, ResultBuffer resbuf)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                dict.UpgradeOpen();
                Xrecord xRec = new Xrecord();
                xRec.Data = resbuf;
                dict.SetAt(key, xRec);
                tr.AddNewlyCreatedDBObject(xRec, true);
                tr.Commit();
            }
      }
      public void SetXrecord(string dictName, string key, ResultBuffer resbuf)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
                DBDictionary dict;
                try
                {
                  dict = tr.GetObject(NOD.GetAt(dictName), OpenMode.ForWrite) as DBDictionary;
                }
                catch
                {
                  dict = new DBDictionary();
                  NOD.SetAt(dictName, dict);
                  tr.AddNewlyCreatedDBObject(dict, true);
                  dict.UpgradeOpen();
                }
                Xrecord xRec = new Xrecord();
                xRec.Data = resbuf;
                dict.SetAt(key, xRec);
                tr.AddNewlyCreatedDBObject(xRec, true);
                tr.Commit();
            }
      }
      public void SetXrecord(ObjectId id, string key, ResultBuffer resbuf)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                if (ent != null)
                {
                  ent.UpgradeOpen();
                  ent.CreateExtensionDictionary();
                  DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite);
                  Xrecord xRec = new Xrecord();
                  xRec.Data = resbuf;
                  xDict.SetAt(key, xRec);
                  tr.AddNewlyCreatedDBObject(xRec, true);
                }
                else
                {
                  DBDictionary dict = tr.GetObject(id, OpenMode.ForRead) as DBDictionary;
                  if (dict != null)
                  {
                        dict.UpgradeOpen();
                        Xrecord xRec = new Xrecord();
                        xRec.Data = resbuf;
                        dict.SetAt(key, xRec);
                        tr.AddNewlyCreatedDBObject(xRec, true);
                  }
                }
                tr.Commit();
            }
      }
      // GetXrecord (overloaded)
      public ResultBuffer GetXrecord(Entity ent, string key)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                  DBDictionary xDict =
                        (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead, false);
                  Xrecord xRec = (Xrecord)tr.GetObject(xDict.GetAt(key), OpenMode.ForRead, false);
                  return xRec.Data;
                }
                catch
                {
                  return null;
                }
            }
      }
      public ResultBuffer GetXrecord(DBDictionary dict, string key)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                  Xrecord xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead, false);
                  return xRec.Data;
                }
                catch
                {
                  return null;
                }
            }
      }
      public ResultBuffer GetXrecord(string dictName, string key)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                  DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                  DBDictionary dict = (DBDictionary)tr.GetObject(NOD.GetAt(dictName), OpenMode.ForRead, false);
                  Xrecord xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead, false);
                  return xRec.Data;
                }
                catch
                {
                  return null;
                }
            }
      }
      public ResultBuffer GetXrecord(ObjectId id, string key)
      {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
             {
                Xrecord xRec = new Xrecord();
                Entity ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
                if (ent != null)
                {
                  try
                  {
                        DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead, false);
                        xRec = (Xrecord)tr.GetObject(xDict.GetAt(key), OpenMode.ForRead, false);
                        return xRec.Data;
                  }
                  catch
                  {
                        return null;
                  }
                }
                else
                {
                  DBDictionary dict = tr.GetObject(id, OpenMode.ForRead, false) as DBDictionary;
                  if (dict != null)
                  {
                        try
                        {
                            xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead, false);
                            return xRec.Data;
                        }
                        catch
                        {
                            return null;
                        }
                  }
                  else
                        return null;
                }
            }
      }
    }
}

fixo 发表于 2010-3-11 03:19:14

Automation Word using late-binding and interaction with AutoCAD
Oops...
I have removed my message by reason of bad code block
Try substituted code instead
Create project 'WordReflection'
Drop on form 'Form1' DataGridView named 'dataGridView1'
Drop on form two buttons named 'button1' and 'button2'
Add text for button1 - "Select Circles"
Add text for button2 - "Export DataGridView To Word Document"
Unzip files from an attached archive and add to the project
Change file name and text for footer inside the code block for 'button2'
Compile project
Draw a several circles then run program
Hope that would helps to somebody from our community
~'J'~

gile 发表于 2010-3-11 11:43:20


When working with a large amount of data, you might use Range.Value; much, much quicker.
Dim objCells As Range = objSheet.Range(objSheet.Cells(1, 1), objSheet.Cells(101, 3))
Dim Values(101, 3) As String
For i = 0 To 100
   For j = 0 To 2
          Values(i, j) = Rnd(5).ToString
   Next
Next
'add the entire array at once.
objCells.Value = Values
页: 1 [2]
查看完整版本: .NET MISCELLANEOUS/GENERAL Routines