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 { }
    }
}
相关推荐
19H1 小时前
Flink-Source算子状态恢复分析
c#·linq
bing_1582 小时前
Excel 如何进行多条件查找或求和?
excel
秀儿还能再秀2 小时前
基于Excel的数据分析思维与分析方法
数据分析·excel
bing_1582 小时前
Excel 如何处理更复杂的嵌套逻辑判断?
excel
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
灵犀学长2 小时前
EasyExcel之SheetWriteHandler:解锁Excel写入的高阶玩法
spring boot·excel
枯萎穿心攻击3 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue4 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
昏睡红猹12 小时前
C#脚本化(Roslyn):如何在运行时引入nuget包
c#
张人玉13 小时前
C# 常量与变量
java·算法·c#