How to fill values in an Excel spreadsheet from c# ?

  • Thread starter Thread starter fmarchioni
  • Start date Start date
F

fmarchioni

Hi all,
I'm just beginning with C#.
I have an Excel spreadsheet which I use to print invoices for my
company.
I'd like to fill some Excel's rows from my Visual C# project.
Which is the easiest way to do it ?
Thanks a lot
Francesco
 
There are several approaches determined by the environemnt your code is
running in and the level of flexibility you need.

If you're writing a c# windows app you could use COM to open an excel
session and dump the data in.

If you're writing a c# web app without excel on the server the simplest
approach is to strip the page of all formatting, change the mime type to
excel and stream the results. There are numerous excellent articles on
the web - do a search for datagrid and excel in google.

Regards,

Chris
 
Hello Chris,
thanks for your answer.
What I need is to write a c# widnwos app. Have you (or has anybody) got
a minimalist sample how to communicate with excel using COM ?
Thanks a lot
Francesco
 
I don't have anything readily to hand but there are numerous examples on
the microsoft site. I'm not sure how complex the excel spreadsheet is
you're trying to create. If its really simple you could save your
dataset as XML / csv and just open that up in Excel depending on which
version of Excel you're using.
 
Francesco -

I've recently completed a very simple Windows form application to
automate data loading with C# and Excel {open XLS, change date, update
XLS, save, load data into SQL Server, repeat}.

Here are the important Excel related statements assuming you've got the
PIA off of MSDN, installed and registered it (all part of the
directions).

object optional = Missing.Value;
object updateLinks = 0;
Microsoft.Office.Interop.Excel._Workbook wkb = app.Workbooks.Open
(fullname, updateLinks, optional, optional, optional, optional,
optional, optional, optional, optional, optional, optional, optional,
optional, optional);
Microsoft.Office.Interop.Excel._Worksheet wks =
(_Worksheet)wkb.Worksheets.get_Item(MySheet.Text);
wks.Activate();
wks.Visible = XlSheetVisibility.xlSheetVisible;
Range wksRange;
wksRange = wks.get_Range("H7", "H7");
wksRange.set_Value(Missing.Value, workingDate);
app.Calculate();
wkb.Save();
wkb.Close(false,Missing.Value,false);
app.Quit();


If this is too cryptic, drop me an email and I'll provide more detail.
Terry
 
Back
Top