Reading Excel in C#
does anyone know how to read an excel file and transfer it's contents to the textboxes? thanks!
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
Dipen LamaRank #3,323 · 432 reputationPosted Aug 27, 2006, 11:01 PM EDT
Manoj Bist15 reputationPosted Aug 29, 2006, 5:35 PM EDT
here is the code to read data from excel file and load it in gridview control
app = new Excel.ApplicationClass();
String filename = "D:\\Test.xls";
Excel.WorkbookClass workbook = (Excel.WorkbookClass)app.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Sheets excelSheets = workbook.Worksheets;
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(1);
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range("A1", "c29");
DataTable dt = new DataTable("Expense");
dt.Columns.Add("Date", Type.GetType("System.String"));
dt.Columns.Add("Item", Type.GetType("System.String"));
dt.Columns.Add("Price", Type.GetType("System.String"));
for (int i = 2; i <= excelCell.Rows.Count; i++)
{
DataRow dr = dt.NewRow();
dr["Date"] = ((Excel.Range)excelCell.Cells[i, 1]).Value2.ToString();
dr["Item"] = ((Excel.Range)excelCell.Cells[i, 2]).Value2.ToString();
dr["Price"] = ((Excel.Range)excelCell.Cells[i, 3]).Value2.ToString();
dt.Rows.Add(dr);
}
gv.DataSource = dt;
saravanan gajendran23 reputationPosted Oct 11, 2006, 2:43 AM EDT
jc mandapat16 reputationPosted Oct 29, 2006, 9:31 PM EST