By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Creating an Excel document from .NET + C#

P: n/a
Hi all,

In an effort to try to impress the boss, I would like to log accounting information to an Excel spreadsheet using C# and .NET. This would be a mockup only, not a real solution - yet!

We are a small ticketing company - not a bunch of IT whiz kids. ...but I do have an IT background.

I have been successful in logging (printing) tickets to a word document, and it all works kinda cool.
However, I wish to log to same information to an Excel document. For example, I want to log each ticket on a new row of a spreadsheet. Alas, writing to an excel document is not as easy as writing to Word.

So I would need to (1) create a new Excel document, if it did not exist, (2) Write the ticket information on the NEXT row of a spreadsheet, (3) Save and close the file. I am looking at writing a new function/method called "public void Log" to do this.

How would I do this? Can someone provide an example of how to create and save a new Excel document? And then write data to the rows?

Thankyou so very much in advance!

Ticketing printing code using .NET and C#:

class Ticket

{

public Event e;

public String Section;

public String Type;

public String Price;

public String Comment;



private Word.ApplicationClass WordApp = null;

private Word.Document Paper = null;



public void Open()

{

if (WordApp == null)

{

object Missing = System.Reflection.Missing.Value;

object FileName = "normal.dot";

object NewTemplate = false;

object DocType = 0;

object isVisible = true;



WordApp = new Word.ApplicationClass();

Paper = WordApp.Documents.Add(ref FileName, ref NewTemplate, ref DocType, ref isVisible);



WordApp.Visible = true;



Paper.Activate();

}



return;

}



public void Close()

{

if (WordApp != null)

{

object Missing = System.Reflection.Missing.Value;

object FileName = "ticket.doc";



Paper.SaveAs(ref FileName, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);



Paper.Close(ref Missing, ref Missing, ref Missing);

WordApp.Quit(ref Missing, ref Missing, ref Missing);



WordApp = null;

}



return;

}



public void Print()

{

if (WordApp != null)

{

WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

WordApp.Selection.Font.Bold = (int) Word.WdConstants.wdToggle;

WordApp.Selection.TypeText("Ticket Information");



InsertBlankLines(1);



WordApp.Selection.Font.Bold = (int) Word.WdConstants.wdToggle;

WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

WordApp.Selection.TypeText("\nEvent:\t" + e.Name);

WordApp.Selection.TypeText("\nTheatre:\t" + e.Venue);

WordApp.Selection.TypeText("\nDate:\t" + e.DateAndTime);

WordApp.Selection.TypeText("\nSection:\t" + Section);

WordApp.Selection.TypeText("\nType:\t" + Type); WordApp.Selection.TypeText("Price:\t" + Price);

WordApp.Selection.TypeText("\nAdditional Information: " + Comment);

WordApp.Selection.TypeText("\nDate Printed:\t"); InsertTimeDate();



InsertBlankLines(2);

}



return;

}



private void InsertBlankLines(int number)

{

if (WordApp != null)

{

int iCount = 0;



for (iCount = 0; iCount < number; ++iCount)

{

WordApp.Selection.TypeParagraph();

}

}



return;

}



private void InsertTimeDate()

{

if (WordApp != null)

{

object Missing = System.Reflection.Missing.Value;

object False = false;

object objDate = "dddd, MMMM dd, yyyy at HH:mm:ss";



WordApp.Selection.InsertDateTime(ref objDate, ref False, ref Missing, ref Missing, ref Missing);

WordApp.Selection.TypeParagraph();

}



return;

}

}

Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.