我在幾年的編程中遇到過這幾次,所以我決定做一些研究,看看它是否可行。通常我在代碼中創建數據結構,這些數據結構在表格中以行和列的形式初始化,我希望具有此代碼可讀性的表到文本功能。如何在word,excel或其他程序中創建表格,並使用空格(而非製表符)將表格的單元格輸出爲文本? Word可以通過製表符來實現,而Excel可以通過不對齊的空格來實現。有沒有任何程序可以自動執行此操作?將表格轉換爲帶空格的文本
0
A
回答
0
當您從excel導出時,您是否嘗試過使用等寬字體,例如快遞?大多數字體會根據每個字符的特定寬度,高度和字距調整間距,但等寬字體將允許您使用空格進行對齊。
至於自動將製表符轉換爲空格,如果沒有1000s的方法,應用程序和命令可用,則必須有100個。
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);
}
}
}
相關問題
- 1. 如何將文本表格轉換爲PowerShell中的HTML表格
- 2. 將製表符轉換爲空格
- 3. 將製表符轉換爲空格
- 4. 將文本格式轉換爲不帶時間戳的日期
- 5. 將GridView轉換爲DataTable - 單元格文本爲空
- 6. 將製表符轉換爲記事本++中的空格
- 7. 將RTF轉換爲純文本格式
- 8. 將文本轉換爲PDU格式
- 9. VB.Net將文本轉換爲WWW格式
- 10. 將密碼文本轉換爲星號後放一個空格
- 11. 將空格的單元格數組轉換爲矩陣(cell2mat),同時將空格轉換爲零
- 12. 將表格文本轉換爲水晶報表
- 13. 將openoffice.org文本文檔轉換爲電子表格
- 14. Eclipse通過將製表符轉換爲空格來彌補我的文本
- 15. 將此信息轉換爲表格格式的基本PHP表格
- 16. 如何將空格轉換爲 ?
- 17. URLDecoder正在將'+'轉換爲空格
- 18. 將PDF轉換爲文本 - 保留表格的行 - Python
- 19. UITextField將不間斷空格轉換爲空格嗎?
- 20. 將文本日期格式轉換爲日期格式
- 21. 將文本格式轉換爲數字格式
- 22. 將plist格式文本轉換爲json格式
- 23. 如何將Excel表格轉換爲Trac Wiki表格格式?
- 24. 將字典轉換爲帶格式的列表
- 25. 帶有PDFSharp的表格單元格內的旋轉文本
- 26. 文檔格式轉換爲文本
- 27. 將表單元格文本轉換爲數組
- 28. 有沒有辦法將文本表格轉換爲PowerShell對象
- 29. 將表格的單列轉換爲行
- 30. 如何將帶有空格的數字文本文件轉換爲使用Python的CSV文件3
您可以使用Excel並保存爲文本,它使用標籤,而不是空格。然後使用Notepad ++等程序將標籤轉換爲空格。但是Excel使用逗號(通常是Excel中的列分隔符)通過在文本輸出中包含逗號的字段加引號引起不希望出現的情況。 – Sam 2011-06-03 15:42:08
哦,是的,字體不影響純文本輸出...是的,你會認爲這樣的事情很容易找到。 – Sam 2011-06-03 15:43:41
我有一個解決方案,網站不會讓我發佈,明天,下一個工作日,星期一。 – Sam 2011-06-03 15:44:15