473,511 Members | 14,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Express C# to excel

jed
What is the best way for me to send data from C# to excel and open the
file in excel from a windows application please help.Is there an add
on i can download to gain access to the excel namespace or some other
way.thanks

Oct 10 '07 #1
3 7593

class ReportGeneration
{
Excel.Application oXL = new Excel.Application();
Excel.Workbook oBook;
Excel.Worksheet oSheet, oSheet1, oSheet2;
string fileNameMerchant = null, fileNameRecycle = null;

public ReportGeneration(string fileNameMerchant, string
fileNameRecycle)
{
this.fileNameMerchant = fileNameMerchant ;
this.fileNameRecycle = fileNameRecycle ;
}

public int MerchantReportGeneration(SqlConnection con)
{
int status=0;
oBook = oXL.Workbooks.Add(Type.Missing);
oXL.Visible = false;
try
{
if (oBook.Worksheets.Count 0)
{
oSheet = (Excel.Worksheet)oBook.Sheets[1];
oSheet.Activate();
SqlDataAdapter da = new SqlDataAdapter("select * from
table", con);
DataSet ds = new DataSet();
da.Fill(ds);
Range rg = oSheet.get_Range("A1", "B1");
rg.Select();
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 16;
rg.WrapText = true;
rg.MergeCells = true;
rg.HorizontalAlignment = Excel.Constants.xlCenter;


rg = oSheet.get_Range("B1", Type.Missing);
rg.Cells.ColumnWidth = 30;
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 10;
rg.Value2 = "columnname";


int x = 9;

int i = 0;
while (i < count)
{
string s = "A" + x.ToString();
rg = oSheet.get_Range(s.ToString(), Type.Missing);
rg.Cells.ColumnWidth = 30;
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 10;
rg.Value2 = ds.Tables[0].Rows[i][1].ToString();

string ss = "B" + x.ToString();
rg = oSheet.get_Range(ss.ToString(), Type.Missing);
rg.Cells.ColumnWidth = 40;
rg.HorizontalAlignment = Excel.Constants.xlRight;
rg.Value2 = "'" + ds.Tables[0].Rows[i][0].ToString();
x++;


}
}
fileNameMerchant = fileNameMerchant +
DateTime.Now.ToString("yyMMdd") + DateTime.Now.ToString("HHmmss");

oXL.ActiveWorkbook.SaveAs(fileNameMerchant,Excel.X lFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
System.Console.WriteLine("Merchant Report is archived at :
{0}", fileNameMerchant);
oXL.ActiveWorkbook.Close(true, fileNameMerchant, false);
status = 1;
}
catch (Exception ex)
{
string excString = ex.Message + " " + ex.Source + " " +
ex.TargetSite;
//ExceptionLogger.LogException(excString);
}

}
this is code for generating excel report throgh .net .steps are given below
1. add microsoft excell as reference from com .
2. using Excel add name space.
3. after selecting worksheet u can add as much valu as u want
here i am taking data from table and throu a for loop assingning data

u can take data from where u want

for opening excel
repeate step 1 and 2 , after defing variale. use worksheet.ope() method.

at ur sytem u must have excel installed.

"je*@auto-soft.co.za" wrote:
What is the best way for me to send data from C# to excel and open the
file in excel from a windows application please help.Is there an add
on i can download to gain access to the excel namespace or some other
way.thanks

Oct 10 '07 #2
Hi,

<je*@auto-soft.co.zawrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
What is the best way for me to send data from C# to excel and open the
file in excel from a windows application please help.Is there an add
on i can download to gain access to the excel namespace or some other
way.thanks
Well, it can be as easy es creating a CSV file and call excel with that file
as parameter
Oct 10 '07 #3
Hi ,
To refer the namespace of Excel , Add reference from com tab in the
reference . Choose Microsoft Excel 11.0 Object library or Microsoft Excel
5.0 library , whichever is available .
For more information , refer the following website :
http://www.codeproject.com/office/csharp_excel.asp

Regards,
Tara
<je*@auto-soft.co.zawrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
What is the best way for me to send data from C# to excel and open the
file in excel from a windows application please help.Is there an add
on i can download to gain access to the excel namespace or some other
way.thanks

Oct 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2009
by: Gal Zilberman | last post by:
Hi I've found a code which should work, but it doesn't. Please Help Imports Microsoft.Office.Interop ' Refence in the Class Dim oExcel As New Object Dim oBook As Object Dim oSheet As Object...
3
1854
by: sebas_cp | last post by:
I just downloaded VB 2005 Express and would like to create some financial functions to use within EXCEL through DLL´s but cannot manage to do it. ANy help would be appreciated. VB 2005 Express...
8
14357
by: danbredy | last post by:
Hi, I'm attempting to connect to an Oracle database using SQL Server 2005 Express (OS is Windows XP Professional) and having absolutely no luck. Here is the information SQL Plus gives me about...
2
1832
by: Crazy Cat | last post by:
I have an Excel add-in that connects to a SQL Server Express 2005 database. I've decided to create a configuration piece for this add-in in Visual Studio 2005 Express. I added a data connection...
2
1355
by: Schizoid Man | last post by:
Hi, I want to write some custom UDFs for Excel in C# and and build them into XLLs. However, I can't seem to check the Register for Com Interop box in the Express Edition of VC#. Is this a...
2
2019
by: =?Utf-8?B?RHVrZSBDYXJleQ==?= | last post by:
I *did* have Excel 2003 on my office PC and was fiddling with a little VB Express app that interacted with Excel - nothing complex, but it worked. Since then IT has upgraded me to Office 2007 and...
2
3066
by: Manikandan | last post by:
Hi, I have a program written in .Net Framework 1.1 using Visual studio enterprise edition 2003. I tried compiling the same program in visual c# express edition 2005. I'm getting following...
2
1477
by: Matt | last post by:
I've coded using VBA on top of MS Excel for the last 2 years (40 hours a week). I've stepped up to VS C# 2005 Express / SQL Server 2005 Express a couple months ago, but the videos that I downloaded...
0
1427
by: Blaine | last post by:
Hello, Can anyone provide me with a simple example of the following: 1.) Writing a COM interface for a subroutine placed inside a "Class Library" project using vb 2008 express edition. I want...
0
7252
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
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
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...
1
7093
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
5676
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,...
1
5077
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...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.