mini-software / MiniExcel

Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet (support Linux, Mac)
https://www.nuget.org/packages/MiniExcel/
Apache License 2.0
2.78k stars 338 forks source link

When reading Excel, can return IDataReader and DataTable to facilitate the import of database. Like ExcelDataReader provide reader.AsDataSet() #216

Closed yfl8910 closed 3 years ago

shps951023 commented 3 years ago

v 0.13.5 support this

e.g

            var path = PathHelper.GetNewTemplateFilePath();
            var value = new[] { new { Test1="1",Test2=2 }, new { Test1 = "3", Test2 = 4 } };
            MiniExcel.SaveAs(path, value);

            {
                var dt = MiniExcel.QueryAsDataTable(path, true);
                var columns = dt.Columns;
                Assert.Equal("Test1", dt.Columns[0].ColumnName);
                Assert.Equal("Test2", dt.Columns[1].ColumnName);
                Assert.Equal("1", dt.Rows[0]["Test1"]);
                Assert.Equal((double)2, dt.Rows[0]["Test2"]);
                Assert.Equal("3", dt.Rows[1]["Test1"]);
                Assert.Equal((double)4, dt.Rows[1]["Test2"]);
            }

            {
                var dt = MiniExcel.QueryAsDataTable(path, false);
                Assert.Equal("Test1", dt.Rows[0]["A"]);
                Assert.Equal("Test2", dt.Rows[0]["B"]);
                Assert.Equal("1", dt.Rows[1]["A"]);
                Assert.Equal((double)2, dt.Rows[1]["B"]);
                Assert.Equal("3", dt.Rows[2]["A"]);
                Assert.Equal((double)4, dt.Rows[2]["B"]);
            }