017集——c# 实现Excel交互(npoi免费开源、interope、aspose前期绑定excel和CAD)(CAD—C#二次开发入门)

 NPOI是一个免费且开源的.NET库,用于操作Excel文件[^0^]。它支持读取、修改和生成Excel文件,兼容  .xls  (Excel 97-2003格式)和  .xlsx  (Excel 2007及以上版本的OpenXML格式)

0、采用interope

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using OpenFileDialog = System.Windows.Forms.OpenFileDialog;
[assembly: CommandClass(typeof(IFoxDemo.ExcelToPolylineCommand))]//只允许此类快捷键命令
namespace IFoxDemo
{
  
    public class ExcelToPolylineCommand
    {
        [CommandMethod("xx")]
        public void ExcelToPolyline()
        {
            Document doc = Acap.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            // 弹窗提示用户选择 Excel 文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
            openFileDialog.Title = "选择包含坐标的 Excel 文件";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                ed.WriteMessage("\n用户取消操作。");
                return;
            }
            string filePath = openFileDialog.FileName;
            // 创建Excel应用程序对象
            Excel.Application excelApp = new Excel.Application();
            //excelApp.Visible =true;
            try
            {
                // 读取 Excel 文件
                Workbook workbook = excelApp.Workbooks.Open(filePath);
                //Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\Users\Administrator\Desktop\2.xls");
                Excel.Worksheet worksheet =(Excel.Worksheet)workbook.Sheets[1]; // 获取第一个工作表
                Excel.Range range = worksheet.UsedRange;
                // 存储坐标点
                List<Point2d> points = new List<Point2d>();
                // 遍历工作表的行
                for (int i = 1; i < range.Rows.Count; i++)
                {
                    // 读取点号、X、Y 坐标
                    //string pointNumber = range.Cells[i, 1].ToString(); // 点号
                    double x = Convert.ToDouble((range.Cells[i, 1] as Excel.Range).Value2);// 坐标
                    double y = Convert.ToDouble((range.Cells[i, 2] as Excel.Range).Value2); // Y坐标
                    // 将坐标添加到点集合
                    points.Add(new Point2d(x, y));
                    ed.WriteMessage($"\n读取点 : X = {x}, Y = {y}");
                    //ed.WriteMessage($"\n读取点 {pointNumber}: X = {x}, Y = {y}");
                    
                }
                关闭excel释放对象(excelApp, workbook, worksheet);
                if (points.Count < 2)
                {
                    ed.WriteMessage("\n至少需要两个点来创建多段线。");
                    return;
                }

                // 创建多段线
              
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值