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 { }
    }
}
相关推荐
oh,huoyuyan2 分钟前
火语言RPA--Excel删除内容
excel·rpa
软件黑马王子2 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
失败尽常态5234 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
水煮庄周鱼鱼6 小时前
C# 入门简介
开发语言·c#
软件黑马王子7 小时前
Unity游戏制作中的C#基础(6)方法和类的知识点深度剖析
开发语言·游戏·unity·c#
Nicole Potter8 小时前
请说明C#中的List是如何扩容的?
开发语言·面试·c#
gu209 小时前
c#编程:学习Linq,重几个简单示例开始
开发语言·学习·c#·linq
杜大哥11 小时前
如何在WPS打开的word、excel文件中,使用AI?
人工智能·word·excel·wps
@LitterFisher13 小时前
Excell 代码处理
前端·javascript·excel
winfredzhang13 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写