VSTO Word插件在拆分两个表

时间:2017-01-31 11:00:26

标签: c# ms-word vsto

我是C#-Topic(PHP-Background)的新手,我在使用Word-Plugin遇到了同样的问题。如果行数达到某个断点,则任务是将表拆分为两部分。为了解决这个问题,我只是拆分了表格,在表格2中添加了完全相同的标题,创建了一个带有两列的不可见表格(它们需要是平行的),将这两个分割的表格粘贴在1,1和1,2列中。这适用于单个表,但如果有两个或更多,它只会崩溃而没有任何异常。我唯一知道的是,在填充新表中的第一个标题之前,应用程序崩溃了。可能是2个或更多表的性能问题?

图片中的任务

+------------+------------+
| Headline 1 | Headline 2 |
+------------+------------+
| Col 1,1    | Col 1,2    |
| Col 2,1    | Col 2,2    |
| Col 3,1    | Col 3,2    |
+------------+------------+

+------------+------------+    +------------+------------+
| Headline 1 | Headline 2 |    | Headline 1 | Headline 2 |
+------------+------------+    +------------+------------+
| Col 1,1    | Col 1,2    |    | Col 3,1    | Col 3,2    |
| Col 2,1    | Col 2,2    |    +------------+------------+
+------------+------------+    

这是我的代码

private long createInvisibleTable() {
    long timestamp = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
    doc.Paragraphs.SpaceAfter = 0;
    Word.Table newTable = doc.Tables.Add(Globals.ThisAddIn.Application.Selection.Range, 1, 2);
    newTable.AllowAutoFit = true;
    newTable.Descr = timestamp.ToString();
    newTable.Cell(1, 1).Range.Text = "1. Tabelle hier einfügen";
    newTable.Cell(1, 2).Range.Text = "2. Tabelle hier einfügen";
    return timestamp;
}


private Word.Table getInvisibleTable(long hash) {
    Word.Tables docTables = Globals.ThisAddIn.Application.ActiveDocument.Tables;
    foreach (Word.Table thisTable in docTables) {
        if (thisTable.Tables.Count > 0) {
            this.getInvisibleTable(hash);
        } else if (thisTable.Descr == hash.ToString()) {
            return thisTable;
        }
    }
    return null;
}


private void splitTables(Word.Tables tablesRec)
{
    foreach (Word.Table table in tablesRec)
    {
        if (table.Tables.Count > 0)
        {
            this.splitTables(table.Tables);
        }
        else
        {
            int schwellenwert = this.splitTableSchwellenwert;
            double breakPoint = schwellenwert;
            int counter = 0;

            if (table.Rows.Count > schwellenwert)
            {
                try
                {
                    breakPoint = 2 + (int)Math.Ceiling((decimal)(table.Rows.Count - 1) / 2);

                    Word.Table tableSplit = table.Split(breakPoint);

                    tableSplit.Range.Select();
                    Globals.ThisAddIn.Application.Selection.Collapse();
                    Globals.ThisAddIn.Application.Selection.InsertRowsAbove();

                    foreach (Word.Cell sourceTableCell in table.Rows[1].Cells)
                    {
                        counter++;
                        tableSplit.Cell(1, counter).Range.Text = sourceTableCell.Range.Text;
                    }

                    table.Select();
                    Globals.ThisAddIn.Application.Selection.Cut();
                    long tableHash = this.createInvisibleTable();
                    Word.Table thisTable = this.getInvisibleTable(tableHash);
                    thisTable.Cell(1, 1).Select();
                    Globals.ThisAddIn.Application.Selection.PasteAsNestedTable();

                    tableSplit.Select();
                    Globals.ThisAddIn.Application.Selection.Cut();
                    thisTable.Cell(1, 2).Select();
                    Globals.ThisAddIn.Application.Selection.PasteSpecial();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Source: \n" + e.Source);
                    Debug.WriteLine("\n\nMessage: \n" + e.Message);
                    Debug.WriteLine("\n\nStacktrace: " + e.StackTrace);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是递归函数@section scripts{ <script> $(document).ready(function () { $('#btnUpload').click(function () { UploadFile($('#uploadFile')[0].files); } ) }); function UploadFile(TargetFile) { // create array to store the buffer chunks var FileChunk = []; // the file object itself that we will work with var file = TargetFile[0]; // set up other initial vars var MaxFileSizeMB = 1; var BufferChunkSize = MaxFileSizeMB * (1024 * 1024); var ReadBuffer_Size = 1024; var FileStreamPos = 0; // set the initial chunk length var EndPos = BufferChunkSize; var Size = file.size; // add to the FileChunk array until we get to the end of the file while (FileStreamPos < Size) { // "slice" the file from the starting position/offset, to the required length FileChunk.push(file.slice(FileStreamPos, EndPos)); FileStreamPos = EndPos; // jump by the amount read EndPos = FileStreamPos + BufferChunkSize; // set next chunk length } // get total number of "files" we will be sending var TotalParts = FileChunk.length; var PartCount = 0; // loop through, pulling the first item from the array each time and sending it while (chunk = FileChunk.shift()) { PartCount++; // file name convention var FilePartName = file.name + ".part_" + PartCount + "." + TotalParts; // send the file UploadFileChunk(chunk, FilePartName); } } function UploadFileChunk(Chunk, FileName) { var FD = new FormData(); FD.append('file', Chunk, FileName); $.ajax({ type: "POST", url: '/Home/UploadFile/', contentType: false, processData: false, data: FD }); } </script> 。有必要循环遍历所有表,但此函数只等待getInvisibleTable,因此无法循环更深。解决方案如下所示:

long

我期待着让这个功能更容易使用。 (例如声明可选参数)