Here is the problem. My users upload Excel files. If the file is an .xls file, I have to manually open the file and click the "Enable Content" button. Then, my website can open the Excel file and import the data. How can I "Enable Content" using code,
get rid of the macros, or somehow get around this issue? Please note that when my users upload .xlsm or .xlsx files, the import works perfectly. How can I convert an .xls file to an .xlsx file in MVC3?
Thanks for your help! Here's my code:
Import Webpage:
@model Photos.Models.ImportFile
@{ ViewBag.Title = "Import Data File";}
<h2>Import Data File</h2>
<form action="/ImportFile/Import" method="post" enctype="multipart/form-data">
<label for="fileName">Select a file to import:</label> <input type="file" name="fileName" id="file" size="40" />
<br />
<p class="button">
Please note that the Import Data File process may take a few seconds<br />
to a few minutes depending on the number of student records to import.<br /><br />
public ActionResult Import() {
string fileName = "";
// save the file to the server
foreach (string file in Request.Files)
{
HttpPostedFileBase posted = Request.Files[file] as HttpPostedFileBase;
if (posted.ContentLength == 0)
continue;
fileName = Server.MapPath("~/Uploaded/"
+ System.IO.Path.GetFileName(posted.FileName));
posted.SaveAs(fileName);
}
var excel = new ExcelQueryFactory(fileName);
var worksheetNames = excel.GetWorksheetNames();
ExcelQueryable<ExcelData> records = null;
foreach( string sheet in worksheetNames )
{
records = (from r in excel.Worksheet<ExcelData>(worksheetNames.ElementAt(0))
select r) as ExcelQueryable<ExcelData>;
if (records != null)
break;
}
// and now I import the records into my SQL server
// ...
}
Birdman2012
Member
8 Points
8 Posts
Turn Off Macros in Excel File using ASP.NET MVC 3
May 18, 2011 02:25 AM|LINK
Here is the problem. My users upload Excel files. If the file is an .xls file, I have to manually open the file and click the "Enable Content" button. Then, my website can open the Excel file and import the data. How can I "Enable Content" using code, get rid of the macros, or somehow get around this issue? Please note that when my users upload .xlsm or .xlsx files, the import works perfectly. How can I convert an .xls file to an .xlsx file in MVC3?
Thanks for your help! Here's my code:
Import Webpage:
@model Photos.Models.ImportFile
@{ ViewBag.Title = "Import Data File";}
<h2>Import Data File</h2>
<form action="/ImportFile/Import" method="post" enctype="multipart/form-data">
<label for="fileName">Select a file to import:</label> <input type="file" name="fileName" id="file" size="40" />
<br />
<p class="button">
Please note that the Import Data File process may take a few seconds<br />
to a few minutes depending on the number of student records to import.<br /><br />
<input type="submit" name="submit" value="Upload Now" /> |
@Html.Raw("<A HREF='javascript:history.go(-1)'>Back</A>") |
@Html.ActionLink("Back to Home", "Index", "Home")</p>
</form>
Method in the MVC3 Controller:
public ActionResult Import() {
string fileName = "";
// save the file to the server
foreach (string file in Request.Files)
{
HttpPostedFileBase posted = Request.Files[file] as HttpPostedFileBase;
if (posted.ContentLength == 0)
continue;
fileName = Server.MapPath("~/Uploaded/"
+ System.IO.Path.GetFileName(posted.FileName));
posted.SaveAs(fileName);
}
var excel = new ExcelQueryFactory(fileName);
var worksheetNames = excel.GetWorksheetNames();
ExcelQueryable<ExcelData> records = null;
foreach( string sheet in worksheetNames )
{
records = (from r in excel.Worksheet<ExcelData>(worksheetNames.ElementAt(0))
select r) as ExcelQueryable<ExcelData>;
if (records != null)
break;
}
// and now I import the records into my SQL server
// ...
}