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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jc mandapatPosted Oct 30, 2006, 2:31 AM
saravanan gajendranPosted Oct 11, 2006, 6:43 AM
Manoj BistPosted Aug 29, 2006, 9:35 PM
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;
Dipen LamaPosted Aug 28, 2006, 3:01 AM