WPF 批量读取文件夹下TXT文件写入EXCEL

2017-09-14 08:05:05来源:CSDN作者:u011413793人点击

分享

 把翻到的很多以前写的程序做个记录,记录学习过程,同时也方便以后查阅


https://github.com/Yiomo/ReadInfo

该APP会读取选定文件夹下的TXT文件并且直接将信息按行写入当前目录下的excel表格

PS.需要excel2010以上的组件&& NET4.0以上&&Microsoft.Office.Interop.Excel引用后需将嵌入互操作属性设置为true


1.定义控件

       <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Height="51" Margin="29,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="27" Visibility="Hidden"/>
        <Button x:Name="button" Content="OpenfileDLG" HorizontalAlignment="Left" Height="54" Margin="54,234,0,0" VerticalAlignment="Top" Width="114" Click="button_Click"/>
        <Button x:Name="button1" Content="OpenfolderDLG" Margin="49,98,55,27" Click="button1_Click"/>
        <ListBox x:Name="listBox" Height="60" Margin="61,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="30" Visibility="Hidden"/>
        <TextBlock x:Name="textBlock1" Margin="96,10,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" Width="44" Height="60" VerticalAlignment="Top" Visibility="Hidden"/>


2.引用

using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;


3.打开TXT文件

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
            openfile.DefaultExt = ".txt";
            openfile.Filter = "Text documents (.txt)|*.txt";
            bool? result = openfile.ShowDialog();
            if (result == true)
            {
                textBlock.Text = openfile.FileName;
                StreamReader readfile = new StreamReader(openfile .FileName);
                textBlock.Text = readfile.ReadToEnd();
            }
         }


//////////该功能在APP里面省略了,我也不知道为什么当初放在APP里面


4.遍历文件夹下TXT文件并写入excel

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog openfolder = new FolderBrowserDialog();
            DialogResult result = openfolder.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            //textBlock.Text = openfolder.SelectedPath.Trim();
            DirectoryInfo TheFolder = new DirectoryInfo(openfolder.SelectedPath);
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                if (NextFile.Name .Contains(".txt")||NextFile.Name.Contains(".TXT"))
                {
                    StreamReader readfile = new StreamReader(openfolder .SelectedPath+"//"+NextFile.Name);
                    listBox.Items.Add(NextFile.Name);
                }
            }
            int a = listBox.Items.Count;//统计多少文件

            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Visible = false;
            app.UserControl = true;
            Workbook workbook;
            Worksheet worksheet;
            workbook = app.Workbooks.Add(System.Reflection.Missing.Value);
            worksheet = workbook.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            worksheet.Name = "test";
            for (int i = 0; i < a; i++)//逐行写入
            {
                //textBlock1.Text = "";
                textBlock.Text = a.ToString();
                string[] lines;
                lines = File.ReadAllLines(openfolder.SelectedPath + "//" + listBox.Items[i].ToString());
                for (int j = 0; j < lines.Length; j++)
                {
                    worksheet.Cells[i + 1, j + 1] = lines[j];
                }
            }
            workbook.SaveAs(openfolder.SelectedPath+"//"+"sum.xls", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            workbook.Close(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
            System.Windows.MessageBox.Show("Done");
        }


//////////////////////////***************************//////////////////////////////



y

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台