Open Excel Project
-
Dear friends, I have to write a project that's able to read & write data to an excel file. Just simply like a data base program. do you have any hints or example?
I had a big headache trying to do this myself. There may be a way to read and write to a file, but I found I a diffent way to write to Excel, and a different was of reading from, with some help from one of our friends on the CodeProject. Here is a simple example of writing to Excel. It creates a new Excel book and writes a string in the 1st cell. /**************************************************/ using System; using Excel; using System.IO; using System.Text; using System.Data.OleDb; namespace Writing_to_Excel_example { class Class1 { static void Main(string[] args) { bool xlsError = false; int rowCount = 0; int counter = 0; int counter2 = 0; Console.WriteLine("Working..."); Excel.Application ExcelObj = new Excel.Application(); Excel.Workbook workbook = ExcelObj.Workbooks.Add(Type.Missing); if (ExcelObj == null) { Console.WriteLine("\nERROR: EXCEL couldn't be started!"); xlsError = true; } // activate the active worksheet in the workbook Worksheet ws = (Worksheet)ExcelObj.ActiveSheet; ws.Activate(); try { //To enter text to a specific row and column: ((Range)ws.Cells[1, 1]).Value2 = ("This is Row 1, Column 1"); //To set an entire row as Bold. ((Range)ws.Cells[rowCount, 1]).EntireRow.Font.Bold = true; }//end try catch(Exception e) { Console.Write(e); } //Make Excel visible. ExcelObj.Visible = true; } } } /***************************************************************/ DONT FORGET TO ADD REFERENCE TO EXCEL 11.0 OBJECT LIBRARY!! As for reading from excel, I tried to apply the same sort of logic as above but it didn't work. So have a look at this link: http://www.codeproject.com/csharp/FasterExcelAccessTOC.asp[^] Great tips there, a great help to me, Thanks to Dusty Candland, great help mate!! t-seanm