WPF中读取Excel文件的内容

演示效果

实现方案

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

Epplus.dll

Excel.dll

ICSharpCode.SharpZipLib.dll

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

3.然后看代码内容

附上源码

cs 复制代码
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项目

相关推荐
失败尽常态5232 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
code_shenbing6 小时前
WPF实现打印机控制及打印
wpf
杜大哥9 小时前
如何在WPS打开的word、excel文件中,使用AI?
人工智能·word·excel·wps
@LitterFisher11 小时前
Excell 代码处理
前端·javascript·excel
winfredzhang11 小时前
Python实战:Excel中文转拼音工具开发教程
python·安全·excel·汉字·pinyin·缩写
oh,huoyuyan13 小时前
火语言RPA--Excel插入空列
excel·rpa
企鹅侠客1 天前
开源免费文档翻译工具 可支持pdf、word、excel、ppt
人工智能·pdf·word·excel·自动翻译
界面开发小八哥1 天前
界面组件DevExpress WPF中文教程:Grid - 如何显示和隐藏列?
wpf·界面控件·devexpress·ui开发·.net9
虚假程序设计1 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf