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项目

相关推荐
_大龄28 分钟前
前端解析excel
前端·excel
johnny23314 小时前
智能电子表格:Airtable、NocoDB、teable、APITable
excel
2501_9307077820 小时前
如何使用C#代码在Excel 文件中添加工作表
excel
shouchaobao1 天前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
dotent·1 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
allbs1 天前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
m5655bj2 天前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
温轻舟2 天前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
WarPigs2 天前
Unity编辑器开发笔记
unity·编辑器·excel
allbs2 天前
spring boot项目excel导出功能封装——3.图表导出
spring boot·后端·excel