Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Table of Contents
Excerpt

WordApplication's ImportData method allows you to import blocks of data to a worksheet from a database or a rectangular array. ImportData is a method of both Element and Table. Element.importData returns a Table object representing a table in the document that contains the imported data.Table.importData adds an new row to an existing table.

Importing from a Database

...

To import values from a database to your document:

  1. Connect to the database and execute a query to return a DataTable, DataView, SqlDataReader, OleDbDataReader, or AdomdDataReader, for example:

    Code Block
    c#
    c#
    
    private DataTable GetEmployeeDataTable()
    {
         string employeeSQL = "SELECT TOP 10 FirstName + ' ' + LastName As Name, " +
              "Title FROM Employees";
    
         DataTable dt = new DataTable();
         using(SqlConnection conn = new SqlConnection(connString))
              new SqlDataAdapter(employeeSQL, conn).Fill(dt);
    
         return dt;
    }
    
  2. Create a document:

    Code Block
    c#
    c#
    
    WordApplication wwApp = new WordApplication();
    Document doc = wwApp.Create();
    
  3. Pass the DataTable returned from the database to ImportData:

    Code Block
    c#
    c#
    
    Table employeesTable = doc.ImportData(dt);
    

Importing from an Array

  1. Create a rectangular array, for example:

    Code Block
    c#
    c#
    
    string[,] arrayData = {{"Nancy", "Davolio", "Sales Manager"},
         {"Michael", "Suyama", "HR Representative"},
         {"Adrian", "King", "IS Support"}};
    
  2. Create a document:

    Code Block
    c#
    c#
    
    WordApplication wwApp = new WordApplication();
    Document doc = wwApp.Create();
    
  3. Pass the array to ImportData:

    Code Block
    c#
    c#
    
    Table employeesTable = doc.ImportData(arrayData);
    

...

To customize a data import using a DataImportProperties object:

  1. Create a DataImportProperties object:

    Code Block
    c#
    c#
    
    WordApplication wwApp = new WordApplication();
    Document doc = wwApp.Create();
    DataImportProperties importProps = doc.CreateDataImportProperties();
    
  2. Set one or more data import properties:

    Code Block
    c#
    c#
    
    //--- Import column names to the first row of the table.
    importProps.UseColumnNames = true;
    
    //--- Automatically resize the table's columns and rows to fit
    //--- the imported data.
    importProps.AutoFit = true;
    
  3. Define a DataTable, DataView, SqlDataReader, OleDbDataReader, AdomdDataReader or rectangular array, for example:

    Code Block
    c#
    c#
    
    OleDbConnection Conn = new OleDbConnection();
    DataTable employeeDt = null;
    int employeeId;
    
    try
    {
         Conn.ConnectionString = Application["connstring"].ToString();
         //--- SQL Query for employee information
         string employeeSQL = "SELECT FirstName + ' ' + LastName As Name, Title " +
             "FROM Employees WHERE employeeID=?";
         OleDbCommand cmdEmployee = new OleDbCommand(employeeSQL, Conn);
         cmdEmployee.Parameters.Add("@employeeID", employeeID);
         OleDbDataAdapter adptEmployee = new OleDbDataAdapter(cmdEmployee);
         employeeDt = new DataTable();
         adptEmployee.Fill(employeeDt);
    }
    
  4. Pass the data and the DataImportProperties object to ImportData:

    Code Block
    c#
    c#
    
    Table importTable = doc.ImportData(employeeDt , importProps);