真的是这样的吗_

真的是这样的吗_

演示效果

WPF中读取Excel文件的内容-LMLPHP

WPF中读取Excel文件的内容-LMLPHP

实现方案

1.首先导入需要的Dll(这部分可能需要你自己搜一下)

Epplus.dll

Excel.dll

ICSharpCode.SharpZipLib.dll

2.在你的解决方案的的依赖项->添加引用->浏览->选择1中的这几个Dll点击确定。(添加依赖)

3.然后看代码内容

WPF中读取Excel文件的内容-LMLPHP

附上源码

using Excel;
using System.Data;
using System.IO;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
            FileStream excelListStream = File.Open(AppDomain.CurrentDomain.BaseDirectory + "Data.xlsx", FileMode.Open, FileAccess.Read, FileShare.Read);
            var excelListStreamRead = ExcelReaderFactory.CreateOpenXmlReader(excelListStream);
            DataSet excelListResult = excelListStreamRead.AsDataSet();
            string Data = "";
            for (int i = 1; i < excelListResult.Tables[0].Rows.Count; i++)
            {
                string _info;
                _info = 
                excelListResult.Tables[0].Rows[0][0].ToString() + ":" + excelListResult.Tables[0].Rows[i][0].ToString() +
                excelListResult.Tables[0].Rows[0][1].ToString() + ":" + excelListResult.Tables[0].Rows[i][1].ToString() +
                excelListResult.Tables[0].Rows[0][2].ToString() + ":" + excelListResult.Tables[0].Rows[i][2].ToString() +
                excelListResult.Tables[0].Rows[0][3].ToString() + ":" + excelListResult.Tables[0].Rows[i][3].ToString()+"\n";
                Data += _info;

            }
            content.Text = Data;//content是我文本框的名字
        }
    }
}

4.赶紧去试试吧,用Excel管理起来你的WPF项目

06-08 09:21