Четене на данни от ексел (има ли по-добър начин?)
Здравейте,
Може ли някой да ми покаже как да си направя метода за четене на данни от ексел използвайки Excel.Workbook, Excel.Application, Excel.Worksheet, Excel.Range (using Microsoft.Office.Interop.Excel => Add reference/COM/TypeLibraries: Microsoft Office 14.0 Object Library)?Инициализиането на sheets не ми сработи.
Пробвах се, но не с получи, но намерих workaround. Бих искала, ако може да разбера и по-интелигентния начин за четене.
Workaround:
public static RegistrationUser GetTestDataRegUser(string keyNameMartialStatus, string keyNameHobby, string keyNameRest)
{
using (var connection = new OleDbConnection(TestDataFileConnection()))
{
connection.Open();
RegistrationUser regUser = new RegistrationUser();
if (keyNameHobby!=null)
{
var hobby_query = string.Format("select * from [RegistrationForm_Hobby$] where Key = '{0}'", keyNameHobby);
OleDbDataAdapter oleDB = new OleDbDataAdapter(hobby_query, connection);
DataTable dtOleDB = new DataTable();
oleDB.Fill(dtOleDB);
foreach (DataRow row in dtOleDB.Rows)
{
string[] hobby = new string[dtOleDB.Rows.Count];
for (int m = 0; m < hobby.Length; m++)
hobby[m] = dtOleDB.Rows[m]["Hobby"].ToString();
int[] int_hobby = new int[dtOleDB.Rows.Count];
for (int i = 0; i < hobby.Length; i++)
int_hobby[i] = int.Parse(hobby[i]);
regUser.Hobby = int_hobby;
}
}
var martialstatus_query = string.Format("select * from [RegistrationForm_MartialStatus$] where Key = '{0}'", keyNameMartialStatus);
var rest_query = string.Format("select * from [RegistrationFormTests$] where Key = '{0}'", keyNameRest);
OleDbDataAdapter oleDA = new OleDbDataAdapter(martialstatus_query, connection);
OleDbDataAdapter oleDC = new OleDbDataAdapter(rest_query, connection);
DataTable dtOleDA = new DataTable();
DataTable dtOleDC = new DataTable();
oleDA.Fill(dtOleDA);
oleDC.Fill(dtOleDC);
foreach (DataRow row in dtOleDA.Rows)
{
string[] martial = new string[dtOleDA.Rows.Count];
for (int m = 0; m < martial.Length; m++)
martial[m] = dtOleDA.Rows[m]["MartialStatus"].ToString();
int[] int_martial = new int[dtOleDA.Rows.Count];
for (int i = 0; i < martial.Length; i++)
int_martial[i] = int.Parse(martial[i]);
regUser.MatrialStatus = int_martial;
}
foreach (DataRow row in dtOleDC.Rows)
{
regUser.FirstName = row["FirstName"].ToString();
regUser.LastName = row["LastName"].ToString();
regUser.Country = row["Country"].ToString();
regUser.BirthMonth = row["BirthMonth"].ToString();
regUser.BirthDay = row["BirthDay"].ToString();
regUser.BirthYear = row["BirthYear"].ToString();
regUser.Phone = row["Phone"].ToString();
regUser.UserName = row["UserName"].ToString();
regUser.Email = row["Email"].ToString();
regUser.UploadPicDir = row["UploadPicDir"].ToString();
regUser.Description = row["Description"].ToString();
regUser.Password = row["Password"].ToString();
regUser.ConfirmPassword = row["ConfirmPassword"].ToString();
}
connection.Close();
return regUser;
}
}