明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1563|回复: 1

新手帖:如何从Excel剪贴板获得二维数组

[复制链接]
发表于 2012-12-16 17:09 | 显示全部楼层 |阅读模式
学习使用C#来开发AutoCAD已经近两周。
由于开发的是工程小插件,免不了经常需要在Cad和Excel的数据间进行切换。
这里贴出一段代码,示例如何从Excel按Ctrl+C后(剪贴板)生成一个二维数组。
但这段代码希望高手能够提供改进意见,因为有如下问题仍未处理:
1)未判断数据类型是否来自于Excel
2)未判断剪贴板上是否有数据

  1. public static double[][] GetDataFromExcel()
  2.         {
  3.             if (System.Windows.Forms.Clipboard.GetDataObject() != null) // 确保粘贴板中有内容
  4.             {
  5.                 Document dc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  6.                 Editor ed = dc.Editor;
  7.                 IDataObject io = System.Windows.Forms.Clipboard.GetDataObject();
  8.                 string strSum = (string)io.GetData(DataFormats.Text);
  9.                 string[] strRows = strSum.Split('\n');
  10.                 int rowNum = strRows.Length-1; // 获得行数
  11.                 string[] strCols = strRows[1].Split('\t');
  12.                 int colNum = strCols.Length;
  13.                 double[][] res = new double[rowNum][];
  14.                 for (int i = 0; i < rowNum; i++)
  15.                 {
  16.                     res = new double[colNum];
  17.                     string[] newCol = strRows.Split('\t');
  18.                     for (int j = 0; j < colNum; j++)
  19.                     {
  20.                         try
  21.                         {
  22.                             res[j] = double.Parse(newCol[j]);
  23.                         }
  24.                         catch (System.Exception err)
  25.                         {
  26.                             ed.WriteMessage("\n 数据有误" + err.Message);
  27.                         }
  28.                         finally
  29.                         {
  30.                             ed.WriteMessage("\n" + res[j].ToString());
  31.                         }
  32.                     }
  33.                 }
  34.                 return res;
  35.                 // 以下为测试代码
  36.                 //Document rxDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  37.                 //Editor ed = rxDoc.Editor;
  38.                 //for (int i = 0; i < rowNum; i++)
  39.                 //{
  40.                 //    ed.WriteMessage("\n");
  41.                 //    for (int j = 0; j < colNum; j++)
  42.                 //    {
  43.                 //        ed.WriteMessage(res[j].ToString() + "\t");
  44.                 //    }
  45.                 //}
  46.                 ////return res;
  47.                 //ed.WriteMessage("\n 共" + rowNum.ToString()+"行");
  48.                 //ed.WriteMessage("\n 共" + colNum.ToString() + "列");               
  49.             }
  50.             else
  51.             {
  52.                 Document rxDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  53.                 Editor ed = rxDoc.Editor;
  54.                 ed.WriteMessage("\n检测到未从Excel中复制内容,请重试。");
  55.                 double[][] res = new double[1][];
  56.                 return res;
  57.             }
  58.         }


注意包含以下API空间引用:

  1. using System.Data;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Web;
  5. using System.Reflection;
  6. using excel = Microsoft.Office.Interop.Excel;
  7. using System.ComponentModel;
  8. using System.Runtime.Serialization.Formatters.Binary;

可取消注释掉的代码在命令行内看执行结果。
发表于 2012-12-20 16:01 | 显示全部楼层
DataFormats可以判断类型。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-1 04:27 , Processed in 0.315501 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表