Daniel Harvey
Daniel Harvey

Reputation: 379

FileUploadControl with no file system access

Unfortunately, both OLEDB and FileUploadControl seem to want a path.

Right now, this works after a user has submitted an excel file but won't work on my server due to 0 file system access:

FileUploadControl.SaveAs(filePath);
ConvertToCSV(filePath);
...
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", filePath);
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);

I see FileUploadControl exposes a stream and a bytecode. Would it be possible to traverse the file with those properties?

Upvotes: 1

Views: 567

Answers (2)

joshb
joshb

Reputation: 5220

Have a look at Excel Data Reader

I was able to read values from the stream no problem using the following code:

Stream myStream = MyFileUpload.FileContent;

IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(myStream);
DataSet result = excelReader.AsDataSet();

string r1c1Val = result.Tables[0].Rows[0][0].ToString();

Obviously my example is really simple but having the data as a DataSet should give you a lot of flexibility in how you read it.

Upvotes: 1

Yahia
Yahia

Reputation: 70369

AFAIK the OleDB won't work on streams...

IF you really want to access the Excel-file directly in-memory after upload then you will need some 3rd-party component (like Flexcel, Aspose.Cells, SpreadsheetGear...) which can load Excel files directly from memory (FileUploadControl.FileBytes or FileUploadControl.FileContent)...

Although not sure what ConvertToCSV does and whether it can work on memory...

Upvotes: 1

Related Questions