wpf选择文件

        private void btnOpenXlsx_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Excel Files (*.xlsx)|*.xlsx"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                this.txtXlsxFileName.Text = openFileDialog.FileName;

                using (Xlsx xlsx = new Xlsx(this.txtXlsxFileName.Text))
                {
                    this.cmbSheets.Items.Clear();
                    //List<string> strs = new List<string>();
                    foreach (var item in xlsx.Sheets)
                        //strs.Add(item.Name);
                        this.cmbSheets.Items.Add(item.Name);

                    if (this.cmbSheets.Items.Count > 0)
                    {
                        this.cmbSheets.SelectedIndex = 0;
                    }
                    //.cmbSheetnames.DataSource = strs.ToArray();
                }

            }

            if (-1 != this.cmbSheets.SelectedIndex)
            {
                string xlsx = txtXlsxFileName.Text;
                string shtName = this.cmbSheets.Items[this.cmbSheets.SelectedIndex].ToString();
                List<string> ls = new List<string>();
                foreach (var item in MyCommon.GetTitleLine(xlsx, shtName, 1).Cells)
                    ls.Add(string.Format("【{0}/{1}/{2}】", MyCommon.IntToLetter(item.Key + 1), item.Key + 1, item.Value));
                ic.ItemsSource = ls;

            }

        }

 

using System;
using System.Collections.Generic;
using System.Data;
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.Navigation;
using System.Windows.Shapes;
using AkImportService;

namespace FM
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private ImportConfig config = null;
        private string configPath = null;
        public MainWindow()
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty(Arg.ThisWbName) && !string.IsNullOrEmpty(Arg.ThisShtName))
            {
                this.thisWb.Text = Arg.ThisWbName;
                this.thisSht.Text = Arg.ThisShtName;
            }

            //加载导入配置文件 (含sfBill)
            configPath = AppDomain.CurrentDomain.BaseDirectory + @"ImportConfig\ImportConfig.xml";
            config = new ImportConfig(configPath);
            CreateDb(config);

        }

        private void btnOpenXlsx_Click(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "Excel Files (*.xlsx)|*.xlsx"
            };
            var result = openFileDialog.ShowDialog();
            if (result == true)
            {
                this.txtXlsxFileName.Text = openFileDialog.FileName;

                using (Xlsx xlsx = new Xlsx(this.txtXlsxFileName.Text))
                {
                    this.cmbSheets.Items.Clear();
                    //List<string> strs = new List<string>();
                    foreach (var item in xlsx.Sheets)
                        //strs.Add(item.Name);
                        this.cmbSheets.Items.Add(item.Name);

                    if (this.cmbSheets.Items.Count > 0)
                    {
                        this.cmbSheets.SelectedIndex = 0;
                    }
                    //.cmbSheetnames.DataSource = strs.ToArray();
                }

            }

            if (-1 != this.cmbSheets.SelectedIndex)
            {
                string xlsx = txtXlsxFileName.Text;
                string shtName = this.cmbSheets.Items[this.cmbSheets.SelectedIndex].ToString();
                List<string> ls = new List<string>();
                foreach (var item in MyCommon.GetTitleLine(xlsx, shtName, 1).Cells)
                    ls.Add(string.Format("【{0}/{1}/{2}】", MyCommon.IntToLetter(item.Key + 1), item.Key + 1, item.Value));
                ic.ItemsSource = ls;

            }

        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            /*
            if (
                //string.IsNullOrEmpty(txtXlsxFileName.Text)
                //|| -1 == cmbSheets.SelectedIndex
                //|| 
                string.IsNullOrEmpty(thisWb.Text)
                || string.IsNullOrEmpty(thisSht.Text)
                )
                return;

            if (
                //string.IsNullOrEmpty(flag.Text)
                //|| string.IsNullOrEmpty(output.Text)

                string.IsNullOrEmpty(jzl.Text)
                || string.IsNullOrEmpty(dest.Text)

                )
                return;

            string xlsx = thisWb.Text;
            string shtName = thisSht.Text;

            BaseDbImp db = DbFactory.CreateDbImp(config, "MBSJ");
            db.Table_Config.Table_Struct.Cols[1].Num = MyCommon.LetterToInt(jzl.Text.ToUpper()) - 1;
            //db.Table_Config.Table_Struct.Cols[2].Num = MyCommon.LetterToInt(dest.Text.ToUpper()) - 1;
            int importedCount = new XlsxImportor(xlsx, db, shtName).Import();
            MessageBox.Show(string.Format("导入{0}条记录。", importedCount));


            xlsx = txtXlsxFileName.Text;
            shtName = this.cmbSheets.Items[this.cmbSheets.SelectedIndex].ToString();

            db = DbFactory.CreateDbImp(config, "YSJ");
            var lf = new LFilter(config, "源数据", "列A");
            db.Table_Config.Table_Struct.Cols[1].Num = MyCommon.LetterToInt(flag.Text.ToUpper()) - 1;
            db.Table_Config.Table_Struct.Cols[2].Num = MyCommon.LetterToInt(output.Text.ToUpper()) - 1;
            db.LineFilter = lf;
            importedCount = new XlsxImportor(xlsx, db, shtName).Import();
            MessageBox.Show(string.Format("导入{0}条记录。", importedCount));
            lf.Clear();
            */

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("OBJECT_ID",typeof(string));
            dataTable.Columns.Add("A", typeof(string));
            DataRow dr = dataTable.NewRow();
            dr.ItemArray = new object[] { string.Format("{{{0}}}", Guid.NewGuid().ToString())
                ,dest.Text.ToUpper()
                };
            dataTable.Rows.Add(dr);
            dataTable.TableName = "MBWZ";
            MyCommon.ExecuteNonQueryBySql("delete from MBWZ where 1=1"
                , config.Db_Info.GetConnectionString(), config.Db_Info.Db_Type);
            MyCommon.CopyToServer(dataTable, config.Db_Info.Db_Type, config.Db_Info.GetConnectionString());
            Application.Current.Shutdown(0);
        }

        private void CreateDb(ImportConfig config)
        {

            var dbc = DbFactory.CreateDbDef(config);
            if (!dbc.IsDbExists(config.Db_Info))
                dbc.CreateDb(config.Db_Info);
            dbc.GenerateCreateScript(config);

        }
    }


    /// <summary>
    /// 行过滤器
    /// </summary>
    public class LFilter : ELineFilter
    {

        private static HashSet<string> keyColumnSet;
        private static Dictionary<string, int> colDict;
        private string keyColumnName;
        private ImportConfig config;
        private string tableDispName;
        public LFilter(ImportConfig config, string tableDispName, string keyColumnName)
        {
            this.config = config;
            this.tableDispName = tableDispName;
            this.keyColumnName = keyColumnName;
        }

        public void Clear()
        {
            keyColumnSet = null;
            colDict = null;
        }

        public bool DoFilter(ELine line, out string msg)
        {
            if (null == colDict)
            {
                //var config = new ImportConfig(AppDomain.CurrentDomain.BaseDirectory + @"ImportConfig\ImportConfig.xml");
                //colDict = config.FindTableConfig(config.GetTableName("顺丰正向订单")).Table_Struct.GetDispnameColNums();
                colDict = config.FindTableConfig(config.GetTableName(this.tableDispName)).Table_Struct.GetDispnameColNums();
            }

            if (null == keyColumnSet)
            {
                keyColumnSet = new HashSet<string>();
            }

            string erpdh = line.Cells[colDict[this.keyColumnName]];
            if (keyColumnSet.Contains(erpdh))
            {
                msg = null;// string.Format("ERP单号:{0}已存在。Line:{1}/{2}", erpdh, line.Number, line.Wjm);
                return false;
            }
            else
            {
                keyColumnSet.Add(erpdh);
                msg = "";
                return true;
            }
        }
    }

}
<Window x:Class="FM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FM"
        mc:Ignorable="d"
        Title="极速匹配工具" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Vertical" Background="AliceBlue">
            <StackPanel Orientation="Horizontal" >
                <Button x:Name="btnOk" Content="确定" Click="btnOk_Click"></Button>
                <Button x:Name="btnCancel" Content="取消"></Button>
            </StackPanel>
            <TextBlock x:Name="thisWb" FontSize="16px" ></TextBlock>
            <TextBlock x:Name="thisSht" FontSize="16px" ></TextBlock>

            <StackPanel Orientation="Horizontal">
                <TextBlock Text="基准列" ></TextBlock>
                <TextBox x:Name="jzl" MinWidth="30px"></TextBox>
                <TextBlock Text="目标列" ></TextBlock>
                <TextBox x:Name="dest" MinWidth="30px"></TextBox>
            </StackPanel>
        </StackPanel>
        <StackPanel Grid.Row="1" Background="Aqua" Orientation="Horizontal" >
            <Button Content="打开xlsx" x:Name="btnOpenXlsx" Click="btnOpenXlsx_Click"></Button>
            <TextBlock x:Name="txtXlsxFileName" ></TextBlock>
            <ComboBox x:Name="cmbSheets" />
        </StackPanel>
        <StackPanel Grid.Row="2">
            <ItemsControl x:Name="ic">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"></WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

        </StackPanel>
        <StackPanel Grid.Row="3" Orientation="Horizontal" Background="AliceBlue">
            <TextBlock Text="基准列" ></TextBlock>
            <TextBox x:Name="flag" MinWidth="30px"></TextBox>
            <TextBlock Text="参考输出列" ></TextBlock>
            <TextBox x:Name="output" MinWidth="30px"></TextBox>
        </StackPanel>

    </Grid>

</Window>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值