解析word中的表格

 因爲word表格的特殊性,其自己中的數據原本就不夠完善,不可以很好的知道其具體的合併、跨行的相關屬性,表格的單位多是PT或者是百分比,而且是共存的,爲處理帶來了必定的負擔,本代碼實現了一個將Word表格解析爲XMLTable。spa

 因爲沒找到上傳附件的位置,僅提供部分代碼,有須要的可email我(提供聯繫方式)或進羣:490571636,我會提供所有代碼。對象

/// <summary>         /// 分析表格經過位置跨度         /// </summary>         /// <param name="table">word表格對象</param>         /// <returns>自定義的表格對象</returns>         public static WordTable ParserTableByPositionSpan(Word.Table table)         {             List<double> positionList = new List<double>();             WordTable wordTable = new WordTable();it

            wordTable.RowCount = table.Rows.Count;             wordTable.ColCount = table.Columns.Count;             positionList.Add(0);             double tableWidth = 0;             //獲取全部行中的單元格及位置列表 若是有的列沒有獲取,說明沒有或被上邊的單元格合併(只有上邊被合併的列纔會)             for (var row = 1; row <= table.Rows.Count; row++)             {                 WTRow currRow = wordTable.AddRow();                 currRow.RowIndex = row;                 double leftPosition = 0;                 for (var col = 1; col <= table.Columns.Count; col++)                 {                     Word.Cell cell = null;                     try                     {                         //獲取指定位置的單元格,若是沒有會觸發異常                         cell = table.Cell(row, col);                     }                     catch (Exception e)                     {                         //System.Diagnostics.Trace.                     }                                         //指定的位置有單元格則直接添加到當前行                     if (cell != null)                     {                         //第一行的寬度確定會有值不會了現9999999的狀況                         if (row == 1)                         {                             tableWidth += cell.Width;                         }                                                                         WTCol wTCol = currRow.AddCol();                         wTCol.Value = GetRangeParagraphs(cell.Range);io

                        double width = cell.Width;                                                 //cell的寬度有三種形式                         //按內容展現(磅值)Word.WdPreferredWidthType.wdPreferredWidthAuto 屬性width有值 PreferredWidth爲0                         //固定寬度(磅值) Word.WdPreferredWidthType.wdPreferredWidthPoints 屬性width有值 PreferredWidth有時爲9999999                         //匹配窗口(百分比)Word.WdPreferredWidthType.wdPreferredWidthPercent 屬性width有時爲9999999 PreferredWidth爲百分比                         switch (cell.PreferredWidthType)                         {                             case Word.WdPreferredWidthType.wdPreferredWidthAuto:                                 width = cell.Width;                                 break;                             case Word.WdPreferredWidthType.wdPreferredWidthPoints:                                 ///9999999爲百分比或磅值時  取PreferredWidth的寬度                                 if (width == 9999999)                                 {                                     width = cell.PreferredWidth;                                 }                                 break;                             case Word.WdPreferredWidthType.wdPreferredWidthPercent:                                 ///9999999爲百分比或磅值時  width和PreferredWidth所對應的寬度                                 if (width == 9999999)                                 {                                     width = GetPercentWidth(tableWidth, cell.PreferredWidth);                                 }                                 break;                         }                                                 wTCol.Width = width;                         wTCol.RealCol = col;                         wTCol.Left = leftPosition;                         leftPosition += width;                         AddNewPosition(positionList, leftPosition);                         continue;                     }table

                    //WTCol prevRowWTCol1 = wordTable.GetPreviousRowRefCol(row, col);                     //WTCol prevRowWTCol2 = wordTable.GetPreviousRowRefColByLeft(row, leftPosition);                     WTCol prevRowWTCol1 = wordTable.GetPreviousRowRefColByLeft(row, leftPosition);                                         //第一列 若是沒有單元格說明被上邊的行給合併了                     if (prevRowWTCol1 != null)                     {                         //if (prevRowWTCol1.Left + prevRowWTCol1.Width > leftPosition)                         {                             prevRowWTCol1.RowSpan += 1;                         }                         leftPosition += prevRowWTCol1.Width;                         continue;                     }                                        //if (prevRowWTCol2 != null)                     //{email

                    //}                     //else                 }             }foreach

            wordTable.ColCount = positionList.Count - 1;List

            //矯正列合併處理 因爲列合併不會產生空的單元格,以列寬判斷列合併狀況(默認均爲合併1列)             foreach (var row in wordTable.Rows)             {                 //int realColCount = 1;                 foreach (var col in row.Cols)                 {                     int colSpan = GetPositionSpan(positionList, col.Left, col.Left + col.Width);數據

                    col.ColSpan = colSpan;                     col.RealCol = GetRealColByPositionList(positionList, col.Left); //realColCount;                     //realColCount += colSpan;                     //realColCount                 }             }異常

            return wordTable;         }

相關文章
相關標籤/搜索