【问题标题】:Convert Table to Text with Spaces将表格转换为带空格的文本
【发布时间】:2011-06-03 13:47:32
【问题描述】:

在几年的编程中,我曾多次遇到过这种情况,所以我决定做一些研究,看看是否有可能。我经常在代码中创建数据结构,这些数据结构以类似表格的方式初始化,包含行和列,我希望拥有这种表格到文本的功能以提高代码的可读性。如何在 word、excel 或其他程序中创建表格,并将表格的单元格输出为带有空格(而不是制表符)的文本? Word 可以用制表符做到这一点,而 excel 可以用未对齐的空格做到这一点。是否有任何程序可以自动执行此操作?

【问题讨论】:

    标签: text ms-word spaces


    【解决方案1】:

    当您从 excel 导出时,您是否尝试过使用等宽字体,例如 courier?大多数字体会根据每个字符的特定宽度、高度和字距调整间距,但等宽字体允许您使用空格进行对齐。

    至于自动将制表符转换为空格,如果不是 1000 种方法、应用程序和命令,那么必须有 100 种可用的方法、应用程序和命令。

    【讨论】:

    • 您可以使用 Excel 并另存为文本,它使用制表符,而不是空格。然后使用 Notepad++ 之类的程序将制表符转换为空格。然而,Excel 通过在文本输出中包含逗号的字段周围加上引号来使用逗号(通常是 excel 中的列分隔符)做了不受欢迎的事情。
    • 哦,是的,字体不会影响纯文本输出......是的,你会认为这样的东西很容易找到。
    • 我有一个解决方案,网站还不让我发帖,明天再发,下个工作日,所以星期一。
    【解决方案2】:

    我花了一两个小时研究这个。我尝试了 excel 和 word,它们都非常接近精确的解决方案,这让我抓狂。我在网上尝试了其他程序,但没有运气。这是我的解决方案,Microsoft 的 Word 的 Table-To-Text 功能和自定义 C# 程序,可将 Word 制表符文本转换为带有空格而不是制表符的列对齐文本。

    1) 将列和行放入 MS Word 表中
    2) 使用标签将表格转换为文本(查看如何执行此操作)
    3) 将转​​换后的表格保存为纯文本文件
    4) 使用我的程序打开并转换文件
    5) 将输出文件中的文本复制到您的代码中

    以下是我编写的 C# Windows 窗体应用程序。我为缺乏优化道歉。我在工作,希望尽快完成:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                OpenFileDialog of = new OpenFileDialog();
                of.Title = "Select Tabbed Text File To Convert";
    
                if (of.ShowDialog() != DialogResult.OK)
                    return;
    
                StreamReader s = new StreamReader(of.OpenFile());
    
                List<string> lines = new List<string>();
    
                string line;
    
                // Get each line into an array of lines.
                while ((line = s .ReadLine()) != null)
                    lines.Add(line);
    
                int numTabs = 0;
    
                // count the number of tabs in each line, assume good input, i.e. 
                // all lines have equal number of tabs.
                foreach (char c in lines[0])
                    if (c == '\t')
                        numTabs++;
    
                for (int i = 0; i < numTabs; i++)
                {
                    int tabIndex = 0;
    
                    // Loop through each line and find the "deepest" location of
                    // the first tab.
                    foreach (string l in lines)
                    {
                        int index = 0;
    
                        foreach (char c in l)
                        {
                            if (c == '\t')
                            {
                                if (index > tabIndex)
                                    tabIndex = index;
    
                                break;
                            }
    
                            index++;
                        }
                    }
    
                    // We know where the deepest tab is, now we go through and 
                    // add enough spaces to take the first tab of each line out
                    // to the deepest.
                    //foreach (string l in lines)
                    for (int l = 0; l < lines.Count; l++)
                    {
                        int index = 0;
    
                        foreach (char c in lines[l])
                        {
                            if (c == '\t')
                            {
                                int numSpaces = (tabIndex - index) + 1;
    
                                string spaces = "";
    
                                for (int j = 0; j < numSpaces; j++)
                                    spaces = spaces + " ";
    
                                lines[l] = lines[l].Remove(index, 1);
                                lines[l] = lines[l].Insert(index, spaces);
    
                                break;
                            }
    
                            index++;
                        }
                    }
                }
    
                FileInfo f = new FileInfo(of.FileName);
    
                string outputFile = f.FullName.Insert(f.FullName.IndexOf(f.Extension), " (Aligned)");
    
                StreamWriter w = new StreamWriter(outputFile);
    
                foreach (string l in lines)
                    w.Write(l + "\r\n");
    
                w.Close();
                s.Close();
    
                MessageBox.Show("Created the file: " + outputFile);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2012-09-28
      • 2019-04-07
      • 1970-01-01
      • 2019-05-08
      • 2021-10-03
      相关资源
      最近更新 更多