using IronIntel.Contractor; using IronIntel.Contractor.ExportExcel; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace LanguageExtractTool { public partial class F_LanguageExcel : Form { public F_LanguageExcel() { InitializeComponent(); } private void F_LanguageExcel_Load(object sender, EventArgs e) { List lan = new List(); lan.Add(new Languages { ID = "all", Name = "All" }); lan.Add(new Languages { ID = "en", Name = "English" }); lan.Add(new Languages { ID = "fr", Name = "France" }); lan.Add(new Languages { ID = "zh", Name = "Chinese" }); lan.Add(new Languages { ID = "pt", Name = "Português" }); lan.Add(new Languages { ID = "en-au", Name = "English (Australia)" }); lan.Add(new Languages { ID = "en-Components", Name = "English (Components)" }); this.cbx_languages.ValueMember = "ID"; this.cbx_languages.DisplayMember = "Name"; this.cbx_languages.DataSource = lan; } private void button1_Click(object sender, EventArgs e) { if (ofd.ShowDialog() == DialogResult.OK) { txt_src.Text = ofd.FileName; } } private void btn_export_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txt_src.Text)) { MessageBox.Show("请选择源文件"); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Excel files(*.xlsx)|*.xlsx"; sfd.FilterIndex = 1; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { List ls = LoadLanguagesXml(); byte[] buffer = ExportLanguages(ls); try { using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write)) { fs.Write(buffer, 0, buffer.Length); MessageBox.Show("导出成功"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } private void btn_import_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txt_src.Text)) { MessageBox.Show("请选择源文件"); return; } OpenFileDialog sfd = new OpenFileDialog(); sfd.Title = "Excel文件"; sfd.FileName = ""; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); sfd.Filter = "Excel文件| *.xlsx;*.xls"; sfd.ValidateNames = true; sfd.CheckFileExists = true; sfd.CheckPathExists = true; if (sfd.ShowDialog() == DialogResult.OK) { try { using (FileStream fs = new FileStream(sfd.FileName, FileMode.Open, FileAccess.Read)) { byte[] fileBuffer = new byte[fs.Length]; fs.Read(fileBuffer, 0, Convert.ToInt32(fs.Length)); DataTable dt = new ImportFromExcel().LoadExcelData(fileBuffer); if (dt == null || dt.Rows.Count == 0) return; List ls = LoadLanguagesDataTable(dt); ImportExcel(ls); MessageBox.Show("导入成功"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } private List LoadLanguagesXml() { XmlDocument doc = new XmlDocument(); doc.Load(txt_src.Text); List ls = new List(); foreach (XmlNode node in doc.DocumentElement.ChildNodes) { if (string.Equals(node.Name, "Category", StringComparison.OrdinalIgnoreCase)) { foreach (XmlNode codenode in node.ChildNodes) { LanguagesItem li = new LanguagesItem(); li.ID = codenode.Name; foreach (XmlNode node1 in codenode.ChildNodes) { if (string.IsNullOrWhiteSpace(node1.InnerText)) { continue; } string lgid = node1.Name; string lgvalue = node1.InnerText; lgvalue = lgvalue.Replace("\n", "
"); lgvalue = Regex.Replace(lgvalue, "^ | $", ""); if (string.Compare(lgid, "en", true) == 0) li.English = lgvalue; else if (string.Compare(lgid, "fr", true) == 0) li.France = lgvalue; else if (string.Compare(lgid, "zh", true) == 0) li.Chinese = lgvalue; else if (string.Compare(lgid, "pt", true) == 0) li.Portuguese = lgvalue; else if (string.Compare(lgid, "en-au", true) == 0) li.EnglishAU = lgvalue; else if (string.Compare(lgid, "en-Components", true) == 0) li.EnglishComponents = lgvalue; } ls.Add(li); } } } return ls; } private byte[] ExportLanguages(List ls) { string clgid = cbx_languages.SelectedValue.ToString(); ExportToExcel ete = new ExportToExcel(); DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(string)); dt.Columns.Add("English", typeof(string)); if (clgid == "all") { dt.Columns.Add("English", typeof(string)); dt.Columns.Add("France", typeof(string)); dt.Columns.Add("Chinese", typeof(string)); dt.Columns.Add("Português", typeof(string)); dt.Columns.Add("English(Components)", typeof(string)); } else if (clgid == "en") dt.Columns.Add("English", typeof(string)); else if (clgid == "fr") dt.Columns.Add("France", typeof(string)); else if (clgid == "zh") dt.Columns.Add("Chinese", typeof(string)); else if (clgid == "pt") dt.Columns.Add("Português", typeof(string)); else if (clgid == "en-au") dt.Columns.Add("English (Australia)", typeof(string)); else if (clgid == "en-Components") dt.Columns.Add("English (Components)", typeof(string)); foreach (var item in ls) { DataRow row = dt.NewRow(); row[0] = item.ID; row[1] = item.English; if (clgid == "all") { row[2] = item.English; row[3] = item.France; row[4] = item.Chinese; row[5] = item.Portuguese; row[6] = item.EnglishAU; row[7] = item.EnglishComponents; } else if (clgid == "en") row[2] = item.English; else if (clgid == "fr") row[2] = item.France; else if (clgid == "zh") row[2] = item.Chinese; else if (clgid == "pt") row[2] = item.Portuguese; else if (clgid == "en-au") row[2] = item.EnglishAU; else if (clgid == "en-Components") row[2] = item.EnglishComponents; dt.Rows.Add(row); } double[] widths = new double[] { 200d, 200d, 200d, 200d, 200d, 200d, 200d, 200d }; byte[] data = ete.CreateExcel(dt, null, widths, null); return data; } private List LoadLanguagesDataTable(DataTable dt) { List ls = new List(); string clgid = cbx_languages.SelectedValue.ToString(); foreach (DataRow dr in dt.Rows) { LanguagesItem li = new LanguagesItem(); li.ID = dr["ID"].ToString(); li.English = DataRowValue(dr, "en"); if (clgid == "all") { li.English = DataRowValue(dr, "en"); li.France = DataRowValue(dr, "fr"); li.Chinese = DataRowValue(dr, "zh"); li.Portuguese = DataRowValue(dr, "pt"); li.Portuguese = DataRowValue(dr, "en-au"); li.EnglishComponents = DataRowValue(dr, "en-Components"); } else if (clgid == "en") li.English = DataRowValue(dr, clgid); else if (clgid == "fr") li.France = DataRowValue(dr, clgid); else if (clgid == "zh") li.Chinese = DataRowValue(dr, clgid); else if (clgid == "pt") li.Portuguese = DataRowValue(dr, clgid); else if (clgid == "en-au") li.EnglishAU = DataRowValue(dr, clgid); else if (clgid == "en-Components") li.EnglishComponents = DataRowValue(dr, clgid); ls.Add(li); } return ls; } private void ImportExcel(List ls) { if (ls == null || ls.Count == 0) return; string clgid = cbx_languages.SelectedValue.ToString(); XmlDocument doc = new XmlDocument(); doc.Load(txt_src.Text); foreach (XmlNode node in doc.DocumentElement.ChildNodes) { if (string.Equals(node.Name, "Category", StringComparison.OrdinalIgnoreCase)) { foreach (XmlNode codenode in node.ChildNodes) { string id = codenode.Name; LanguagesItem lan = ls.FirstOrDefault(m => m.ID == id); if (lan == null) continue; foreach (XmlNode node1 in codenode.ChildNodes) { string lgid = node1.Name; string lgvalue = node1.InnerText; if (clgid == "all") { string text = LanguagesText(lgid, lan); if (!string.IsNullOrEmpty(text)) node1.InnerText = text; } else { if (clgid == lgid) { string text = LanguagesText(clgid, lan); if (!string.IsNullOrEmpty(text)) node1.InnerText = text; } } } } } } doc.Save(txt_src.Text); } private string DataRowValue(DataRow dr, string lgid) { string value = string.Empty; if (lgid == "en") value = "English"; else if (lgid == "fr") value = "France"; else if (lgid == "zh") value = "Chines"; else if (lgid == "pt") value = "Português"; else if (lgid == "en-au") value = "English (Australia)"; else if (lgid == "en-Components") value = "English (Components)"; else value = "English"; string v = dr[value].ToString(); v = Regex.Replace(v, "^|$", " "); return v; } private string LanguagesText(string lgid, LanguagesItem lan) { string text = string.Empty; if (lgid == "en" && !string.IsNullOrEmpty(lan.English)) text = lan.English; else if (lgid == "fr" && !string.IsNullOrEmpty(lan.France)) text = lan.France; else if (lgid == "zh" && !string.IsNullOrEmpty(lan.Chinese)) text = lan.Chinese; else if (lgid == "pt" && !string.IsNullOrEmpty(lan.Portuguese)) text = lan.Portuguese; else if (lgid == "en-au" && !string.IsNullOrEmpty(lan.EnglishAU)) text = lan.EnglishAU; else if (lgid == "en-Components" && !string.IsNullOrEmpty(lan.EnglishComponents)) text = lan.EnglishComponents; return text; } private class Languages { public string ID { get; set; } public string Name { get; set; } } private class LanguagesItem { public string ID { get; set; } public string English { get; set; } public string EnglishAU { get; set; } public string EnglishComponents { get; set; } public string France { get; set; } public string Chinese { get; set; } public string Portuguese { get; set; } } } }