我的MVC应用程序中有一个控制器方法,该方法读取txt文档(从Excel电子表格保存的制表符分隔文件)。这来自HttpPostedFileBase
类型的上传。
我想做的就是获取该HttpPostedFileBase文件,如果它是Excel电子表格,则以与文本文件相同的方式将其读入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
}