Blunderfest
Blunderfest

Reputation: 1854

Is it possible to read data from an excel file without uploading it to the server in VB.Net?

I need to be able to read the data from an excel file and upload the data to a database after validating it.

However, the server I'm working with does not allow write privileges for the web applications, so I need to know if it is possible to read from an excel file without writing it to the server through upload?

So far I haven't been able to find a clear answer.

Thanks!

Upvotes: 3

Views: 2139

Answers (1)

Linus Caldwell
Linus Caldwell

Reputation: 11078

Let's say your upload control is called fileUpload.

You don't need to do a fileUpload.SaveAs("path"). You can read the stream with fileUpload.PostedFile.InputStream. I used this for a zip file with excel sheets in it (the library is Ionic by the way):

using (var file = ZipFile.Read(fileUpload.PostedFile.InputStream))
{
    foreach (var zipEntry in file.Where(ze => ze.FileName.EndsWith(".xls")
                                           || ze.FileName.EndsWith(".xlsx")))
    {
        // process the Excel files here.
    }
}

Sorry, I'm not very familiar with VB.net, so the following might be wrong. But because you asked for a VB.net version:

Using file As var = ZipFile.Read(fileUpload.PostedFile.InputStream)
    For Each zipEntry As var In file.Where(ze => ze.FileName.EndsWith(".xls") or ze.FileName.EndsWith(".xlsx"))
        ' process the Excel files here.
    Next
End Using

Upvotes: 2

Related Questions