View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
David David is offline
external usenet poster
 
Posts: 2
Default Displaying a dataset or datatable in Excel

There's something I think ought to be easy, but I can't find a way to do it.
Either there's something wrong with the way I'm searching, or there's a
reason it can't be done easily.

What I want is to create an excel addin that, when the user opens a
spreadsheet , it goes out and fetches some data from a database, and displays
it in a table. Pretty straightforward. Here's some code:


void Application_WorkbookOpen(Microsoft.Office.Interop. Excel.Workbook wb)
{//I've also attached this to other events. The "open" event isn't
the key part. It doesn't do anything in any event.

string connstring="Integrated Security=SSPI;
database=MyInventoryDB; Data Source=MyServer\\MyInventory";

System.Data.SqlClient.SqlDataAdapter da=new
System.Data.SqlClient.SqlDataAdapter("Select * from
PlantInventory",connstring);
System.Data.DataSet ds=new System.Data.DataSet();
da.Fill(ds);
Excel.Worksheet
activeWorksheet=((Excel.Worksheet)Application.Acti veSheet);
Excel.Range
inventoryrange=activeWorksheet.get_Range("C6",miss ing);
//Here's the line I know doesn't work, but I think something
sort of like it ought to.
inventoryrange.Value2=ds.Tables[0];
//or maybe create a range and set a datasource.
}

So, the key is that I want to use a range of cells that I define sort of
like I would use a datagridview. I want to fetch a table, as above, and then
tell the sheet to display the data in a given range.

When googling, I seem to find people who have iterated through each row in
the returned datatable, and each column within the row, and filled in one
cell at a time. I know how to do that, but it seems like this would be so
commonly requested that there ought to be a simple, one or two step command
to make this happen. Any suggestions?