单词表:混合单元格宽度wpf

时间:2018-02-22 10:25:02

标签: c# ms-word

我的Word表在标题中有一些分裂的单元格。 我尝试将数据插入较低的单元格。 但我发现它在某些Pcs中起作用并在其他Pcs上崩溃 message"无法访问此集合中的各个列,因为该表具有混合的单元格宽度"堆栈跟踪:在Microsoft.Office.Interop.Word.Cell.get_Column()"  如何使用这种表? 感谢。

以下是代码中Word文档的基本设置:

    #region Create  table -->Body


            Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
            //nRows =5;//Number of rows
            int nRows = cats.Count + 1;
            Table firstTable = document.Tables.Add(para1.Range, nRows, 3, ref missing, ref missing);
            #region Style

            // object setStyle = Microsoft.Office.Interop.Word.WdTableFormat.wdTableFormatProfessional;
            object setStyle = "Table Professional ";
            firstTable.set_Style(ref setStyle);


          // object styleName = "Table Professional";
         //   object styleName = Microsoft.Office.Interop.Word.WdTableFormat.wdTableFormatProfessional;
           // firstTable.set_Style(ref styleName);
            int tcount = 0;
            bool isfirstCellInRow = false;
            firstTable.Borders.Enable = 1;
            firstTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleEngrave3D;
            firstTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleEmboss3D;
            firstTable.TableDirection = Microsoft.Office.Interop.Word.WdTableDirection.wdTableDirectionRtl;
            firstTable.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdAuto;
    //       firstTable.PreferredWidth = Microsoft.Office.Interop.Word.;
            #endregion

            int rowcount = -1;

            foreach (Row row in firstTable.Rows)
            {
                isfirstCellInRow = true;

                foreach (Cell cell in row.Cells)
                {
                    cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                    #region Fill Header ^_^
                    if (cell.RowIndex == 1)
                    {
                        //------------------------------- Set Header 
                        if (cell.RowIndex == 1 && cell.ColumnIndex == 1)
                        {
                            cell.Range.Text = "م";
                           //   cell.Column.SetWidth(33f, WdRulerStyle.wdAdjustNone);
                        }
                        else if (cell.RowIndex == 1 && cell.ColumnIndex == 2)
                        {
                            cell.Range.Text = " العميل ";
                        }
                        else if (cell.RowIndex == 1 && cell.ColumnIndex == 3)
                        {
                            cell.Range.Text = "المنطقه ";
                        }

                        cell.Range.Font.Bold = 1;
                        //other format properties goes here
                        cell.Range.Font.Name = "verdana";
                        cell.Range.Font.Size = 10;
                        //cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;                            
                        cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
                        //Center alignment for the Header cells
                        cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                        cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                    }
                    #endregion
                    //Data row
                    else
                    {
                        //if (isfirstCellInRow)
                        //{
                        //    cell.Range.Text = (rowcount + 1).ToString(); //(cell.RowIndex - 2 + cell.ColumnIndex).ToString();
                        //}
                        //else
                            switch (cell.ColumnIndex)
                            {
                                case 1:
                                    cell.Range.Text = (rowcount + 1).ToString(); //(cell.RowIndex - 2 + cell.ColumnIndex).ToString();
                                    break;
                                case 2:
                                    cell.Range.Text = cats[rowcount].Name;
                                    break;
                                case 3:
                                    cell.Range.Text = cats[rowcount].SubDistricts;
                                    break;
                                default:
                                    break;
                            }
                    }

                    isfirstCellInRow = false;
                }
                rowcount++;
                tcount++;
            }
            #endregion

1 个答案:

答案 0 :(得分:0)

只需要在表格之前添加行由于在此表格之上还有另一个表格,因此它创建的表格中没有任何空格 解决方法在表格之前添加此代码

edgesForExtendedLayout = []