Sehr geehrte Community,
ich hoffe ihr könnt mir weiterhelfen.
Ich möchte gerne aus einer Excel-Zelle eine Zeit (Format: hh:ss) auslesen und einer Textbox übergeben.
das übergeben klappt zwar, aber die Textbox zeigt die Zeit wie folgt an. (z. B. 0,3182145).
Vielleicht könnt ihr mir da den richtigen Anstoß geben und bedanke mich schon einmal im Voraus.
String ergebnis = WorkSheet.Cells[getIndexVeranstaltung() + getIndexWettkampf, i + 1].Value + "";
String.Format("hh:mm", ergebnis);
MessageBox.Show(ergebnis);
String.Format("hh:mm", ergebnis);
MessageBox.Show(ergebnis);
mfg
Wim SturkenboomPosted Sep 14, 2014, 1:37 AM
Wim SturkenboomPosted Sep 14, 2014, 1:34 AM
Your first problem is that you're using String.Format the wrong way. String.Format returns a string so you have to save it in a variable or use the result directly.
The two ways:
string Wert = String.Format("hh:mm", ergebnis);
MessageBox.Show(Wert);
Or
MessageBox.Show(String.Format("hh:mm", ergebnis));
The next problem with your String.Format is that you don't tell it to use the parameter ergebnis that you're passing. So if you use the above, it will simply give you "hh:mm" in the MessageBox.
To solve that, you need a construction like
string Wert = String.Format("{0}", ergebnis);
In the above, Wert now contains the same value as ergebnis. I unfortunately have not been able to figure out what the 0,3182145 represents so I could not get further. It's also unknown to me how you exactly read your excel sheet. Below my attempt.
The method to read the excel sheet is a slightly modified version of an example that I've found here: http://aspsnippets.com/Articles/Read-Excel-file-using-OLEDB-Data-Provider-in-C-Net.aspx. Just check it out. The difference with the original is that the below version returns a dataset that can be used for further processing.
private DataSet ImportExcel2007(String strFilePath)
{
if (!File.Exists(strFilePath)) return null;
String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=" + strFilePath + ";"
+ "Extended Properties='Excel 8.0;HDR=No'";
OleDbConnection connExcel = new OleDbConnection(strExcelConn);
OleDbCommand cmdExcel = new OleDbCommand();
try
{
cmdExcel.Connection = connExcel;
//Check if the Sheet Exists
connExcel.Open();
DataTable dtExcelSchema;
//Get the Schema of the WorkBook
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
connExcel.Close();
//Read Data from Sheet1
connExcel.Open();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
//Range Query
//cmdExcel.CommandText = "SELECT * From [" + SheetName + "A3:B5]";
da.SelectCommand = cmdExcel;
da.Fill(ds);
connExcel.Close();
return ds;
}
catch
{
return null;
}
finally
{
cmdExcel.Dispose();
connExcel.Dispose();
}
}
The code that uses the above
private void btnRead_Click(object sender, EventArgs e)
{
DataSet ds = ImportExcel2007("F:\\3_DevelopmentDemo\\ReadExcel\\ReadExcel\\bin\\Debug\\test.xlsx");
DataTable tbl = ds.Tables[0];
for (int rowcnt = 0; rowcnt < tbl.Rows.Count; rowcnt++)
{
DateTime x = (DateTime)tbl.Rows[rowcnt][0];
MessageBox.Show(String.Format("{0}", x.ToString("hh:mm")));
}
}
It reads an excel file and stores the resulting dataset in ds. The first table in dataset contains sheet1.
Next the program loops through the rows of the datatable (the rows in sheet1), converts the value of column A to a datetime and displays it in a messagebox with the required format.
Note:
If you get a 'provider not registered' exception, you can read http://social.msdn.microsoft.com/Forums/en-US/1d5c04c7-157f-4955-a14b-41d912d50a64/how-to-fix-error-the-microsoftaceoledb120-provider-is-not-registered-on-the-local-machine?forum=vstsdb
Installing the database engine (first solution) worked for me (http://www.microsoft.com/en-us/download/confirmation.aspx?id=13255)
I hope this helps you on the way.