如何使用word互操作将dataTable导出为word

本文关键字:word dataTable 何使用 互操作 | 更新日期: 2023-09-27 17:53:11

是否可以使用word互操作将数据表导出到word ?我的意图是在不使用第三方库的情况下将数据表导出到word。这可能吗?

如何使用word互操作将dataTable导出为word

当单击Button时,下面的代码将在代码中创建的自定义DataTable导出为Microsoft Office Word文档。

using Microsoft.Office.Interop.Word;
using System;
using System.Data;
using System.Windows.Forms;
using DataTable = System.Data.DataTable;
using Word = Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using Document = Microsoft.Office.Interop.Word.Document;
using WdWindowState = Microsoft.Office.Interop.Word.WdWindowState;
using WdColor = Microsoft.Office.Interop.Word.WdColor;
namespace StackOverflowCode
{
    public partial class StackOverflow : Form
    {
        public StackOverflow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Creating a sample DataTable - same can be queried from a database
            DataTable dataTable = new DataTable();
            dataTable.Clear();
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Marks");
            DataRow dataRow0 = dataTable.NewRow();
            DataRow dataRow1 = dataTable.NewRow();
            DataRow dataRow2 = dataTable.NewRow();
            dataRow0["Name"] = "Robert";
            dataRow0["Marks"] = "100";
            dataTable.Rows.Add(dataRow0);
            dataRow1["Name"] = "Method";
            dataRow1["Marks"] = "97";
            dataTable.Rows.Add(dataRow1);
            dataRow2["Name"] = "Karamagi";
            dataRow2["Marks"] = "92";
            dataTable.Rows.Add(dataRow2);
            //Opening the Word Document
            Application application = new Application();
            Document document = application.Documents.Add();
            application.Visible = true;
            application.WindowState = WdWindowState.wdWindowStateMaximize;

            //Counting the DataTaable Rows and Column to Export
            int RowCount = dataTable.Rows.Count;
            int ColumnCount = dataTable.Columns.Count;
            //Creating the Table DataArray to Export
            Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
            int r = 0;
            for (int c = 0; c <= ColumnCount - 1; c++)
            {
                DataArray[r, c] = dataTable.Columns[c].ColumnName;
                for (r = 0; r <= RowCount - 1; r++)
                {
                    DataArray[r, c] = dataTable.Rows[r][c];
                } //end row loop
            } //end column loop
            dynamic range = document.Content.Application.Selection.Range;
            String temp = "";
            for (r = 0; r <= RowCount - 1; r++)
            {
                for (int c = 0; c <= ColumnCount - 1; c++)
                {
                    temp = temp + DataArray[r, c] + "'t";
                }
            }
            range.Text = temp;
            //Basic Table Styling and Exporting
            object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs;
            object Format = Word.WdTableFormat.wdTableFormatWeb1;
            object ApplyBorders = false;
            object AutoFit = true;
            object ApplyHeadingRows = true;
            object DefaultTableBehavior = Word.WdDefaultTableBehavior.wdWord9TableBehavior;
            object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
            range.ConvertToTable(ref Separator,
                ref RowCount, ref ColumnCount, Type.Missing, ref Format,
                ref ApplyBorders, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, ref ApplyHeadingRows,
                    Type.Missing, ref AutoFit, ref AutoFitBehavior,
                    ref DefaultTableBehavior);
            range.Select();
            document.Application.Selection.Tables[1].Select();
            document.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
            document.Application.Selection.Tables[1].Rows.Alignment = 0;
            document.Application.Selection.Tables[1].Rows[1].Select();
            document.Application.Selection.InsertRowsAbove(1);
            document.Application.Selection.Tables[1].Rows[1].Select();
            document.Application.Selection.Tables[1].Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            document.Application.Selection.Tables[1].Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;

            //inserting the Header Row
            for (int c = 0; c <= ColumnCount - 1; c++)
            {
                document.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = dataTable.Columns[c].ColumnName;
                //Header Row Font Color and Background Color
                document.Application.Selection.Tables[1].Cell(1, c + 1).Range.Font.Color = WdColor.wdColorWhite;
                document.Application.Selection.Tables[1].Cell(1, c + 1).Range.Shading.BackgroundPatternColor = WdColor.wdColorBlue;
            }
        }
    }
}