翻譯|行業(yè)資訊|編輯:胡濤|2024-07-15 10:34:01.253|閱讀 71 次
概述:在本文中,我們將向您展示如何通過檢測制表位并將其轉(zhuǎn)換為包含位置的表格單元格,將純文本表格轉(zhuǎn)換為表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
文檔布局中的一項典型任務是將使用制表位創(chuàng)建的純文本表格轉(zhuǎn)換為完全格式化的表格。在本文中,我們將向您展示如何通過檢測制表位并將其轉(zhuǎn)換為包含位置的表格單元格,將純文本表格轉(zhuǎn)換為表格。
TX Text Control 是一款功能類似于 MS Word 的文字處理控件,包括文檔創(chuàng)建、編輯、打印、郵件合并、格式轉(zhuǎn)換、拆分合并、導入導出、批量生成等功能。廣泛應用于企業(yè)文檔管理,網(wǎng)站內(nèi)容發(fā)布,電子病歷中病案模板創(chuàng)建、病歷書寫、修改歷史、連續(xù)打印、病案歸檔等功能的實現(xiàn)。
讓我們從 TX Text Control 中的一個簡單表格開始:
要轉(zhuǎn)換表格,我們需要遍歷所有行和列,并創(chuàng)建包含由制表位分隔的每個表格單元格的文本的行字符串。以下代碼顯示如何將表格轉(zhuǎn)換為帶有制表位的純文本表格:
private void ConvertTableToText(TXTextControl.Table table, TXTextControl.TextControl textControl)
{
if (table.NestedTables.Count >= 1)
{
// throw an exception
throw new Exception("Nested tables are not supported.");
}
// Create a new StringBuilder object
StringBuilder sb = new StringBuilder();
var numRows = table.Rows.Count;
var numCols = table.Columns.Count;
int[] tabStopLocations = new int[14];
// Loop through all rows
for (int i = 1; i <= numRows; i++)
{
var offset = 0;
// Loop through all columns
for (int j = 1; j <= numCols; j++)
{
var position = table.Columns[j].Position;
if (j == 1)
{
if (position > 0)
{
tabStopLocations[j - 1] = position;
}
else
{
offset = 1;
}
}
else
{
tabStopLocations[j - 1 - offset] = position;
}
if (j > 1 || (j == 1 && position > 0))
{
sb.Append("\t");
}
sb.Append(table.Cells[i, j].Text);
}
// Append a new line character to the StringBuilder object
sb.Append("\r\n");
}
// Remove table
table.Select();
textControl1.Tables.Remove();
textControl1.Selection.ParagraphFormat.TabPositions = tabStopLocations;
// Set the text of the TextControl
textControl1.Selection.Text = sb.ToString();
}
上述代碼用于StringBuilder創(chuàng)建純文本表格。每個表格行由一行字符串表示,其中包含由制表位分隔的每個表格單元格的文本。表格列的Position 屬性用于獲取表格列在表格中的位置。此位置用于確定純文本表格中的制表位位置。
以下動畫截圖顯示了轉(zhuǎn)換的結(jié)果:
在下一個屏幕截圖中,您可以看到如何處理不同列大小并將其轉(zhuǎn)換為純文本表:
現(xiàn)在,讓我們將純文本表轉(zhuǎn)換回 TX Text Control 中的表。以下代碼顯示如何將帶有制表位的純文本表轉(zhuǎn)換為完全格式化的表:
private void ConvertTextToTable(TXTextControl.TextControl textControl)
{
if (textControl1.Selection.Length == 0)
{
throw new Exception("No text selected.");
}
int[] storedTabPositions = textControl1.Selection.ParagraphFormat.TabPositions;
int startPosition = textControl1.Selection.Start;
int endPosition = startPosition + textControl1.Selection.Length;
var startLine = textControl1.Lines.GetItem(startPosition);
var endLine = textControl1.Lines.GetItem(endPosition - 1);
List<string> lineText = new List<string>();
// Loop through all lines and add the text of each line to the list
for (int i = startLine.Number; i <= endLine.Number; i++)
{
var line = textControl1.Lines[i];
lineText.Add(line.Text);
}
// Calculate the number of rows and columns
int numRows = endLine.Number - startLine.Number + 1;
int numCols = lineText[0].Split('\t').Length;
textControl1.Selection.Text = string.Empty;
// Create a new table
var tableInserted = textControl1.Tables.Add(numRows, numCols);
// Set input position to the first cell
textControl1.Selection.Start -= 1;
// Loop through all rows and columns to populate the table
for (int i = 1; i <= numRows; i++)
{
string[] splitText = lineText[i - 1].Split('\t');
for (int j = 1; j <= numCols; j++)
{
var tableCell = textControl1.Tables.GetItem().Cells[i, j];
// Set the text of the cell, and remove the last CRLF
tableCell.Text = (splitText.Length >= j) ? splitText[j - 1].TrimEnd('\r', '\n') : string.Empty;
// Set the tab position if it's not the first column and the tab position exists
if (j > 1 && storedTabPositions.Length >= j - 1 && storedTabPositions[j - 1] != 0)
{
tableCell.Position = storedTabPositions[j - 2];
}
}
}
}
這基本上是第一個代碼片段的反向操作。純文本表格被拆分成行,每行按制表位拆分。制表位位置用于創(chuàng)建表格列,并將文本插入表格單元格中。
首先必須選擇要轉(zhuǎn)換的純文本表。以下動畫截圖顯示了轉(zhuǎn)換的結(jié)果:
需要注意的是,這只適用于帶有制表位的簡單表格。帶有合并單元格或不同列寬的更復雜的表格需要更復雜的算法才能將純文本表格轉(zhuǎn)換為完全格式化的表格。但是,這種簡單的方法在大多數(shù)情況下已經(jīng)足夠了。
本文介紹了如何在 TX Text Control 中將帶制表位的純文本表格轉(zhuǎn)換為完全格式化的表格,以及如何將表格轉(zhuǎn)換為純文本表格。此轉(zhuǎn)換的關鍵是檢測制表位和正確定位表格單元格。
歡迎下載|體驗更多TX Text Control產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn