From aeb569d1cb8ea2ac7a8411119db28a11b0a0e00f Mon Sep 17 00:00:00 2001 From: dd <822360096@qq.com> Date: Sun, 13 Mar 2022 15:04:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EMiniExcelDynamic=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E5=AF=B9=E5=A4=9Asheet=E9=81=8D=E5=8E=86=E6=8F=90?= =?UTF-8?q?=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MiniExcel/MiniExcelDynamic.cs | 111 ++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/MiniExcel/MiniExcelDynamic.cs diff --git a/src/MiniExcel/MiniExcelDynamic.cs b/src/MiniExcel/MiniExcelDynamic.cs new file mode 100644 index 0000000..0162c00 --- /dev/null +++ b/src/MiniExcel/MiniExcelDynamic.cs @@ -0,0 +1,111 @@ +using MiniExcelLibs.OpenXml; +using MiniExcelLibs.Utils; +using MiniExcelLibs.Zip; +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MiniExcelLibs +{ + public class MiniExcelDynamic + { + FileStream stream; + ExcelOpenXmlZip archive; + ExcelOpenXmlSheetReader excelOpenXmlSheetReader; + public delegate void MyDelegate(string str);//用于输出异常信息 + public MyDelegate ShowMsgHandler; + + public bool Open(string path, IConfiguration configuration = null) + { + bool result = false; + if(!File.Exists(path))return result; + try + { + stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + archive = new ExcelOpenXmlZip(stream); + if (ExcelTypeHelper.GetExcelType(stream, ExcelType.UNKNOWN) != ExcelType.XLSX) return false; + excelOpenXmlSheetReader = new ExcelOpenXmlSheetReader(stream, configuration); + result = true; + } + catch(Exception ex) + { + result = false; + showMsg(ex.Message); + } + return result; + } + + public void Close() + { + stream?.Close(); + archive?.Dispose(); + //excelOpenXmlSheetReader?.Dispose(); + } + + public IEnumerable Query(string sheetName = null, string startCell = "A1") where T : class, new() + { + return excelOpenXmlSheetReader.Query(sheetName, startCell); + } + public IEnumerable Query(bool useHeaderRow = false, string sheetName = null, string startCell = "A1") + { + return excelOpenXmlSheetReader.Query(useHeaderRow, sheetName, startCell); + } + public DataTable QueryAsDataTable(bool useHeaderRow = true, string sheetName = null, string startCell = "A1") + { + if (sheetName == null) /*Issue #279*/ + sheetName = stream.GetSheetNames().First(); + + var dt = new DataTable(sheetName); + var first = true; + var rows = excelOpenXmlSheetReader.Query(useHeaderRow, sheetName, startCell); + + var keys = new List(); + foreach (IDictionary row in rows) + { + if (first) + { + foreach (var key in row.Keys) + { + if (!string.IsNullOrEmpty(key)) // avoid #298 : Column '' does not belong to table + { + var column = new DataColumn(key, typeof(object)) { Caption = key }; + dt.Columns.Add(column); + keys.Add(key); + } + } + + dt.BeginLoadData(); + first = false; + } + + var newRow = dt.NewRow(); + foreach (var key in keys) + { + newRow[key] = row[key]; //TODO: optimize not using string key + } + + dt.Rows.Add(newRow); + } + + dt.EndLoadData(); + return dt; + } + public List GetSheetNames() + { + List sheetNames=new List(); + if (stream == null)return sheetNames; + sheetNames = excelOpenXmlSheetReader.GetWorkbookRels(archive.entries).Select(s => s.Name).ToList(); + + return sheetNames; + } + + private void showMsg(string msg) + { + if(ShowMsgHandler!=null) ShowMsgHandler(msg); + } + } +} -- Gitee