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
// ...
}
@Forest Cheng: No thanks. If I wanted to buy a component, I would. If I wanted someone to write the code, I would hire that person. However, I am a software developer and I would like to learn these techniques.
Can someone assist me with this issue? I am looking for a way to "Enable Content" on an Excel file without having to manually open the file and clicking the "Enable Content" button. I shared my working code to help people with uploading files. Can someone
help me with this issue? No spamming, please (eg, Forest Cheng). Thanks!
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
// ...
}
Forest Cheng...
Star
8370 Points
819 Posts
Re: Turn Off Macros in Excel File using ASP.NET MVC 3
May 26, 2011 01:45 AM|LINK
Hi Birdman,
You can use GemBox.Spreadsheet, which is a .Net component to write, read or convert spreadsheet files (XLS, CSV, XLSX, HTML or ODS) in .Net Application. Check these links: http://www.gemboxsoftware.com/GBSpreadsheet.htm and http://www.gemboxsoftware.com/WebDemo/Index.aspx
For example:
ExcelFile excelFile = new ExcelFile(); excelFile.LoadXls("../../Input/HelloWorld.xls"); excelFile.SaveXlsx("../../Output/HelloWorld.xlsx");Hope this helpful,
Forest Cheng
If you have any feedback about my replies,please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Birdman2012
Member
8 Points
8 Posts
Re: Turn Off Macros in Excel File using ASP.NET MVC 3
Dec 22, 2011 03:49 PM|LINK
@Forest Cheng: No thanks. If I wanted to buy a component, I would. If I wanted someone to write the code, I would hire that person. However, I am a software developer and I would like to learn these techniques.
Can someone assist me with this issue? I am looking for a way to "Enable Content" on an Excel file without having to manually open the file and clicking the "Enable Content" button. I shared my working code to help people with uploading files. Can someone help me with this issue? No spamming, please (eg, Forest Cheng). Thanks!