乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: Kerry

.NET MISCELLANEOUS/GENERAL Routines

[复制链接]

15

主题

687

帖子

169

银币

中流砥柱

Rank: 25

铜币
582
发表于 2010-2-25 02:38:29 | 显示全部楼层
Working with OpenOffice Calc document
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.Text.RegularExpressions;
  6. //require references to all "cli_*.dll's files from:
  7. //C:/Program Files/OpenOffice 3.0/openofficeorg1.cab/
  8. // set Copy Local = false
  9. //set Specific Version = false
  10. using unoidl.com.sun.star.system;
  11. using unoidl.com.sun.star.lang;
  12. using unoidl.com.sun.star.uno;
  13. using unoidl.com.sun.star.bridge;
  14. using unoidl.com.sun.star.frame;
  15. using unoidl.com.sun.star.text;
  16. using unoidl.com.sun.star.beans;
  17. using unoidl.com.sun.star.sheet;
  18. using unoidl.com.sun.star.container;
  19. using unoidl.com.sun.star.table;
  20. //See original article from there:
  21. //http://c-programming.suite101.com/article.cfm/creating_an_openoffice_calc_document_with_c
  22. namespace OOfficeExm
  23. {
  24.     public class OOfficeTools
  25.     {
  26.         ///
  27.         ///                 * Write data into the existing Calc document *
  28.         ///
  29.         public static void WriteToExistingCalc()
  30.         {
  31.             //file name is with back slashes in the OOffice format only:
  32.             string fileName = @"file:///C:/test.ods";
  33.             //The first step is to use
  34.             //the bootstrap method to start OpenOffice.org (or to access any existing instances):
  35.             XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
  36.             //The next step is to use OpenOffice.org's service manager to create a desktop:
  37.             XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
  38.             XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
  39.             PropertyValue[] propVals = new PropertyValue[0];
  40.             //And then an existing Calc document is added to the desktop:
  41.             XComponent oDoc = oDesk.loadComponentFromURL(fileName, "_private:stream", 0, propVals);//OK
  42.             XSpreadsheets oSheets = ((XSpreadsheetDocument)oDoc).getSheets();
  43.             XIndexAccess oSheetsIA = (XIndexAccess)oSheets;
  44.             XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value;
  45.             for (int i = 0; i
  46.         ///                 * Read data from existing Calc document *
  47.         ///
  48.         public static void ReadExistingCalcRange()
  49.         {
  50.             //file name is with back slashes in the OOffice format only:
  51.             string fileName = @"file:///C:/test.ods";
  52.             string target = "A1:D999";
  53.             //The first step is to use
  54.             //the bootstrap method to start OpenOffice.org (or to access any existing instances):
  55.             XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
  56.             //The next step is to use OpenOffice.org's service manager to create a desktop:
  57.             XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
  58.             //Create loader component
  59.             XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
  60.             PropertyValue[] propVals = new PropertyValue[0];
  61.             //And then an existing Calc document is added to the desktop:
  62.             XComponent xDoc = oDesk.loadComponentFromURL(fileName, "_private:stream", 0, propVals);
  63.             // Then use the service manager for current document
  64.             XMultiServiceFactory xDocFactory = (XMultiServiceFactory)xDoc;
  65.             // Get document sheets
  66.             XSpreadsheets oSheets = ((XSpreadsheetDocument)xDoc).getSheets();
  67.             // Create indexer
  68.             XIndexAccess oSheetsIA = (XIndexAccess)oSheets;
  69.             // Get first sheet
  70.             XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value;
  71.             // Get desired range
  72.             XCellRange oRange = (XCellRange)oSheet.getCellRangeByName(target);
  73.             // After this line there are few ways for the iteration through cells
  74.             // see help docs here:
  75.             //http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/
  76.             //I'm pretty sure there is an easier way to loop through cells
  77.             //but here is my way to retrive rows and columns from address of range:
  78.             int[] columns = GetColumnsFromString(target);
  79.             int[] rows = GetRowsFromString(target);
  80.             // (row and column indexes are starting from zero)
  81.             int startcol = columns[0] - 1;
  82.             int endcol = columns[1] - 1;
  83.             int startrow = rows[0] - 1;
  84.             int endrow = rows[1] - 1;
  85.             ArrayList arr = new ArrayList();
  86.             for (int r = startrow; r
  87.         ///     *   Get first and last column from range address *
  88.         ///
  89.         ///
  90.         ///
  91.         public static int[] GetColumnsFromString(string address)
  92.         {
  93.             int[] columns = new int[2];
  94.             string sep = ":";
  95.             string[] target = address.Split(sep.ToCharArray());
  96.             string head = target[0];
  97.             string tail = target[1];
  98.             columns[0] = Col_AToI(CutNumeric(head));
  99.             columns[1] = Col_AToI(CutNumeric(tail));
  100.             return columns;
  101.         }
  102.         ///
  103.         ///      *   Get first and last row from range address *
  104.         ///
  105.         ///
  106.         ///
  107.         public static int[] GetRowsFromString(string address)
  108.         {
  109.             int[] rows = new int[2];
  110.             string sep = ":";
  111.             string[] target = address.Split(sep.ToCharArray());
  112.             string head = target[0];
  113.             string tail = target[1];
  114.             rows[0] = int.Parse(CutAlpha(head));
  115.             rows[1] = int.Parse(CutAlpha(tail));
  116.             return rows;
  117.         }
  118.         public static string CutNumeric(string str)
  119.         {
  120.             try
  121.             {
  122.                 string pattern = @"[0-9]";
  123.                 Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
  124.                 str = reg.Replace(str, "");
  125.                 return str;
  126.             }
  127.             catch (System.Exception ex)
  128.             {
  129.                 throw ex;
  130.             }
  131.         }
  132.         public static string CutAlpha(string str)
  133.         {
  134.             try
  135.             {
  136.                 string pattern = @"[aA-zZ]";
  137.                 Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
  138.                 str = reg.Replace(str, "");
  139.                 return str;
  140.             }
  141.             catch (System.Exception ex)
  142.             {
  143.                 throw ex;
  144.             }
  145.         }
  146.         ///
  147.         ///           * Change column letter(s) to integer *
  148.         ///
  149.         /// from http://www.codekeep.net/snippets/63b58dcb-ab47-4a75-8016-e771fad706c6.aspx
  150.         ///
  151.         ///
  152.         public static int Col_AToI(string strColumn)
  153.         {
  154.             strColumn = strColumn.ToUpper();
  155.             if (strColumn.Length == 1)
  156.             {
  157.                 return Convert.ToByte(Convert.ToChar(strColumn)) - 64;
  158.             }
  159.             else if (strColumn.Length == 2)
  160.             {
  161.                 return
  162.                     ((Convert.ToByte(strColumn[0]) - 64) * 26) +
  163.                     (Convert.ToByte(strColumn[1]) - 64);
  164.             }
  165.             else if (strColumn.Length == 3)
  166.             {
  167.                 return
  168.                     ((Convert.ToByte(strColumn[0]) - 64) * 26 * 26) +
  169.                     ((Convert.ToByte(strColumn[1]) - 64) * 26) +
  170.                     (Convert.ToByte(strColumn[2]) - 64);
  171.             }
  172.             else
  173.             {
  174.                 throw new ApplicationException("Column Length must be between 1 and 3.");
  175.             }
  176.         }
  177.     }
  178. }
回复

使用道具 举报

116

主题

996

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1466
发表于 2010-3-6 11:31:30 | 显示全部楼层
Getting or Setting Xrecord to a dictionary (or extension dictionary)
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  6. namespace Dictionaries
  7. {
  8.     public class DictSample
  9.     {
  10.         // SetXrecord (overloaded)
  11.         public void SetXrecord(Entity ent, string key, ResultBuffer resbuf)
  12.         {
  13.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  14.             Database db = doc.Database;
  15.             using (Transaction tr = db.TransactionManager.StartTransaction())
  16.             {
  17.                 ent.UpgradeOpen();
  18.                 ent.CreateExtensionDictionary();
  19.                 DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite);
  20.                 Xrecord xRec = new Xrecord();
  21.                 xRec.Data = resbuf;
  22.                 xDict.SetAt(key, xRec);
  23.                 tr.AddNewlyCreatedDBObject(xRec, true);
  24.                 tr.Commit();
  25.             }
  26.         }
  27.         public void SetXrecord(DBDictionary dict, string key, ResultBuffer resbuf)
  28.         {
  29.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  30.             Database db = doc.Database;
  31.             using (Transaction tr = db.TransactionManager.StartTransaction())
  32.             {
  33.                 dict.UpgradeOpen();
  34.                 Xrecord xRec = new Xrecord();
  35.                 xRec.Data = resbuf;
  36.                 dict.SetAt(key, xRec);
  37.                 tr.AddNewlyCreatedDBObject(xRec, true);
  38.                 tr.Commit();
  39.             }
  40.         }
  41.         public void SetXrecord(string dictName, string key, ResultBuffer resbuf)
  42.         {
  43.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  44.             Database db = doc.Database;
  45.             using (Transaction tr = db.TransactionManager.StartTransaction())
  46.             {
  47.                 DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
  48.                 DBDictionary dict;
  49.                 try
  50.                 {
  51.                     dict = tr.GetObject(NOD.GetAt(dictName), OpenMode.ForWrite) as DBDictionary;
  52.                 }
  53.                 catch
  54.                 {
  55.                     dict = new DBDictionary();
  56.                     NOD.SetAt(dictName, dict);
  57.                     tr.AddNewlyCreatedDBObject(dict, true);
  58.                     dict.UpgradeOpen();
  59.                 }
  60.                 Xrecord xRec = new Xrecord();
  61.                 xRec.Data = resbuf;
  62.                 dict.SetAt(key, xRec);
  63.                 tr.AddNewlyCreatedDBObject(xRec, true);
  64.                 tr.Commit();
  65.             }
  66.         }
  67.         public void SetXrecord(ObjectId id, string key, ResultBuffer resbuf)
  68.         {
  69.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  70.             Database db = doc.Database;
  71.             using (Transaction tr = db.TransactionManager.StartTransaction())
  72.             {
  73.                 Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
  74.                 if (ent != null)
  75.                 {
  76.                     ent.UpgradeOpen();
  77.                     ent.CreateExtensionDictionary();
  78.                     DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite);
  79.                     Xrecord xRec = new Xrecord();
  80.                     xRec.Data = resbuf;
  81.                     xDict.SetAt(key, xRec);
  82.                     tr.AddNewlyCreatedDBObject(xRec, true);
  83.                 }
  84.                 else
  85.                 {
  86.                     DBDictionary dict = tr.GetObject(id, OpenMode.ForRead) as DBDictionary;
  87.                     if (dict != null)
  88.                     {
  89.                         dict.UpgradeOpen();
  90.                         Xrecord xRec = new Xrecord();
  91.                         xRec.Data = resbuf;
  92.                         dict.SetAt(key, xRec);
  93.                         tr.AddNewlyCreatedDBObject(xRec, true);
  94.                     }
  95.                 }
  96.                 tr.Commit();
  97.             }
  98.         }
  99.         // GetXrecord (overloaded)
  100.         public ResultBuffer GetXrecord(Entity ent, string key)
  101.         {
  102.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  103.             Database db = doc.Database;
  104.             using (Transaction tr = db.TransactionManager.StartTransaction())
  105.             {
  106.                 try
  107.                 {
  108.                     DBDictionary xDict =
  109.                         (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead, false);
  110.                     Xrecord xRec = (Xrecord)tr.GetObject(xDict.GetAt(key), OpenMode.ForRead, false);
  111.                     return xRec.Data;
  112.                 }
  113.                 catch
  114.                 {
  115.                     return null;
  116.                 }
  117.             }
  118.         }
  119.         public ResultBuffer GetXrecord(DBDictionary dict, string key)
  120.         {
  121.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  122.             Database db = doc.Database;
  123.             using (Transaction tr = db.TransactionManager.StartTransaction())
  124.             {
  125.                 try
  126.                 {
  127.                     Xrecord xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead, false);
  128.                     return xRec.Data;
  129.                 }
  130.                 catch
  131.                 {
  132.                     return null;
  133.                 }
  134.             }
  135.         }
  136.         public ResultBuffer GetXrecord(string dictName, string key)
  137.         {
  138.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  139.             Database db = doc.Database;
  140.             using (Transaction tr = db.TransactionManager.StartTransaction())
  141.             {
  142.                 try
  143.                 {
  144.                     DBDictionary NOD = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
  145.                     DBDictionary dict = (DBDictionary)tr.GetObject(NOD.GetAt(dictName), OpenMode.ForRead, false);
  146.                     Xrecord xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead, false);
  147.                     return xRec.Data;
  148.                 }
  149.                 catch
  150.                 {
  151.                     return null;
  152.                 }
  153.             }
  154.         }
  155.         public ResultBuffer GetXrecord(ObjectId id, string key)
  156.         {
  157.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  158.             Database db = doc.Database;
  159.             using (Transaction tr = db.TransactionManager.StartTransaction())
  160.              {
  161.                 Xrecord xRec = new Xrecord();
  162.                 Entity ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
  163.                 if (ent != null)
  164.                 {
  165.                     try
  166.                     {
  167.                         DBDictionary xDict = (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead, false);
  168.                         xRec = (Xrecord)tr.GetObject(xDict.GetAt(key), OpenMode.ForRead, false);
  169.                         return xRec.Data;
  170.                     }
  171.                     catch
  172.                     {
  173.                         return null;
  174.                     }
  175.                 }
  176.                 else
  177.                 {
  178.                     DBDictionary dict = tr.GetObject(id, OpenMode.ForRead, false) as DBDictionary;
  179.                     if (dict != null)
  180.                     {
  181.                         try
  182.                         {
  183.                             xRec = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForRead, false);
  184.                             return xRec.Data;
  185.                         }
  186.                         catch
  187.                         {
  188.                             return null;
  189.                         }
  190.                     }
  191.                     else
  192.                         return null;
  193.                 }
  194.             }
  195.         }
  196.     }
  197. }
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 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'~
回复

使用道具 举报

15

主题

687

帖子

169

银币

中流砥柱

Rank: 25

铜币
582
发表于 2010-3-11 11:43:20 | 显示全部楼层

When working with a large amount of data, you might use Range.Value; much, much quicker.
  1. Dim objCells As Range = objSheet.Range(objSheet.Cells(1, 1), objSheet.Cells(101, 3))
  2. Dim Values(101, 3) As String
  3. For i = 0 To 100
  4.      For j = 0 To 2
  5.           Values(i, j) = Rnd(5).ToString
  6.      Next
  7. Next
  8. 'add the entire array at once.
  9. objCells.Value = Values
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-5 22:13 , Processed in 0.225614 second(s), 58 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表