大众计算机学习网

专门提供各种电脑教程

aspose.words插入表格的代码

Admin | 2015-8-27 10:22:49 | 被阅次数 | 3236

public bool InsertTable(System.Data.DataTable dt, bool haveBorder) 
        { 
            Aspose.Words.Tables.Table  table= oWordApplic.StartTable();//开始画Table 
            ParagraphAlignment paragraphAlignmentValue = oWordApplic.ParagraphFormat.Alignment; 
            oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center; 
            //添加Word表格 
            for (int row = 0; row < dt.Rows.Count; row++) 
            { 
                 oWordApplic.RowFormat.Height =25; 
                for (int col = 0; col < dt.Columns.Count; col++) 
                { 
                    oWordApplic.InsertCell(); 
                    oWordApplic.Font.Size = 10.5; 
                    oWordApplic.Font.Name = "宋体"; 
                    oWordApplic.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐 
                    oWordApplic.ParagraphFormat.Alignment = ParagraphAlignment.Center;//水平居中对齐 
                    oWordApplic.CellFormat.Width = 50.0; 
                    oWordApplic.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50); 
                    if (haveBorder == true) 
                    { 
                        //设置外框样式    
                        oWordApplic.CellFormat.Borders.LineStyle = LineStyle.Single; 
                        //样式设置结束    
                    } 
 
                    oWordApplic.Write(dt.Rows[row][col].ToString()); 
                } 
 
                oWordApplic.EndRow(); 
 
            } 
            oWordApplic.EndTable(); 
            oWordApplic.ParagraphFormat.Alignment = paragraphAlignmentValue; 
            table.Alignment=Aspose.Words.Tables.TableAlignment.Center; 
            table.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto;  
            return true; 
        }