2011-06-03 160 views
0

我在幾年的編程中遇到過這幾次,所以我決定做一些研究,看看它是否可行。通常我在代碼中創建數據結構,這些數據結構在表格中以行和列的形式初始化,我希望具有此代碼可讀性的表到文本功能。如何在word,excel或其他程序中創建表格,並使用空格(而非製表符)將表格的單元格輸出爲文本? Word可以通過製表符來實現,而Excel可以通過不對齊的空格來實現。有沒有任何程序可以自動執行此操作?將表格轉換爲帶空格的文本

回答

0

當您從excel導出時,您是否嘗試過使用等寬字體,例如快遞?大多數字體會根據每個字符的特定寬度,高度和字距調整間距,但等寬字體將允許您使用空格進行對齊。

至於自動將製表符轉換爲空格,如果沒有1000s的方法,應用程序和命令可用,則必須有100個。

+0

您可以使用Excel並保存爲文本,它使用標籤,而不是空格。然後使用Notepad ++等程序將標籤轉換爲空格。但是Excel使用逗號(通常是Excel中的列分隔符)通過在文本輸出中包含逗號的字段加引號引起不希望出現的情況。 – Sam 2011-06-03 15:42:08

+0

哦,是的,字體不影響純文本輸出...是的,你會認爲這樣的事情很容易找到。 – Sam 2011-06-03 15:43:41

+0

我有一個解決方案,網站不會讓我發佈,明天,下一個工作日,星期一。 – Sam 2011-06-03 15:44:15

0

我花了一個小時或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); 
     } 
    } 
}