欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 读取Excel,一波华丽的操作

程序员文章站 2022-08-09 16:04:40
C# 读取Excel,其实有很多方法。但是今天要来一波华丽的操作。 先看效果: 以上这波操作使用了 ExcelDataReader 和 ExcelDataReader.DataSet 完成的。 ExcelDataReader 是一个快速读取 Excel 的 C# 库。使用简单,读取速度比较快,感觉比 ......

c# 读取excel,其实有很多方法。但是今天要来一波华丽的操作。

先看效果:

C# 读取Excel,一波华丽的操作

以上这波操作使用了 exceldatareader 和 exceldatareader.dataset 完成的。

exceldatareader 是一个快速读取 excel 的 c# 库。使用简单,读取速度比较快,感觉比 npoi 快一点。但是遗憾的是只能读 excel 没有写的操作。

以上这波操作的全部代码:

using exceldatareader;
using system;
using system.io;
using system.windows.forms;

namespace excelfastread
{
    public partial class frmmain : form
    {
        public frmmain()
        {
            initializecomponent();
        }

        private void button1_click(object sender, eventargs e)
        {
            openfiledialog dialog = new openfiledialog();
            dialog.filter = "(excel 97-03)|*.xls|(excel 2007)|*.xlsx|all|*.*";
            if (dialogresult.ok != dialog.showdialog())
            {
                return;
            }

            string strfilename = dialog.filename;
            if (string.isnullorwhitespace(strfilename))
            {
                return;
            }

            using (var stream = file.open(strfilename, filemode.open, fileaccess.read))
            {

                // auto-detect format, supports:
                //  - binary excel files (2.0-2003 format; *.xls)
                //  - openxml excel files (2007 format; *.xlsx)
                using (var reader = excelreaderfactory.createreader(stream))
                {

                    // choose one of either 1 or 2:

                    // 1. use the reader methods
                    do
                    {
                        while (reader.read())
                        {
                            // reader.getdouble(0);
                        }
                    } while (reader.nextresult());

                    exceldatasetconfiguration configuration = new exceldatasetconfiguration()
                    {

                        // gets or sets a value indicating whether to set the datacolumn.datatype 
                        // property in a second pass.
                        //usecolumndatatype = true,

                        // gets or sets a callback to obtain configuration options for a datatable. 
                        configuredatatable = (tablereader) => new exceldatatableconfiguration()
                        {

                            // gets or sets a value indicating the prefix of generated column names.
                            //emptycolumnnameprefix = "column",

                            // gets or sets a value indicating whether to use a row from the 
                            // data as column names.
                            useheaderrow = true,

                            // gets or sets a callback to determine which row is the header row. 
                            // only called when useheaderrow = true.
                            //readheaderrow = (rowreader) => {
                            //    // f.ex skip the first row and use the 2nd row as column headers:
                            //    rowreader.read();
                            //},

                            // gets or sets a callback to determine whether to include the 
                            // current row in the datatable.
                            //filterrow = (rowreader) => {
                            //    return true;
                            //},

                            // gets or sets a callback to determine whether to include the specific
                            // column in the datatable. called once per column after reading the 
                            // headers.
                            //filtercolumn = (rowreader, columnindex) => {
                            //    return true;
                            //}
                        }
                    };
                    var result = reader.asdataset(configuration);

                    // 2. use the asdataset extension method
                    //var result = reader.asdataset();

                    dgvlist.datasource = result.tables[0];

                    // the result of each spreadsheet is in result.tables
                }
            }

        }
    }
}

 

exceldatareader 项目地址

exceldatareader nuget包管理

exceldatareader.dataset nuget包管理