private static Tuple <List <ResxStrings>, List <string> > ReadExcelData(string xlsxPath) { var list = new List <ResxStrings>(); var langs = new List <string>(); using (XLWorkbook wb = new XLWorkbook(xlsxPath)) { var sht = wb.Worksheets.First(); int col = 3; while (!sht.Cell(1, col).IsEmpty()) { langs.Add(sht.Cell(1, col).Value.ToString()); col++; } int row = 2; while (!sht.Cell(row, 1).IsEmpty()) { ResxStrings data = new ResxStrings() { Key = sht.Cell(row, 1).Value.ToString(), Comment = sht.Cell(row, 2).Value.ToString() }; for (int i = 0; i < langs.Count; i++) { data.Strings.Add(langs[i], sht.Cell(row, i + 3).Value.ToString()); } list.Add(data); row++; } } return(Tuple.Create(list, langs)); }
//REF: http://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx private static void ConvExcelToResx(string xlsxPath) { var list = new List <ResxStrings>(); var langs = new List <string>(); using (XLWorkbook wb = new XLWorkbook(xlsxPath)) { var sht = wb.Worksheets.First(); int col = 3; while (!sht.Cell(1, col).IsEmpty()) { langs.Add(sht.Cell(1, col).Value.ToString()); col++; } int row = 2; while (!sht.Cell(row, 1).IsEmpty()) { ResxStrings data = new ResxStrings() { Key = sht.Cell(row, 1).Value.ToString(), Comment = sht.Cell(row, 2).Value.ToString() }; for (int i = 0; i < langs.Count; i++) { data.Strings.Add(langs[i], sht.Cell(row, i + 3).Value.ToString()); } list.Add(data); row++; } } //Gen resx //string path = Path.GetDirectoryName(xlsxPath); //if (!Directory.Exists(path)) //{ // Directory.CreateDirectory(path); //} string path = System.IO.Directory.GetCurrentDirectory(); path = Path.Combine(path, @"data\resdata"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } else { Directory.Delete(path, true); Directory.CreateDirectory(path); } string pattern = "Resources";//Path.GetFileNameWithoutExtension(xlsxPath); foreach (string lang in langs) { string resxPath = Path.Combine(path, pattern + (lang != "DEFAULT" ? "." + lang : string.Empty) + ".resx"); using (ResXResourceWriter rsxw = new ResXResourceWriter(resxPath)) { foreach (var data in list) { ResXDataNode node = new ResXDataNode(data.Key, data.Strings[lang]); node.Comment = data.Comment; rsxw.AddResource(node); } rsxw.Generate(); rsxw.Close(); } } }