Skip to content

Instantly share code, notes, and snippets.

@JBoye
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JBoye/d183ad17c811303ecbe6 to your computer and use it in GitHub Desktop.
Save JBoye/d183ad17c811303ecbe6 to your computer and use it in GitHub Desktop.
Method of Exporting Umbraco dictionary items to Excel and back. Uses http://epplus.codeplex.com. Based on https://github.com/hfloyd/DictionaryDashboardForUmbraco
<%@ WebHandler Language="C#" Class="DictionaryExport" %>
using System;
using System.IO;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.language;
public class DictionaryExport : IHttpHandler {
protected List<Language> allLanguages = new List<Language>();
public void ProcessRequest (HttpContext context) {
allLanguages.AddRange(Language.GetAllAsList());
var list = new List<Dictionary.DictionaryItem>();
foreach (var root in Dictionary.getTopMostItems) {
this.AddToList(root, ref list);
}
var excel = new OfficeOpenXml.ExcelPackage();
var sheet = excel.Workbook.Worksheets.Add("Dictionary");
sheet.DefaultColWidth = 30;
sheet.Cells["A1"].Value = "Key";
sheet.Cells["B1"].Value = "Description";
int col = 3;
int row = 1;
foreach (var lang in allLanguages) {
sheet.Cells[1, col].Value = lang.CultureAlias;
col++;
}
sheet.Cells["1:1"].Style.Font.Bold = true;
sheet.Cells["1:1"].Style.Font.Size = 12;
row = 2;
foreach (var item in list) {
sheet.Row(row).Hidden = String.IsNullOrWhiteSpace(item.Value(allLanguages.First().id));
sheet.Cells[row, 1].Value = item.key;
col = 3;
foreach (var lang in allLanguages) {
sheet.Cells[row, col].Value = item.Value(lang.id).Replace("\n", ">");
col++;
}
row++;
}
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AddHeader("content-disposition", "attachment; filename=Dictionary.xlsx");
context.Response.BinaryWrite(excel.GetAsByteArray());
}
private void AddToList ( Dictionary.DictionaryItem parent, ref List<Dictionary.DictionaryItem> list ) {
list.Add(parent);
if (parent.hasChildren) {
foreach (var item in parent.Children) {
AddToList(item, ref list);
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
<%@ WebHandler Language="C#" Class="DictionaryImport" %>
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Linq;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.language;
public class DictionaryImport : IHttpHandler {
protected List<Language> allLanguages = new List<Language>();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
allLanguages.AddRange(Language.GetAllAsList());
FileInfo file = new FileInfo(context.Server.MapPath("~/App_Data/Dictionary.xlsx"));
var excel = new OfficeOpenXml.ExcelPackage(file);
var sheet = excel.Workbook.Worksheets.First();
foreach (var key in sheet.Cells["A:A"]) {
if (!Dictionary.DictionaryItem.hasKey(key.Value.ToString())) {
context.Response.Write("NO KEY:" + key.Value.ToString() + "\n");
continue;
}
var dictItem = new Dictionary.DictionaryItem(key.Value.ToString());
foreach (var cell in sheet.Cells[key.Start.Row.ToString() + ":" + key.Start.Row.ToString()].Skip(1)) {
if (allLanguages.Any(x => x.CultureAlias.ToLower() == sheet.Cells[cell.Address.Substring(0, 1) + "1"].Value.ToString())) {
var lang = allLanguages.First(x => x.CultureAlias.ToLower() == sheet.Cells[cell.Start.Address.Substring(0, 1) + "1"].Value.ToString());
context.Response.Write(dictItem.key + "-" + lang.CultureAlias + ":" + cell.Value + "\n");
dictItem.setValue(lang.id, cell.Value.ToString().Replace(">", "\n").Trim());
}
}
}
excel.Dispose();
}
public bool IsReusable {
get {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment