Umbraco Excel Macro that injects the Excell content into the RTE so it can be edited


Some code to to create an umbraco macro that takes an id of an excell sheet stored in the media lib

and shows it in the RTE, but normally you can edit the content of an excel sheet in an macro like dragging the borders to make the table bigger or to add custom styles.

 

first create an umbraco macro, and open the view file in visual studio and add the following code

this code also uses ASPOSE to read in the excel file

<code>
<code>
@using System.Diagnostics
@using System.Web.Hosting
@using Aspose.Cells
@using Aspose.Cells.Tables;
@using System.Text;
@using ClientDependency.Core.Mvc
@using Newtonsoft.Json.Linq
@inherits Umbraco.Web.Macros.PartialViewMacroPage

@{
string datasource = Model.MacroParameters[“dataSource”].ToString();
string titel = Model.MacroParameters[“titel”].ToString();
var media = ApplicationContext.Current.Services.MediaService.GetById(Convert.ToInt32(datasource));
string path = HostingEnvironment.MapPath(media.Properties[“umbracoFile”].Value.ToString());
string tableId = string.Format(“media{0}”, datasource);
}
<div id=”exellDiv”>
@if (media != null)
{
using (FileStream stream = new FileStream(path, FileMode.Open))
{
Workbook book = new Workbook(stream);
if (book != null)
{
Worksheet sheet = book.Worksheets[0];
int rowCount = sheet.Cells.MaxDisplayRange.RowCount;
int columnCount = sheet.Cells.MaxDisplayRange.ColumnCount;

StringBuilder sb = new StringBuilder();

sb.AppendFormat(“<table id=\”{0}\” border=\”1\”>”, tableId);
for (int r = 0; r <= rowCount; r++)
{
sb.Append(“<tr>”);
for (int c = 0; c <= columnCount; c++)
{
Cell cell = sheet.Cells.GetCell(r, c);
if (cell == null)
{
continue;
}

sb.AppendFormat(“<td>{0}</td>”, Html.Raw(cell.HtmlString));

}
sb.Append(“</tr>”);
}
sb.Append(“</table>”);

<script>
tinymce.activeEditor.execCommand(‘insertHTML’, false, ‘@Html.Raw(sb.ToString())’);
tinymce.activeEditor.execCommand(‘mceRemoveControl’, false, ‘exellDiv’);
</script>
}
book.DisposeIfDisposable();
}
}
</div>

</code>

then in yr class that inherits from the  IApplicationEventHandler

add the following lines of code in the event

<code>
void PublishingStrategy_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
foreach (var item in e.PublishedEntities)
{
// its a hackarround to remove the injector macro
// the macro reads an exell file, then injects it into the RTE, and removes the macro tag so it doesnt reload
if (item.ContentType.Alias.ToLower() == “***yourContentType***”)
{
if (item.Properties[“bodyText”].Value != null)
{
string bodyText = item.Properties[“bodyText”].Value.ToString();
bodyText = System.Text.RegularExpressions.Regex.Replace(bodyText, “<\\?UMBRACO_MACRO macroAlias=\”***aliasOfYourMacro***\” [^>]+>”, “”);
item.Properties[“bodyText”].Value = bodyText;
}
}

}
}
</code>

About johnnblade aka johnnyblade Thugnology

C# Developer 9Ja Thugnology
This entry was posted in Umbraco. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s