Sie sind auf Seite 1von 3

4/18/2014 Export DataSet into Excel using C# Excel Interop - CodeProject

http://www.codeproject.com/Reference/753207/Export-DataSet-into-Excel-using-Csharp-Excel-Inter 1/3
highlights off

10,548,809 members (69,492 online)


Not quite what you are looking for? You may want to try:
C# - Retrieve Excel Workbook Sheet Names.
Fast Exporting from DataSet to Excel


Sign in
Master The Excel 2007 VBA
intellisoft.com.sg/vba.html
Learn to Manipulate Worksheets. Automate Routine Tasks in Excel.
home quick answers discussions features community
help
C# excel
Articles Languages C# General

Article
Browse Code
Bugs / Suggestions
Stats
Revisions (12)
Alternatives
Comments
Share
About Article
This article is all about how
to save the dataset into
excel using C# excel interop
Type Reference
Licence CPOL
First Posted 1 Apr 2014
Views 1,875
Bookmarked 4 times
C# .NET Visual-Studio
Dev Beginner C#4.0 , +

Top News
Next
Rate this:
Export DataSet into Excel using C# Excel Interop
By Yugandhar Lakkaraju, 1 Apr 2014
Introduction
This article explains how to export the DataSet into Excel using C# excel interop API.
Using the code
Before starting code make sure add excel interop reference (Microsoft.Office.Interop.Excel) from add
references in visual studio.
Here I have create namespace alias Excel for the Microsoft.Office.Interop.Excel namepsace.
Usually in projects we need to export the data from DataSet to excel this would be a common task in
most of the projects in such cases we can use the below code to export the DataSet into Excel.
Collapse | Copy Code
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExportDataSetToExcel
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
//Create an Emplyee DataTable
DataTable employeeTable = new DataTable("Employee");
employeeTable.Columns.Add("Employee ID");
employeeTable.Columns.Add("Employee Name");
employeeTable.Rows.Add("1", "ABC");
employeeTable.Rows.Add("2", "DEF");
employeeTable.Rows.Add("3", "PQR");
employeeTable.Rows.Add("4", "XYZ");
//Create a Department Table
DataTable departmentTable = new DataTable("Department");
departmentTable.Columns.Add("Department ID");
departmentTable.Columns.Add("Department Name");
departmentTable.Rows.Add("1", "IT");
departmentTable.Rows.Add("2", "HR");
departmentTable.Rows.Add("3", "Finance");
//Create a DataSet with the existing DataTables
DataSet ds = new DataSet("Organization");
ds.Tables.Add(employeeTable);
ds.Tables.Add(departmentTable);
p.ExportDataSetToExcel(ds);
}
/// <summary>
/// This method takes DataSet as input paramenter and it exports the same to excel
/// </summary>
/// <param name="ds"></param>
private void ExportDataSetToExcel(DataSet ds)
{
//Creae an Excel application instance
Excel.Application excelApp = new Excel.Application();
5.00 (3 votes)
articles
4/18/2014 Export DataSet into Excel using C# Excel Interop - CodeProject
http://www.codeproject.com/Reference/753207/Export-DataSet-into-Excel-using-Csharp-Excel-Inter 2/3
C# 6: First reactions
Get the Insider News free each
morning.
Related Videos
Related Articles
How to Check Whether Excel is
Installed in the System or Not
Excel Export Component Using
XSL
Excel Connectivity in VB.NET
Reading Excel file in SQL Server
C# - Retrieve Excel Workbook
Sheet Names.
How to Convert Excel to CSV
using Interop
Displaying Charts in SharePoint
using Excel Services
BasicExcel - A Class to Read and
Write to Microsoft Excel
Fast Exporting from DataSet to
Excel
Reading and Writing Excel using
OLEDB
Convert Excel to XML file/XML
Schema, and validate Excel
against XML Schema
Excel serial date to Day, Month,
Year and vise versa
Saving Excel 2.1 Workbook
Display MS Excel Sheets and
Charts in ASPX Pages using C#
Excel XML Writer /Reader
Automating MS Excel Using
Visual Studio .NET
DataSet to Excel File Conversion
using ExcelLibrary
Upload Update Retrieve Files
(Picture/MP3/Excel/DOC etc)
to/from SQL Server
Generate Excel files without
using Microsoft Excel
Exporting in MS Excel by MS
Excel way
Working with MS Excel(xls / xlsx)
Using MDAC and Oledb
Related Research
Essential Keys to Mobile
Usability
Yugandhar Lakkaraju
Software Developer
India
I am a .NET C# developer with 3 years of experience. Knowledge sharing is my hobby.
Search this forum Go


//Create an Excel workbook instance and open it from the predefined location
Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(@"E:\Org.xlsx");
foreach (DataTable table in ds.Tables)
{
//Add a new worksheet to workbook with the Datatable name
Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
excelWorkSheet.Name = table.TableName;
for (int i = 1; i < table.Columns.Count + 1; i++)
{
excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
}
for (int j = 0; j < table.Rows.Count; j++)
{
for (int k = 0; k < table.Columns.Count; k++)
{
excelWorkSheet.Cells[j + 2, k + 1] =
table.Rows[j].ItemArray[k].ToString();
}
}
}
excelWorkBook.Save();
excelWorkBook.Close();
excelApp.Quit();
}
}
}
License
This article, along with any associated source code and files, is licensed under The Code Project Open
License (CPOL)
About the Author
Article Top
Comments and Discussions
You must Sign In to use this message board.
Profile popups Spacing Relaxed Noise Medium Layout Open All Per page 50 Update

-- There are no messages in this forum --
4/18/2014 Export DataSet into Excel using C# Excel Interop - CodeProject
http://www.codeproject.com/Reference/753207/Export-DataSet-into-Excel-using-Csharp-Excel-Inter 3/3
Permalink | Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 2 Apr 2014
Article Copyright 2014 by Yugandhar Lakkaraju
Everything else Copyright CodeProject, 1999-2014
Terms of Use
Protecting Your Business Data:
Five Dos and Donts
Best Practices for Securing Your
Private Keys and Code Signing
Certificates
Learn Agile: Ten Tips for
Launching and Testing High
Quality Apps for the American
Market
Layout: fixed | fluid

Das könnte Ihnen auch gefallen