开发者

Convert Table to Text with Spaces

开发者 https://www.devze.com 2023-03-10 09:45 出处:网络
I\'ve come across this several times in a couple years of programming so I decided to do some research to see if it was possible. Often I create data structures in code that are initialized in a table

I've come across this several times in a couple years of programming so I decided to do some research to see if it was possible. Often I create data structures in code that are initialized in a table like manner, with rows and columns, and I would have liked to have this table-to-text feature for code readability. How can you create a table in word, or excel, or some other program, and output the cells of the table to text, with spaces (not tabs)? Word can do it with tabs, and excel can do it with misaligned spaces. Is there any pr开发者_高级运维ogram out there that automates this?


Have you tried using a monospace font, such as courier, when you export from excel? Most fonts will adjust spacing based on the specific width, height and kerning of each character but a monospace font will allow you to use spaces for alignment.

As for converting tabs to spaces automagically, there must be 100s if not 1000s of methods, apps, commands available out there.


I spent an hour or 2 researching this. I experimented with excel and word and they both came so close to exact solution that it made me crazy. I tried other programs online but with no luck. Here's my solution, Microsoft's Word's Table-To-Text feature and custom C# program that converts the Word-tabified text to column aligned text with spaces and not tabs.

1) Put your columns and rows in an MS Word Table
2) Convert table to text with tabs (look up how to do this)
3) Save the converted table to a plain text file
4) Use my program to open and convert the file
5) Copy the text in the output file to your code

Below is the C# Windows Form Application I wrote. I apologize for lack of optimization. I was at work and wanted it done as quickly as possible:

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);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消