Use code samples below to get data from Data Table and import them to Excel table (XLS spreadsheet) in C# and Visual Basic .NET using Bytescout Spreadsheet SDK.
C#
using System.Data; using System.Diagnostics; using System.IO; namespace Bytescout.Spreadsheet.Demo.Csharp.ImportFromDataTable { class Program { static void Main(string[] args) { const string fileName = "CSharpImportFromDataTable.xls"; // Create a new spreadsheet Spreadsheet spreadsheet = new Spreadsheet(); // Get the data from the datatable that we want to import DataTable periodicTable = GetDataTable(); // Import data into spreadheet spreadsheet.ImportFromDataTable(periodicTable); // Save the spreadsheet if (File.Exists(fileName)) File.Delete(fileName); spreadsheet.SaveAs(fileName); // Close spreadsheet spreadsheet.Close(); // Open the spreadsheet Process.Start(fileName); } /// <summary> /// Creates a data table of the periodic table of elements /// </summary> /// <returns>A data table of the periodic table of elements</returns> private static DataTable GetDataTable() { DataTable periodicTable = new DataTable("PeriodicTable"); periodicTable.Columns.Add("Name", typeof(string)); periodicTable.Columns.Add("Symbol", typeof(string)); periodicTable.Columns.Add("AtomicNumber", typeof(int)); DataRow dr = periodicTable.Rows.Add(); dr[0] = "Hydrogen"; dr[1] = "H"; dr[2] = "1"; dr = periodicTable.Rows.Add(); dr[0] = "Helium"; dr[1] = "He"; dr[2] = "2"; dr = periodicTable.Rows.Add(); dr[0] = "Lithium"; dr[1] = "Li"; dr[2] = "3"; dr = periodicTable.Rows.Add(); dr[0] = "Beryllium"; dr[1] = "Be"; dr[2] = "4"; dr = periodicTable.Rows.Add(); dr[0] = "Boron"; dr[1] = "B"; dr[2] = "5"; dr = periodicTable.Rows.Add(); dr[0] = "Carbon"; dr[1] = "C"; dr[2] = "6"; return periodicTable; } } }
VB.NET
Imports System.IO Module Module1 Sub Main() Const fileName As String = "VBNetImportFromDataTable.xls" 'Create a new spreadsheet Dim spreadsheet As Bytescout.Spreadsheet.Spreadsheet = New Bytescout.Spreadsheet.Spreadsheet 'Get the data from the 2D array that we want to import Dim periodicTable As DataTable = GetDataTable() 'Import data into spreadheet spreadsheet.ImportFromDataTable(periodicTable) 'Save the spreadsheet If (File.Exists(fileName)) Then File.Delete(fileName) End If spreadsheet.SaveAs(fileName) 'Close spreadsheet spreadsheet.Close() 'Open the spreadsheet Process.Start(fileName) End Sub ''' <summary> ''' Creates a data table of the periodic table of elements ''' </summary> ''' <returns>A data table of the periodic table of elements</returns> Function GetDataTable() As DataTable Dim periodicTable = New DataTable("PeriodicTable") Dim dr As DataRow periodicTable.Columns.Add("Name", GetType(String)) periodicTable.Columns.Add("Symbol", GetType(String)) periodicTable.Columns.Add("AtomicNumber", GetType(Integer)) dr = periodicTable.Rows.Add() dr(0) = "Hydrogen" dr(1) = "H" dr(2) = "1" dr = periodicTable.Rows.Add() dr(0) = "Helium" dr(1) = "He" dr(2) = "2" dr = periodicTable.Rows.Add() dr(0) = "Lithium" dr(1) = "Li" dr(2) = "3" dr = periodicTable.Rows.Add() dr(0) = "Beryllium" dr(1) = "Be" dr(2) = "4" dr = periodicTable.Rows.Add() dr(0) = "Boron" dr(1) = "B" dr(2) = "5" dr = periodicTable.Rows.Add() dr(0) = "Carbon" dr(1) = "C" dr(2) = "6" Return periodicTable End Function End Module