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 { }
    }
}
相关推荐
江沉晚呤时27 分钟前
C# 实现雪花算法(Snowflake Algorithm)详解与应用
c#·.net
酷炫码神3 小时前
C#语法基础
开发语言·c#
CodeJourney.4 小时前
ChemBlender:科研绘图创新解决方案
数据库·人工智能·信息可视化·excel
酷炫码神5 小时前
C#数据类型
java·服务器·c#
CodeCraft Studio6 小时前
国产化Word处理控件Spire.Doc教程:通过C# 删除 Word 文档中的超链接
开发语言·c#·word
olllo.top7 小时前
从 Excel 到 Data.olllo:数据分析师的提效之路
数据分析·excel
appleคิดถึง7 小时前
wps excel将表格输出pdf时所有列在一张纸上
pdf·excel·wps
Dxy12393102168 小时前
python如何设置excel单元格边框样式
开发语言·python·excel
chaodaibing9 小时前
Python解析Excel入库如何做到行的拆分
开发语言·python·excel
ghost1439 小时前
C#学习第22天:网络编程
开发语言·学习·c#