I am a beginner in C#.
I need examplecode of how to read excel files into c#, and then how to use a dataset to store the excel columns into the database wich is an MS Sql 2000 db.
Any suggestions.
Thanks!
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
davidh122 reputationPosted Mar 16, 2006, 6:01 PM EST
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2005_ta/html/OfficeVSTOExcelObj.asp
The Office Tools for visual studio should give you what you need. I haven't used them myself yet, though.
Dave
li2 reputationPosted Mar 17, 2006, 4:21 AM EST
??????
Craig MurphyRank #2,956 · 488 reputationPosted Mar 19, 2006, 12:13 PM EST
using
System;using
System.Runtime.InteropServices;using
Excel = Microsoft.Office.Interop.Excel;namespace
cmExcelEngine{
public class ExcelEngine{
private System.Object missingValue = System.Reflection.Missing.Value; private const int cA = 1;// thru cZ = 26; as required
private Excel.Application excelApp = null; private Excel.Workbook excelWorkbook = null; private Excel.Sheets excelSheets = null; private Excel.Worksheet excelWorksheet = null; private Excel.Workbooks excelWorkbooks = null; public ExcelEngine()
{
string cd = System.Environment.CurrentDirectory;excelApp =
new Excel.ApplicationClass(); // Open an Excel template, create a new "document"excelApp.Workbooks.Add(cd +
"\\template.xlt");// or load a file of your choosing
//excelApp.Workbooks.Open(cd + "\\template.xlt", false, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", // true, false, null, false, null, false);
}
public void Show(){
excelApp.Visible =
true;}
public Excel.Worksheet SelectSheet(string sheetName){
Excel.Worksheet returnSheet =
null; foreach(Excel.Worksheet workSheet in excelWorkbook.Worksheets){
if (workSheet.Name == sheetName)returnSheet = workSheet;
}
return returnSheet;}
// Read/Write to/from the Excel sheet...public void PopulateSummary;
{
excelWorksheet.Cells[17, cC] = 1000;
}
public void Quit(){
excelWorkbook.Close(
false, missingValue, missingValue);excelWorkbooks.Close();
excelApp.Quit();
Marshal.ReleaseComObject(excelWorksheet); Marshal.ReleaseComObject(excelSheets); Marshal.ReleaseComObject(excelWorkbooks); Marshal.ReleaseComObject(excelWorkbook); Marshal.ReleaseComObject(excelApp);excelWorksheet =
null;excelSheets =
null;excelWorkbooks =
null;excelWorkbook =
null;excelApp =
null; GC.GetTotalMemory(false); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.GetTotalMemory(true);}
}
}
It is invoked like this:
ExcelEngine ee =
new ExcelEngine();ee.PopulateSummary;
ee.Show();
This example demonstrates how to open an Excel spreadsheet and populate it - but reading from the spreadsheet uses the same code:
SummaryCost = excelWorksheet.Cells[17, cC];
Once you have got the Excel aspect working, post back here with some code/data examples...may be somebody will be able to help with your database question then...
HTH
Francois Steyn2 reputationPosted Oct 3, 2006, 12:47 PM EDT