zoukankan      html  css  js  c++  java
  • x01.TextProc: 两三分钟完成的一个小工具

    在工作中,遇到这么个问题,需要将 Excel 表中类似 2134-1234-4456 的商品编号输入到单位的程序中,而程序只认 213412344456 这种没有 ‘-’ 的输入。数量比较多,一笔一笔的敲,费时费力不可取,所以转换一下,复制粘贴,不仅可以提高速度,而且也不易出错。并且,由于 Excel 表是别人提供,可能反复遇到此问题,所以写个转换的小工具是必要的。

    直接操作 Excel 吗?问题 ”矮小下“,显然用不着这么麻烦。我的方法是,将商品编号列复制粘贴到记事本中保存为 temp.txt 文件。现在问题一下子就简化为普通的文本处理了。新建一个 WPF 程序,其 MainWindow.xaml 和 MainWindow.xaml.cs 内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <Window
        x:Class="TextProc.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Text Proc"
        Height="380"
        Width="500"
        WindowStartupLocation="CenterScreen">
        <StackPanel>
            <ScrollViewer
                Height="300"
                VerticalScrollBarVisibility="Auto">
                <TextBox
                    Name="txtContent"
                    Margin="5" />
            </ScrollViewer>
            <StackPanel
                Orientation="Horizontal"
                HorizontalAlignment="Right">
                <Button
                    Name="btnSelect"
                    Content="_Select" />
                <Button
                    Name="btnProcess"
                    Content="_Process" />
            </StackPanel>
        </StackPanel>
    </Window>
    MainWindow.xaml
    /**
     * MainWindow.xaml.cs (c) 2015 by x01
     * ----------------------------------
     */
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    
    using Microsoft.Win32;
    
    namespace TextProc
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            string _path = null;
            OpenFileDialog _dlg = new OpenFileDialog();
            
            public MainWindow()
            {
                InitializeComponent();
                
                _dlg.Filter = "*.txt|*.txt|*.*|*.*";
                
                btnProcess.Click += delegate { 
                    if (string.IsNullOrEmpty(_path)) return;
                    var text = File.ReadAllText(_path);
                    text = ProcessText(text);
                    txtContent.Text = text;
                    File.WriteAllText(_path + ".proc", text);
                    MessageBox.Show("Process Text Success!");
                };
                
                btnSelect.Click += delegate { 
                    _dlg.ShowDialog();
                    _path = _dlg.FileName;
                    txtContent.Text = File.ReadAllText(_path);
                };
            }
            
            // 主要的文本处理逻辑,换行作分割,只保留数字,可根据实际情况调整。
            private string ProcessText(string text)
            {
                string [] arr = text.Split('
    ');
                List<string> result = new List<string>();
                foreach (var a in arr) {
                    char[] cs = a.ToCharArray();
                    string s = string.Empty;
                    for (int i = 0; i < cs.Length; i++) {
                        if (cs[i] < '0' || cs[i] > '9') continue;
                        s += cs[i].ToString();
                    }
                    result.Add(s);
                }
                
                string str = string.Empty;
                foreach (var r in result) {
                    str += r + "
    ";
                }
                return str;
            }
        }
    }
    MainWindow.cs

    由于是在 XP 系统用 SharpDev 编写,所以 xaml 文件稍有不同。

    处理后,再复制回 Excel 表中。花个两三分钟就解决了问题,还是不错的。

    在 windows10 和 vs2015 这两个巨无霸来临之际,作为编程爱好者,不要忘了,编程是为了解决问题这一本质,是为记。

    我也是醉了,Ctl + H 即可解决,竟然写了个程序!之所以如此,是因为我几乎从来不用  Ctl+H 来全部替换:一不小心改了不该改的数据,在单位可不是小问题。

    解决问题的方法从来都不是一种,还是予以保留吧,因为我的出发点是解决 Excel 问题,并不一定要操作 Excel。

  • 相关阅读:
    POJ_2513Colored Sticks 字典树+
    hdu1098:Ignatius's puzzle
    hdu1010:Tempter of the Bone 搜索+剪枝
    轻院1875: 蛤玮的财宝
    POJ3069:Saruman's Army
    轻院1874: 蛤玮学计网
    Educational Codeforces Round 18 E. Colored Balls
    浏览器f12的方法下载资源
    把手机上的新浪微博客户端卸载了
    Xmind使用总结
  • 原文地址:https://www.cnblogs.com/china_x01/p/4686409.html
Copyright © 2011-2022 走看看