csharp 阅读Excel文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 阅读Excel文件相关的知识,希望对你有一定的参考价值。

// API's  used:  https://exceldatareader.codeplex.com/


// Reading an XLSX file

         private static void ImportValueSetAttributeFile( string filePath)
        {
            FileStream stream = File .Open(filePath, FileMode.Open, FileAccess.Read);

            // Reading from a OpenXml Excel file (2007 format; *.xlsx)
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            // DataSet - The result of each spreadsheet will be created in the result.Tables
            DataSet result = excelReader.AsDataSet();

            // Free resources (IExcelDataReader is IDisposable)
            excelReader.Close();

            var connectionString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
            var database = ConfigurationManager.AppSettings["database" ];
            var mongoAccess = new MongoDataAccess(connectionString, database);

            var cdm = new BaseDataManager();

            int ind = 0;

            for ( int i = 0; i < result.Tables.Count; i++)
            {
                int row_no = 1;
                while (row_no < result.Tables[ind].Rows.Count) // ind is the index of table
                    // (sheet name) which you want to convert to csv
                {
                    var currRow = result.Tables[ind].Rows[row_no];
                    var valueSetAttribute = new ValueSetAttribute()
                        {
                            CmsId = currRow[0].ToString(),
                            NqfNumber = currRow[1].ToString(),
                            ValueSetName = currRow[2].ToString(),
                            ValueSetOid = currRow[3].ToString(),
                            Definition = currRow[4].ToString(),
                            QdmCategory = currRow[5].ToString(),
                            Expansion = currRow[6].ToString(),
                            Code = currRow[7].ToString(),
                            Description = currRow[8].ToString(),
                            CodeSystem = currRow[9].ToString(),
                            CodeSystemOid = currRow[10].ToString(),
                            CodeSystemVersion = currRow[11].ToString()
                        };

                    cdm.AddRecords< ValueSetAttribute>(valueSetAttribute, "ValueSetLookup");

                    row_no++;
                }
                ind++;
            }
        }

以上是关于csharp 阅读Excel文件的主要内容,如果未能解决你的问题,请参考以下文章

csharp Excel阅读器

csharp 阅读Excel

csharp Excel文件构造函数C#

csharp Excel文件构造函数C#NET Core

csharp 读取Excel文件并插入带有序列化的List

csharp 例如-CSHARP-GroupDocs.AssemblyExamples-GenerateReport-GenerateBulletedListFromJsoninOpenExcel.c