473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to pass datatable from windows application to excel sheet by using c# coding?

19
Hi....

i want to pass datatable from windows application to excel sheet.im using c# language

if any one know this.... help me
Aug 2 '10 #1
4 5555
PsychoCoder
465 Expert Mod 256MB
If you're wanting to convert a DataTable to Excel first you can add a reference to the Office interops

Expand|Select|Wrap|Line Numbers
  1. using Microsoft.Office.Interop.Excel
  2. using Microsoft.Office.Interop
  3.  
Then it's just a matter of creating your Excel application, then your Workbook and a Worksheet inside the Worksheet. You then loop through each row in your DataTable adding the values to your worksheet.

Here's an example

Expand|Select|Wrap|Line Numbers
  1. public bool ExportDataToExcel(System.Data.DataTable data, string worksheetName, string saveAs)
  2. {
  3.     Excel.Application excel;
  4.     Excel.Workbook workBook;
  5.     Excel.Worksheet sheet;
  6.     Excel.Range range;
  7.  
  8.     try
  9.     {
  10.         // Start Excel and get Application object.
  11.         excel = new Excel.Application();
  12.  
  13.         // here we make sure Excel doesnt show while we're working with it
  14.         excel.Visible = false;
  15.         excel.DisplayAlerts = false;
  16.  
  17.         // create a new Workbook
  18.         workBook = excel.Workbooks.Add;
  19.  
  20.         // get the first active sheet from the workbook
  21.         sheet = (Excel.Worksheet)workBook.ActiveSheet;
  22.         sheet.Name = worksheetName;
  23.  
  24.         // loop through each row and add values to our sheet
  25.         int count = 1;
  26.         foreach (DataRow row in data.Rows)
  27.         {
  28.             count += 1;
  29.             for (int i = 1; i <= data.Columns.Count; i++)
  30.             {
  31.                 // on the first iteration we add the column headers
  32.                 if (count == 2)
  33.                 {
  34.                     sheet.Cells(1, i) = data.Columns[i - 1].ColumnName;
  35.                 }
  36.                 sheet.Cells(count, i) = row[i - 1].ToString();
  37.             }
  38.  
  39.         }
  40.  
  41.         // now we resize the columns
  42.         range = sheet.Range(sheet.Cells(1, 1), sheet.Cells(count, data.Columns.Count));
  43.         range.EntireColumn.AutoFit();
  44.  
  45.         //now save the workbook and exit Excel
  46.         workBook.SaveAs(saveAs);
  47.         workBook.Close();
  48.         excel.Quit();
  49.         return true;
  50.     }
  51.     catch (Exception ex)
  52.     {
  53.         MessageBox.Show(ex.Message);
  54.         return false;
  55.     }
  56.     finally
  57.     {
  58.         sheet = null;
  59.         range = null;
  60.         workBook = null;
  61.     }
  62. }
  63.  
Hope that helps ;)
Aug 2 '10 #2
vidhyaG
19
hi,

thanks for your reply....

i used your code...but it makes error...

error occurring in line 18,46,47...

i am developing a project in windows application by using c# language....

and than i want to pass a datatable to excel and have to draw chart from that...(ex marksheet)

if you know. help me..
Aug 3 '10 #3
Frinavale
9,735 Expert Mod 8TB
Do you have Excel installed on the machine where you are trying to execute the application?

What is the error message?

-Frinny
Aug 3 '10 #4
vidhyaG
19
yes i have installed Ms Excel.

i am doing this in windows application using c# language.i am creating student mark list.so if i cal any function it will show that marklist in excel sheet and have to disply chart for that.

1)In line No 18. workBook = excel.Workbooks.Add;

Error:43 Cannot convert method group 'Add' to non-delegate type Microsoft.Office.Interop.Excel.Workbook'. Did you intend to invoke the method?

2)In Line no 42. range = sheet.Range(sheet.Cells[1, 1], sheet.Cells[count, data.Columns.Count]);

Error 44:Property, indexer, or event 'Range' is not supported by the language; try directly calling accessor method

3)workBook.SaveAs(saveAs)

Error 45:No overload for method 'SaveAs' takes '1' arguments

4)workBook.Close()
Error 46:No overload for method 'Close' takes '0' arguments
Aug 5 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: B.N.Prabhu | last post by:
How to delete first row from an Excel Sheet using C#.Net. Please Help. Its very urgent.
5
by: Rishika14 | last post by:
Hi, Somebody pls tell me how to export data from webpage to excel sheet using java script. rishika
1
by: gowthamkumar | last post by:
HII ... please give me a code to open a new excel sheet using VB so that i can also directly add some contents in the cells of the excel sheet in VB itself .. i ve use the existing excel sheet code...
4
by: sandeep123456 | last post by:
hi, can we send data from html to excel sheet using java script sandeep
2
by: Ch Pravin | last post by:
Hi All: I am having the following xml which i need to convert to excel using xslt. Please help me out. Afghanistan.xml <?xml version="1.0" encoding="utf-16"?> <Languages...
2
by: abiramii | last post by:
Hi How to Import datagridview datas to excel sheet using c#.net(win forms)
3
by: dileepkms | last post by:
can we disable copy and paste functionality for excel sheet using javascript? if we do it, can you send me the code
2
by: sanjuindia2005 | last post by:
How can i read the excel sheet using javascript ?
4
by: =?Utf-8?B?Sm9zaW4gSm9obg==?= | last post by:
I could create MS Excel sheet using ASP.NET 2.0 with C# but it is not being created in some systems, following error occurs when the program compiles : Microsoft Office Excel cannot open or...
1
by: sree ram | last post by:
code for getting maximum rows in excel sheet using perl script
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.