using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using IronIntel.Contractor; using IronIntel.Contractor.ExportExcel; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace LanguageExtractTool { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { List lan = new List(); lan.Add(new Languages { ID = "all", Name = "ALL" }); lan.Add(new Languages { ID = "en-us", Name = "English (United States)" }); lan.Add(new Languages { ID = "en-ca", Name = "English (Canada)" }); lan.Add(new Languages { ID = "en-au", Name = "English (Australia)" }); lan.Add(new Languages { ID = "fr-fr", Name = "français (France)" }); lan.Add(new Languages { ID = "fr-ca", Name = "français (Canada)" }); lan.Add(new Languages { ID = "zh-cn", Name = "中文" }); lan.Add(new Languages { ID = "es-es", Name = "Spain" }); this.cbx_languages.ValueMember = "ID"; this.cbx_languages.DisplayMember = "Name"; this.cbx_languages.DataSource = lan; } private void button1_Click_1(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 void btn_export_mobile_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 = LoadLanguagesMobileXml(); 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_mobile_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); ImportExcelMobile(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-us", true) == 0) li.EnglishUS = lgvalue; else if (string.Compare(lgid, "en-au", true) == 0) li.EnglishAU = lgvalue; else if (string.Compare(lgid, "en-ca", true) == 0) li.EnglishCA = lgvalue; else if (string.Compare(lgid, "fr-fr", true) == 0) li.France = lgvalue; else if (string.Compare(lgid, "fr-ca", true) == 0) li.FranceCA = lgvalue; else if (string.Compare(lgid, "zh-cn", true) == 0) li.Chinese = lgvalue; else if (string.Compare(lgid, "es-es", true) == 0) li.Spain = lgvalue; } ls.Add(li); } } } return ls; } private List LoadLanguagesMobileXml() { XmlDocument doc = new XmlDocument(); doc.Load(txt_src.Text); List ls = new List(); foreach (XmlNode node in doc.DocumentElement.ChildNodes) { LanguagesItem li = new LanguagesItem(); li.ID = node.Name; foreach (XmlNode node1 in node.ChildNodes) { if (string.IsNullOrWhiteSpace(node1.InnerText)) { continue; } string lgid = node1.Name; string lgvalue = node1.InnerText; if (string.Compare(lgid, "en-us", true) == 0) li.EnglishUS = lgvalue; else if (string.Compare(lgid, "en-au", true) == 0) li.EnglishAU = lgvalue; else if (string.Compare(lgid, "en-ca", true) == 0) li.EnglishCA = lgvalue; else if (string.Compare(lgid, "fr-fr", true) == 0) li.France = lgvalue; else if (string.Compare(lgid, "fr-ca", true) == 0) li.FranceCA = lgvalue; else if (string.Compare(lgid, "zh-cn", true) == 0) li.Chinese = lgvalue; else if (string.Compare(lgid, "es-es", true) == 0) li.Spain = 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 (United States)", typeof(string)); if (clgid == "all") { dt.Columns.Add("English (Canada)", typeof(string)); dt.Columns.Add("English (Australia)", typeof(string)); dt.Columns.Add("français (France)", typeof(string)); dt.Columns.Add("français (Canada)", typeof(string)); dt.Columns.Add("中文", typeof(string)); dt.Columns.Add("Spain", typeof(string)); } else if (clgid == "en-ca") dt.Columns.Add("English (Canada)", typeof(string)); else if (clgid == "en-au") dt.Columns.Add("English (Australia)", typeof(string)); else if (clgid == "fr-fr") dt.Columns.Add("français (France)", typeof(string)); else if (clgid == "fr-ca") dt.Columns.Add("français (Canada)", typeof(string)); else if (clgid == "zh-cn") dt.Columns.Add("中文", typeof(string)); else if (clgid == "es-es") dt.Columns.Add("Spain", typeof(string)); foreach (var item in ls) { DataRow row = dt.NewRow(); row[0] = item.ID; row[1] = item.EnglishUS; if (clgid == "all") { row[2] = item.EnglishCA; row[3] = item.EnglishAU; row[4] = item.France; row[5] = item.FranceCA; row[6] = item.Chinese; row[7] = item.Spain; } else if (clgid == "en-ca") row[2] = item.EnglishCA; else if (clgid == "en-au") row[2] = item.EnglishAU; else if (clgid == "fr-fr") row[2] = item.France; else if (clgid == "fr-ca") row[2] = item.FranceCA; else if (clgid == "zh-cn") row[2] = item.Chinese; else if (clgid == "es-es") row[2] = item.Spain; 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.EnglishUS = DataRowValue(dr, "en-us"); if (clgid == "all") { li.EnglishCA = DataRowValue(dr, "en-ca"); li.EnglishAU = DataRowValue(dr, "en-au"); li.France = DataRowValue(dr, "fr-fr"); li.FranceCA = DataRowValue(dr, "fr-ca"); li.Chinese = DataRowValue(dr, "zh-cn"); li.Spain = DataRowValue(dr, "es-es"); } else if (clgid == "en-ca") li.EnglishCA = DataRowValue(dr, clgid); else if (clgid == "en-au") li.EnglishAU = DataRowValue(dr, clgid); else if (clgid == "fr-fr") li.France = DataRowValue(dr, clgid); else if (clgid == "fr-ca") li.FranceCA = DataRowValue(dr, clgid); else if (clgid == "zh-cn") li.Chinese = DataRowValue(dr, clgid); else if (clgid == "es-es") li.Spain = DataRowValue(dr, clgid); ls.Add(li); } return ls; } private string DataRowValue(DataRow dr, string lgid) { string value = string.Empty; if (lgid == "en-ca") value = "English (Canada)"; else if (lgid == "en-au") value = "English (Australia)"; else if (lgid == "fr-fr") value = "français (France)"; else if (lgid == "fr-ca") value = "français (Canada)"; else if (lgid == "zh-cn") value = "中文"; else if (lgid == "es-es") value = "Spain"; else value = "English (United States)"; string v = dr[value].ToString(); v = Regex.Replace(v, "^|$", " "); return v; } 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 void ImportExcelMobile(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) { string id = node.Name; LanguagesItem lan = ls.FirstOrDefault(m => m.ID == id); if (lan == null) continue; foreach (XmlNode node1 in node.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 LanguagesText(string lgid, LanguagesItem lan) { string text = string.Empty; if (lgid == "en-us" && !string.IsNullOrEmpty(lan.EnglishUS)) text = lan.EnglishUS; else if (lgid == "en-ca" && !string.IsNullOrEmpty(lan.EnglishCA)) text = lan.EnglishCA; else if (lgid == "en-au" && !string.IsNullOrEmpty(lan.EnglishAU)) text = lan.EnglishAU; else if (lgid == "fr-fr" && !string.IsNullOrEmpty(lan.France)) text = lan.France; else if (lgid == "fr-ca" && !string.IsNullOrEmpty(lan.FranceCA)) text = lan.FranceCA; else if (lgid == "zh-cn" && !string.IsNullOrEmpty(lan.Chinese)) text = lan.Chinese; else if (lgid == "es-es" && !string.IsNullOrEmpty(lan.Spain)) text = lan.Spain; return text; } } class Languages { public string ID { get; set; } public string Name { get; set; } } class LanguagesItem { public string ID { get; set; } public string EnglishUS { get; set; } public string EnglishCA { get; set; } public string EnglishAU { get; set; } public string France { get; set; } public string FranceCA { get; set; } public string Chinese { get; set; } public string Spain { get; set; } } }