using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data; namespace SiaSun.LMS.WPFClient.Dialog { /// /// 导入类型 /// public enum ImportType { Excel, Xml } /// /// ImportWindow.xaml 的交互逻辑 /// public partial class ImportExcelDialog : Window { ImportType emImportType = ImportType.Excel; bool _IsCommit = true; //是否提交保存 string _tableConverterCode = null; DataSet dsImport = null; /// /// 导入文件类型 /// public ImportType U_ImportType { set { emImportType = value; } } /// /// 导入的数据集集合 /// public DataSet U_ImportDataSet { get { return dsImport; } set { dsImport = value; } } #region ------构造函数 /// /// 构造函数 /// public ImportExcelDialog(bool IsCommit) { InitializeComponent(); this._IsCommit = IsCommit; } /// /// 构造函数 /// public ImportExcelDialog(bool IsCommit,string TableConverterCode):this(IsCommit) { this._tableConverterCode = TableConverterCode; this.cmbTemplateName.IsEnabled = false; } #endregion /// /// 加载窗体 /// private void Window_Loaded(object sender, RoutedEventArgs e) { //加载所有模板 this.LoadTemplate(); } /// /// 加载所有模板 /// private void LoadTemplate() { try { this.cmbTemplateName.DisplayMemberPath = "TABLE_CONVERTER_NAME"; this.cmbTemplateName.SelectedValuePath = "TABLE_CONVERTER_CODE"; this.cmbTemplateName.ItemsSource = MainApp._I_SystemService.TABLE_CONVERTER_GetList(); //设置默认值 if (!string.IsNullOrEmpty(this._tableConverterCode)) { this.cmbTemplateName.SelectedValue = this._tableConverterCode; } } catch (Exception ex) { MainApp._MessageDialog.ShowException(ex); } } /// /// 按钮事件 /// private void WrapPanel_Click(object sender, RoutedEventArgs e) { Button btn = e.OriginalSource as Button; if (btn != null) { switch (btn.Name) { case "btnOpen": this.OpenFile(); break; case "btnImport": this.ImportFile(); break; case "btnClose": this.Close(); break; } } } /// /// 选择导入文件 /// private void OpenFile() { System.Windows.Forms.OpenFileDialog opDig = new System.Windows.Forms.OpenFileDialog(); //判断导入文件类型 switch (emImportType) { case ImportType.Excel: break; case ImportType.Xml: this.ImportXml(); break; } if (opDig.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.txtFileName.Text = opDig.FileName; } } /// /// 导入文件 /// private void ImportFile() { if (string.IsNullOrEmpty(this.txtFileName.Text) || this.cmbTemplateName.SelectedValue == null) { MainApp._MessageDialog.ShowDialog(Enum.MessageConverter.Null, string.Format("[{0}/{1}]", this.lblTemplateName.Text, this.lblFileName.Text)); return; } //判断导入文件类型 switch (emImportType) { case ImportType.Excel: this.ImportExcel(); break; case ImportType.Xml: this.ImportXml(); break; } } /// /// 导入EXCEL文件 /// private void ImportExcel() { try { //获得Excel数据源 using (DataTable tableExcel = new SiaSun.LMS.Common.Excel().ImportToDataTable(this.txtFileName.Text, true)) { if (tableExcel.Rows.Count > 0) { tableExcel.TableName = "ExcelImport"; string strResult = string.Empty; //获得转换信息实例 //Model.SYS_TABLE_CONVERTER mTABLE_CONVERTER = this.cmbTemplateName.SelectedItem as Model.SYS_TABLE_CONVERTER; //if (mTABLE_CONVERTER != null) //{ // //向父级表和子级表中导入数据 // string strResult = string.Empty; // this.dsImport = MainApp._I_SystemService.SYS_TABLE_CONVERTER_Import(this.cmbTemplateName.SelectedValue.ToString(), tableExcel, out strResult); // //是否提交导入数据 // if (string.IsNullOrEmpty(strResult)) // { // if (this._IsCommit) // { // int intAffect = MainApp._I_SystemService.TABLE_CONVERTER_Save(mTABLE_CONVERTER, dsImport, out strResult); // if (intAffect == 0) // { // MainApp._MessageDialog.ShowDialog(Enum.MessageConverter.AffectCount); // } // else // { // MainApp._MessageDialog.Show(true); // } // return; // } // } // else // { // MainApp._MessageDialog.ShowResult(false, strResult); // } // this.DialogResult = true; // return; //} if (MainApp._I_SystemService.Import_GOODS_MAIN(tableExcel, out strResult)) { MainApp._MessageDialog.ShowResult(true, strResult); } else { MainApp._MessageDialog.ShowResult(false, strResult); } } else { MainApp._MessageDialog.ShowResult(false, "处理物料EXCEL错误"); } } } catch (Exception ex) { MainApp._MessageDialog.ShowException(ex); } } /// /// 导入XML文件 /// private void ImportXml() { if (this.dsImport != null) { this.dsImport.ReadXml(this.txtFileName.Text); //判断是否提交保存 if (this._IsCommit) { int intAffect = MainApp._I_BaseService.Save(dsImport.Tables[0], dsImport.Tables[0].TableName); if (intAffect == 0) { MainApp._MessageDialog.ShowDialog(Enum.MessageConverter.AffectCount); } else { MainApp._MessageDialog.Show(true); } return; } this.DialogResult = true; return; } } } }