I am having Openoffice excel sheet that contains some fields like state,district,taluka,zone,region,territory
i want to store excel sheet data into datatable
Please any body help me
Thanks.
Loading
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.
Gopal MahadakPosted Feb 10, 2013, 11:39 PM
using System.Data.OleDb;
using excel = Microsoft.Office.Interop.Excel;
using System.IO;
//////////for opening excel file
OpenFileDialog o = new OpenFileDialog();
o.Filter = "|*.xls";
o.Filter = "Excel files |*.xls";
o.Title = "select your Excel attachment ";
if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtFilePath.Text = o.FileName;
}
str=Path.GetFileName(txtFilePath.Text.ToString());
txtFilePath.Text = str;
/////////////uploading excel file in Datatable
excel._Application ae = new excel.Application();
excel.Workbook we = ae.Workbooks.Open(txtFilePath.Text);
excel.Worksheet ws = ae.Worksheets.get_Item(1);
excel.Range re = ws.UsedRange;
DataTable dt = new DataTable();
dt.Columns.Add("Taluka");//adding column to datatable similar to excel sheet
dt.Columns.Add("Place");
for (row = 2; row <= re.Rows.Count; row++)
{
DataRow dr = dt.NewRow();
for (column = 1; column <= re.Columns.Count; column++)
{
dr[column - 1] = (re.Cells[row, column] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
}
dt.Rows.Add(dr);
}
we.Close();
ae.Quit();