Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to import excel to list using NPOI in C #

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how C# uses NPOI to import excel into list. I hope you will get something after reading this article. Let's discuss it together.

The details are as follows

This is to make sure that the entity class receives

/ / Import excel into list/// Stream file stream / transformed Dictionary: for example ("nickname", "nickname") / public static List ExcelToList (this Stream fs, Dictionary list) where T: class, new () {List ts = new List (); IWorkbook workbook = null; ISheet sheet = null; T t = new T () List listName = new List (); try {/ / get the common property of this model var propertys = t.GetType (). GetProperties (). ToList (); workbook = new HSSFWorkbook (fs); if (workbook! = null) {sheet = workbook.GetSheetAt (0) / / read the first sheet, of course, you can also read each sheet if (sheet! = null) {int rowCount = total number of sheet.LastRowNum;// lines if (rowCount > 0) {IRow firstRow = sheet.GetRow (0) / / first line int cellCount = number of firstRow.LastCellNum;// columns / / number of circular columns for (int I = 0; I < cellCount Foreach (var item in list) {if (item.Key.Equals (firstRow.GetCell (I) .StringCellValue)) {/ / replace header firstRow.GetCell (I) .SetCellValue (item.Value) }} / / get the replaced header var s = firstRow.GetCell (I) .StringCellValue / / add to listname listName.Add (s);} for (int I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report