public Object readExcel(string filepath)
{
static string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\EmployeeXLS.xls;
Extended Properties=Excel 5.0";
string query = @"Select * From [Sheet1$]";
System.Data.OleDb.OleDbConnection con =
new System.Data.OleDb.OleDbConnection(
ConnectionString);
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter(query, con);
// DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(ds, "nyNameHere");
//con.Close();
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
List<Employee> _empList = new List<Employee>();
try
{
foreach (DataRow myDataRow in ds.Tables[0].Rows)
{
Employee emp = new Employee();
emp.FName = myDataRow["fname"].ToString();
emp.LName = myDataRow["lname"].ToString();
emp.Age = int.Parse( myDataRow["age"].ToString());
emp.Salary = Double.Parse(myDataRow["wage"].ToString());
_empList.Add(emp);
//Stores info in Datarow into an array
Object[] cells = myDataRow.ItemArray;
//Traverse through each array and put into object cellContent as type Object
//Using Object as for some reason the Dataset reads some blank value which
//causes a hissy fit when trying to read. By using object I can convert to
//String at a later point.
foreach (object cellContent in cells)
{
//Convert object cellContect into String to read whilst replacing Line Breaks with a defined character
string cellText = cellContent.ToString();
cellText = cellText.Replace("\n", "|");
sb.Append("\n" + cellText);
//Read the string and put into Array of characters chars
// richTextBox1.AppendText("\n" + cellText);
}
return )empList;
}
No comments:
Post a Comment