|
-
Sep 26th, 2006, 08:21 PM
#1
Thread Starter
PowerPoster
dataset exporting
how is it possible to export a dataset to an excel file? This is using WinForms, not ASP.NET - I know its much easier to do in ASP.NET
I would like to avoid the solution of using a COM interop (however the solution to it would be appreciated for my knowledge on how to open/close/insert data into a workbook/worksheet) but use OleDbConnection if at all possible
Thanks
-
Sep 26th, 2006, 08:36 PM
#2
Re: dataset exporting
Using the Excel Object Model from my Office Dev FAQ -
Automate Excel with C# (Early binding and Late binding examples) (Posts #2 and #3)
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 26th, 2006, 08:37 PM
#3
Thread Starter
PowerPoster
Re: dataset exporting
awesome, good job and many thanks. I would also still like to here other methods too but no doubt I will learn heck of a lot from that article!
-
Sep 26th, 2006, 08:40 PM
#4
Re: dataset exporting
Thanks. I would have posted the other method but it would take me a bit to write one up. So I figured the link would be good for now.
I have the main index link in my signature with many VB.NET and C# examples for Office apps. 
Ps, I dont think I have ever seen a Late binding C# code example around anywhere before.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 26th, 2006, 08:54 PM
#5
Thread Starter
PowerPoster
Re: dataset exporting
first time for everything :P
I get errors, well, COM exception when creating a workbook/worksheet
Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154.
I do have Office 2003 installed with Excel and added the Excel COM component also as a reference.
I just want to take data from the dataset and "shove" it into an excel worksheet, being it via OleDb or COM - preferably OleDb
-
Sep 26th, 2006, 08:54 PM
#6
Re: dataset exporting
You can use ADO.NET with Excel files via OleDb, which negates the need for Excel to be installed. You obviously don't have all the Excel functionality that you do via Automation but if it's just getting data in and out of workbooks then ADO.NET is probably easier anyway.
-
Sep 26th, 2006, 09:12 PM
#7
Thread Starter
PowerPoster
Re: dataset exporting
but how would I go about actually doing it if I have a dataset?
I have:
dataset with records
a worksheet file (book1.xls)
I know how to use ADO.NET to connect to and execute commands BUT how would I pretty much code it to take all data from dataset and insert it into the file?
I know I require an OleDbCommand and an Insert statement but would I have to iterate through each column, get the names, and add the @param as values, then set the datasource/sourcecolumn and execute the query?
-
Sep 26th, 2006, 09:44 PM
#8
Re: dataset exporting
I guess I should qualify what I said previously, because I don't know for sure but I doubt that ADO.NET can create XLS files. Assumimg that you have an XLS file you may be able to create worksheets with CREATE TABLE statements, although, again, I'm not sure. You would have to add column headers using CREATE COLUMN statements I would expect, or you can specify that the columns don't have headers using the connection string and just refer to the columns by the generic names F1, F2, etc. www.connectionstrings.com has the connection string format and the basic syntax for referring to a worksheet or cell range as a table. I should specify that I haven't done anything more than basic data access this way, so I don't know exactly how capable the mechanism is.
-
Sep 26th, 2006, 09:49 PM
#9
Thread Starter
PowerPoster
Re: dataset exporting
sure, many thanks. I tried doing it the way I had thought you meant but I get an error "@theID has no default Value"
Code:
OleDbCommand theOleDbCommand = new OleDbCommand("INSERT INTO [Sheet1$] (", this.theOleDbConnection);
string theParams = String.Empty;
foreach (DataColumn curColumn in this.theDataSet.Tables[0].Columns)
{
if (curColumn.Ordinal < this.theDataSet.Tables[0].Columns.Count - 1)
{
theOleDbCommand.CommandText += curColumn.ColumnName + ",";
OleDbParameter theParam = new OleDbParameter();
theParam.SourceColumn = curColumn.ColumnName;
theParam.ParameterName = "@the" + curColumn.ColumnName;
theOleDbCommand.Parameters.Add(theParam);
}
else
{
theOleDbCommand.CommandText += curColumn.ColumnName;
OleDbParameter theParam = new OleDbParameter();
theParam.SourceColumn = curColumn.ColumnName;
theParam.ParameterName = "@the" + curColumn.ColumnName;
theOleDbCommand.Parameters.Add(theParam);
}
}
theOleDbCommand.CommandText += ") VALUES (" + theParams + ")";
theOleDbCommand.Connection.Open();
theOleDbCommand.ExecuteNonQuery();
theOleDbCommand.Connection.Close();
yes I know it's a bad way..... lol.
-
Sep 27th, 2006, 04:27 PM
#10
Re: dataset exporting
You've created a string called theParams, but you haven't assigned it any values. If you want your SP to take you seriously, you should give it corresponding values.
-
Sep 27th, 2006, 04:31 PM
#11
Thread Starter
PowerPoster
Re: dataset exporting
forget theparams string lol I'm not using it at all.... instead im just directly giving and creating the OleDbParameter....
-
Sep 27th, 2006, 04:33 PM
#12
Re: dataset exporting
Yes, but if you don't supply a value to it, the stored procedure will look for any default values specified in the CREATE PROCEDURE... and you obviously haven't specified one.
So you need to give those parameters a value.
-
Sep 27th, 2006, 04:57 PM
#13
Thread Starter
PowerPoster
Re: dataset exporting
hmmmmm
im not using a SPROC - im trying to write data to an excel sheet.
I thought I would be "mapping" the source column so it can get the values from there during insertion?
-
Sep 27th, 2006, 05:19 PM
#14
Re: dataset exporting
Ah, the Sproc was an oversight on my part, but you are inserting values into the excel sheet. Right?
-
Sep 27th, 2006, 05:29 PM
#15
Thread Starter
PowerPoster
Re: dataset exporting
that is what I want to do.
I have a dataset filled with data. I now want to insert this data into an excel spreadsheet without the need for using the COM approach, if at all possible
-
Sep 28th, 2006, 04:14 PM
#16
Re: dataset exporting
I don't think you can escape COM. You can use ADO.NET but that in turn uses COM objects to talk to Excel. However, considering that the .NET framework will be on the user's machine, there shouldn't really be a problem there. Why the COM-aversion?
-
Sep 28th, 2006, 04:25 PM
#17
Thread Starter
PowerPoster
Re: dataset exporting
I just don't understand it! I need to learn it no doubt, just thought there would be a simpler way of using it via ADO.NET
-
Sep 28th, 2006, 04:42 PM
#18
Re: dataset exporting
Look here
http://support.microsoft.com/kb/316934/
Understanding that COM is used in the background helps, but it's not entirely necessary.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|