2
votes

im building an excel upload feature on my web. it will read excel data using OLEDB. the problem is the excel file that user inputted can be either in 'real' excel file or excel in html import extended properties so the connection string is different. right now i handle it with try catch. if the first conn string generate exception then it will open connection with the other connection string in catch.

the problem is my lead says i can't use that kinda method. so is there any way so i can do that without try catch ( anything related throwing an exception ), or to see whether the excel file is in excel 8.0 extended properties or html import extended properties.

try
{
    ...
    try
    {
        connection.ConnectionString = GetConnectionString(pathCopy);
        connection.Open();
    }
    catch
    {
        connection.ConnectionString = GetConnectionStringHTMLEncoded(pathCopy);
        connection.Open();
    }
    ...
catch (Exception ex)
{
    ...
}

Thanks

1
Could you show your try/catch block?Tyanna

1 Answers

1
votes

You can verify the internal makeup of the file by opening one of the html string encoded files in a text editor (like notepad). You should see that the underlying file is still html text. You should be able to read the first line of the file and check for the string "html"

var reader = System.IO.File.OpenText(pathCopy);
string firstLine = reader.ReadLine();
if (firstLine.IndexOf("html", StringComparison.InvariantCultureIgnoreCase) > 0)
    //some stuff
else
    //other stuff

You shouldn't get an error on the binary file, you'll just get some really strange characters in the string—fortunately, characters that won't include the string "html".