1. 程式人生 > >WPF 批量讀取資料夾下TXT檔案寫入EXCEL

WPF 批量讀取資料夾下TXT檔案寫入EXCEL

 把翻到的很多以前寫的程式做個記錄,記錄學習過程,同時也方便以後查閱

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