当前位置: 移动技术网 > IT编程>开发语言>.net > C# 读取Excel,一波华丽的操作

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

2018年10月22日  | 移动技术网IT编程  | 我要评论

张小盒表情,古巴英雄,arm 嵌入式

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包管理

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网