CAD表格转excel

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

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

//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 { }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山水CAD插件定制

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

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

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

打赏作者

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

抵扣说明:

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

余额充值