CAD表格转excel

cad表格转excel课通过插件实现。

网上找到网友分享的代码如下:

cs 复制代码
//CAD直线表格转CSV
[CommandMethod(nameof(BG))]
public void BG()
{
    using var tr = new DBTrans();
    var r1 = Env.Editor.GetSelection();
    if (r1.Status == PromptStatus.OK)
    {
        var lines = r1.Value.GetEntities<Line>();
        var dbtexts = r1.Value.GetEntities<DBText>().OrderByDescending(t => t.Position.Y);
        var mtexts = r1.Value.GetEntities<MText>().OrderByDescending(t => t.Location.Y);

        // 初始化CSV构建器
        StringBuilder csvBuilder = new StringBuilder();

        // 提取唯一的X坐标并排序
        var xCoords = lines.SelectMany(line => new[] { Math.Round(line.StartPoint.X, 3), Math.Round(line.EndPoint.X, 3) })
           .Distinct()
           .OrderBy(x => x)
           .ToList();

        // 提取唯一的Y坐标并排序
        var yCoords = lines.SelectMany(line => new[] { Math.Round(line.StartPoint.Y, 3), Math.Round(line.EndPoint.Y, 3) })
           .Distinct()
           .OrderByDescending(y => y)
           .ToList();

        // 表格数据的二维数组
        string[,] tableData = new string[yCoords.Count - 1, xCoords.Count - 1];

        // 定位每个文本到对应的单元格
        foreach (var text in dbtexts)
        {
            // 查找文本对应的列索引
            var colIndex = xCoords.FindIndex(x => x > text.Position.X) - 1;
            // 查找文本对应的行索引
            var rowIndex = yCoords.FindIndex(y => y < text.Position.Y) - 1;

            // 如果索引有效,将文本的字符串内容(TextString)放入之前定义的tableData二维数组的相应位置
            // tableData数组的每个元素代表表格的一个单元格,行和列索引对应于数组的维度
            if (rowIndex >= 0 && colIndex >= 0)
            {
                if (tableData[rowIndex, colIndex] != "")
                    tableData[rowIndex, colIndex] =tableData[rowIndex, colIndex]+text.TextString;
                else
                    tableData[rowIndex, colIndex] = text.TextString;
            }
        }

        // 定位每个多行文本到对应的单元格
        foreach (var text in mtexts)
        {
            // 查找文本对应的列索引
            var colIndex = xCoords.FindIndex(x => x > text.Location.X) - 1;
            // 查找文本对应的行索引
            var rowIndex = yCoords.FindIndex(y => y < text.Location.Y) - 1;

            // 如果索引有效,将文本的字符串内容(TextString)放入之前定义的tableData二维数组的相应位置
            // tableData数组的每个元素代表表格的一个单元格,行和列索引对应于数组的维度
            if (rowIndex >= 0 && colIndex >= 0)
            {
                if (tableData[rowIndex, colIndex] != "")
                    tableData[rowIndex, colIndex] = tableData[rowIndex, colIndex] + text.Text.Replace("\r\n", "");
                else
                    tableData[rowIndex, colIndex] = text.Text.Replace("\r\n", "");
            }
        }

        // 构建CSV内容
        for (int i = 0; i < tableData.GetLength(0); i++)
        {
            var rowData = new List<string>();
            for (int j = 0; j < tableData.GetLength(1); j++)
            {
                rowData.Add(tableData[i, j] ?? "");
            }
            csvBuilder.AppendLine(string.Join(",", rowData.ToArray()));
        }

        // 将内容写入CSV文件
        string 文件路径 = @"D:\1.csv";
        try
        {
            File.WriteAllText(文件路径, csvBuilder.ToString());
        }
        finally { }
    }
}
相关推荐
Abigail_chow1 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
绿荫阿广3 小时前
互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(五):使用.NET为树莓派开发Wifi配网功能
c#·.net
ou.cs5 小时前
c# :this() 和 :base()区别
开发语言·c#
汪小白JIY8 小时前
【C#】异步和多线程
c#·thread·async·task·threapool
宝桥南山10 小时前
DeepSeek - 尝试一下GitHub Models中的DeepSeek
microsoft·ai·微软·c#·github·.net
xiaohezi12 小时前
Rag chunk 之:Excel 文档解析
excel
weixin_4723394616 小时前
python批量解析提取word内容到excel
python·word·excel
lljss202017 小时前
C# 一个解决方案放一个dll项目,一个dll测试项目 ,调试dll项目的源码
c#
ghost1431 天前
C#学习第27天:时间和日期的处理
开发语言·学习·c#
jason成都1 天前
c#压缩与解压缩-SharpCompress
开发语言·c#