Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Very Easy to Use Excel XML Import-Export Library

, 25 Nov 2008
Import export library for the Excel XML format, which reduces a programmer's work to the bare minimum.
Documentation.zip
ExcelXml.zip
ExcelXml
ExcelXmlTest
bin
Debug
ExcelXmlTest.exe
ExcelXmlTest.vshost.exe
Yogesh.ExcelXml.dll
Yogesh.Extensions.dll
obj
Debug
TempPE
Properties
Yogesh.ExcelXml
bin
Debug
Yogesh.ExcelXml.dll
Yogesh.Extensions.dll
Help
Documentation.chm
LastBuild.log
obj
Debug
Refactor
TempPE
Release
Properties
Extensions.zip
Extensions
Yogesh.Extensions
bin
Debug
Yogesh.Extensions.dll
Properties
Yogesh.Extensions.zip
using System;
using System.Data;
using System.Globalization;

namespace Yogesh.ExcelXml
{
	partial class ExcelXmlWorkbook
	{
		/// <summary>
		/// Converts a dataset to a work book
		/// </summary>
		/// <param name="source">The source dataset to convert to a work book</param>
		/// <returns>Returns the <see cref="Yogesh.ExcelXml.ExcelXmlWorkbook"/>
		/// for the dataset.</returns>
		/// <remarks>All the tables are converted into sheets with sheet names as table + table number, 
		/// eg. "Table0" "Table1" etc. Supported types which can be successfully
		/// converted to cells are the same as described in <see cref="Yogesh.ExcelXml.Cell"/>
		/// except <see cref="Yogesh.ExcelXml.Cell"/> and 
		/// <see cref="Yogesh.ExcelXml.Formula"/></remarks>
		public static ExcelXmlWorkbook DataSetToWorkbook(DataSet source)
		{
			ExcelXmlWorkbook book = new ExcelXmlWorkbook();

			for (int tableNumber = 0; tableNumber < source.Tables.Count; tableNumber++)
			{
				Worksheet sheet = book[tableNumber];
				sheet.Name = "Table" + tableNumber.ToString(CultureInfo.InvariantCulture);

				int columnCount = source.Tables[tableNumber].Columns.Count;
				for (int columnNumber = 0; columnNumber < columnCount; columnNumber++)
				{
					sheet[columnNumber, 0].Value = source.Tables[tableNumber].Columns[columnNumber].ColumnName;

					sheet[columnNumber, 0].Font.Bold = true;
				}

				int rowNumber = 0;
				foreach (DataRow row in source.Tables[tableNumber].Rows)
				{
					rowNumber++;

					for (int columnNumber = 0; columnNumber < columnCount; columnNumber++)
					{
						string rowType = row[columnNumber].GetType().FullName;

						switch (rowType)
						{
							case "System.DateTime":
								{
									sheet[columnNumber, rowNumber].Value = (DateTime)row[columnNumber];
									break;
								}
							case "System.Boolean":
								{
									sheet[columnNumber, rowNumber].Value = (bool)row[columnNumber];
									break;
								}
							case "System.SByte":
							case "System.Int16":
							case "System.Int32":
							case "System.Int64":
							case "System.Byte":
							case "System.UInt16":
							case "System.UInt32":
							case "System.UInt64":
							case "System.Single":
							case "System.Double":
							case "System.Decimal":
								{
									sheet[columnNumber, rowNumber].Value = Convert.ToDecimal(row[columnNumber],
										CultureInfo.InvariantCulture);
									break;
								}
							case "System.DBNull":
								break;
							// case "System.String": <-- default covers this...
							default:
								{
									sheet[columnNumber, rowNumber].Value = row[columnNumber].ToString();
									break;
								}
						}
					}
				}
			}

			return book;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 26 Nov 2008
Article Copyright 2008 by Yogesh Jagota
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid