Showing posts with label utils. Show all posts
Showing posts with label utils. Show all posts

Capturing strings between stopwords



private string StringBetween(string beginTag, string endTag, string texteToParse)
{
System.Collections.Specialized.StringCollection coll = StringsBetween(beginTag, endTag, texteToParse);
if (coll.Count == 0) return "";
else return coll[0];
}
private System.Collections.Specialized.StringCollection StringsBetween(string beginTag, string endTag, string texteToParse)
{
System.Collections.Specialized.StringCollection result = new System.Collections.Specialized.StringCollection();
int pos1, pos2;
pos1 = 0;
pos2 = -1;
pos1 = texteToParse.IndexOf(beginTag);
while (pos1 > -1)
{
pos2 = texteToParse.IndexOf(endTag, pos1 + beginTag.Length);
if (pos2 > -1)
{
result.Add(texteToParse.Substring(pos1 + beginTag.Length, pos2 - (pos1 + beginTag.Length)));
pos1 = texteToParse.IndexOf(beginTag, pos2 + endTag.Length);

}
else
pos1 = -1;

}
return result;
}

Reading an Excel file as a DataTable

This method open a Excel file and retun it as a datatable.
It uses oleDB, wich is very powerful.

If your columns contains headers, they will be read as datacolumn names. This way, you can access datarows with theese names as indexers, ex: value = myRow("myColumnName").



Protected Function GetDatatableFromExcel(ByVal fileName As String, ByVal sheetName As String) As DataTable
Dim conn As System.data.oledb.OleDbConnection
Dim dataResult As New DataTable
Try
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
+ fileName + ";Extended Properties=Excel 8.0;")
conn.Open()

Dim command As New System.Data.OleDb.OleDbCommand(" SELECT * FROM [" + sheetName + "$]")
command.Connection = conn
Dim adaperForExcelBook As New OleDbDataAdapter
adaperForExcelBook.SelectCommand = command
adaperForExcelBook.Fill(dataResult)
conn.Close()

Catch err As Exception
Throw New Exception("Error reading file: " + fileName)
End Try

Return dataResult
End Function