alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace AddInPlugin
{
    public class ChineseTableExporter
    {
        // 计算字符串的实际显示宽度(考虑中文)
        private static int GetDisplayWidth(string str)
        {
            if (string.IsNullOrEmpty(str)) return 0;
 
            int width = 0;
            foreach (char c in str)
            {
                if (IsChinese(c) || IsFullWidth(c))
                {
                    width += 2;
                }
                else
                {
                    width += 1;
                }
            }
            return width;
        }
 
        // 判断是否为中文字符
        private static bool IsChinese(char c)
        {
            return c >= 0x4E00 && c <= 0x9FFF;
        }
 
        // 判断是否为全角字符
        private static bool IsFullWidth(char c)
        {
            return (c >= 0x3000 && c <= 0x303F) ||  // CJK Symbols
                   (c >= 0xFF00 && c <= 0xFFEF) ||  // Fullwidth Forms
                   (c >= 0x1100 && c <= 0x11FF) ||  // Hangul Jamo
                   (c >= 0xAC00 && c <= 0xD7AF) ||  // Hangul Syllables
                   (c >= 0x3040 && c <= 0x309F) ||  // Hiragana
                   (c >= 0x30A0 && c <= 0x30FF);    // Katakana
        }
 
        // 格式化字符串(考虑中文对齐)
        private static string FormatString(string str, int targetWidth, bool alignLeft = true)
        {
            int currentWidth = GetDisplayWidth(str);
            int padding = targetWidth - currentWidth;
 
            if (padding <= 0) return str;
 
            if (alignLeft)
            {
                return str + new string(' ', padding);
            }
            else
            {
                return new string(' ', padding) + str;
            }
        }
 
        // 导出表格
        public static string ToString(int columns, List<string[]> datas)
        {
 
            // 计算每列的最大宽度
            int[] columnWidths = new int[columns];
            for (int i = 0; i < columns; i++)
            {
                int maxDataWidth = datas.Max(row => GetDisplayWidth(row[i]));
                columnWidths[i] = maxDataWidth + 2;
            }
            string txt = string.Empty;
 
            // 写入数据
            for (int i = 0; i < datas.Count; i++)
            {
                var row = datas[i];
                string dataLine = string.Join(" | ",
                    Enumerable.Range(0, row.Length)
                              .Select(j => FormatString(row[j], columnWidths[j])));
                txt += $"{dataLine}\r\n";
                if (i < datas.Count - 1)
                {
                    // 写入分隔线
                    string separator = string.Join("-+-",
                        Array.ConvertAll(columnWidths, w => new string('-', w)));
                    txt += $"{separator}\r\n";
                }
            }
            return txt;
        }
    }
 
}