I have a controller method in my MVC application which reads a txt document (which is tab delimited file saved from a Excel spreadsheet). This comes from the upload which is a HttpPostedFileBase
type.
What I would like to do is to take that HttpPostedFileBase file and if it is an Excel spreadsheet, read it in to the streamreader the same way I would the text file. Is this possible?
The following is my code where I read in the text file into a streamreader:
[HttpPost]
public JsonResult functionalFileImport(IEnumerable<HttpPostedFileBase> functionalFile)
{
//Prepare validation result
ValidationResult validationResult = new ValidationResult();
validationResult.vResult = (int)Result.Success;
validationResult.vMessage = "";
//Prepare import result
ImportResult importResults = new ImportResult();
//Prepare file variables
string fileName;
string filepath;
string fileExtension;
string tmpTableName = "";
string message = "";
string logfile = "";
List<ValidationResult> validation = null;
try
{
using (cDataAccess da = new cDataAccess(true))
{
if (functionalFile != null)
{
foreach (HttpPostedFileBase file in functionalFile)
{
//Set file details.
SetFileDetails(file, out fileName, out filepath, out fileExtension);
if (fileExtension == ".txt")
{
StreamReader stream = new StreamReader(file.InputStream);
string buffer = stream.ReadToEnd();
// Remove all carriage returns, if present
buffer = buffer.Replace("\r", "");
//Create a DataTable from the File data
DataTable dt = CreateDataTableFromFile(buffer);
DataTable dtFC = new DataTable();
// --- Would like to do a conversion if this is a Excel spreadsheet
}
}
}
}
}
catch (Exception ex)
{
// something to do
}
// something else with return statement
}