/// <summary> /// 插入表格 /// </summary> /// <param name="bookmark">书签</param> /// <param name="dt">数据表</param> /// <param name="haveBorder">是否有边框</param> /// <param name="wd">自动调整方式</param> /// <returns></returns> public bool InsertTable(string bookmark, System.Data.DataTable dt, bool haveBorder, WdAutoFitBehavior wd) { return InsertTable(bookmark, dt, haveBorder, null, wd); }
/// <summary> /// 插入表格 /// </summary> /// <param name="bookmark">书签</param> /// <param name="dt">数据表</param> /// <param name="haveBorder">是否有边框</param> /// <param name="wd">自动调整方式</param> /// <returns></returns> public bool InsertTable(string bookmark, System.Data.DataTable dt, bool haveBorder, WdAutoFitBehavior wd) { return(InsertTable(bookmark, dt, haveBorder, null, wd)); }
/// <summary> /// 插入表格 /// </summary> /// <param name="dt">数据表</param> /// <param name="haveBorder">是否有边框</param> /// <param name="colWidths">边距</param> /// <param name="bookmark">标签</param> /// <param name="wd">自动调整方式</param> /// <returns></returns> public bool InsertTable(string bookmark, System.Data.DataTable dt, bool haveBorder, double[] colWidths, WdAutoFitBehavior wd) { try { object oBookmark = bookmark;//WORD中预定义的书签 Range tableLocation = oDoc.Bookmarks.get_Item(ref oBookmark).Range;//获取当前文档的书签位置。 //oWord.Selection.GoTo(oBookmark); // Range tableLocation2 = oWord.Selection.Range; object Nothing = System.Reflection.Missing.Value; tableLocation.InsertParagraphAfter();//插入一个段落,在此段落上插入一个2行一列的表格。 // Microsoft.Office.Interop.Word.Table newTable = oDoc.Tables.Add(tableLocation, 2, 1, ref Nothing, ref Nothing); //添加Word表格 Microsoft.Office.Interop.Word.Table table = oDoc.Tables.Add(tableLocation, dt.Rows.Count, dt.Columns.Count, ref Nothing, ref Nothing); if (colWidths != null) { for (int i = 0; i < colWidths.Length; i++) { table.Columns[i + 1].Width = (float)(28.5F * colWidths[i]); } } ///设置TABLE的样式 table.AllowAutoFit = true; table.AutoFitBehavior(wd);//设置自动调整方式 table.Rows.HeightRule = Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast; //table.Rows.Height = oWord.CentimetersToPoints(float.Parse("0.8")); table.Range.Font.Size = 9.0F; table.Range.Font.Name = "宋体"; table.Range.Font.Bold = 0; table.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; table.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; table.Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; if (haveBorder == true) { //设置外框样式 table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; //样式设置结束 } for (int row = 0; row < dt.Rows.Count; row++) { for (int col = 0; col < dt.Columns.Count; col++) { table.Cell(row + 1, col + 1).Range.Text = dt.Rows[row][col].ToString(); } } return true; } catch (Exception e) { // MessageBox.Show(e.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
/// <summary> /// 插入表格 /// </summary> /// <param name="dt">数据表</param> /// <param name="haveBorder">是否有边框</param> /// <param name="colWidths">边距</param> /// <param name="bookmark">标签</param> /// <param name="wd">自动调整方式</param> /// <returns></returns> public bool InsertTable(string bookmark, System.Data.DataTable dt, bool haveBorder, double[] colWidths, WdAutoFitBehavior wd) { try { object oBookmark = bookmark; //WORD中预定义的书签 Range tableLocation = oDoc.Bookmarks.get_Item(ref oBookmark).Range; //获取当前文档的书签位置。 //oWord.Selection.GoTo(oBookmark); // Range tableLocation2 = oWord.Selection.Range; object Nothing = System.Reflection.Missing.Value; tableLocation.InsertParagraphAfter();//插入一个段落,在此段落上插入一个2行一列的表格。 // Microsoft.Office.Interop.Word.Table newTable = oDoc.Tables.Add(tableLocation, 2, 1, ref Nothing, ref Nothing); //添加Word表格 Microsoft.Office.Interop.Word.Table table = oDoc.Tables.Add(tableLocation, dt.Rows.Count, dt.Columns.Count, ref Nothing, ref Nothing); if (colWidths != null) { for (int i = 0; i < colWidths.Length; i++) { table.Columns[i + 1].Width = (float)(28.5F * colWidths[i]); } } ///设置TABLE的样式 table.AllowAutoFit = true; table.AutoFitBehavior(wd);//设置自动调整方式 table.Rows.HeightRule = Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast; //table.Rows.Height = oWord.CentimetersToPoints(float.Parse("0.8")); table.Range.Font.Size = 9.0F; table.Range.Font.Name = "宋体"; table.Range.Font.Bold = 0; table.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; table.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; table.Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; if (haveBorder == true) { //设置外框样式 table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; //样式设置结束 } for (int row = 0; row < dt.Rows.Count; row++) { for (int col = 0; col < dt.Columns.Count; col++) { table.Cell(row + 1, col + 1).Range.Text = dt.Rows[row][col].ToString(); } } return(true); } catch (Exception e) { // MessageBox.Show(e.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }