当前位置:网站首页>C import Xls data method summary II (save the uploaded file to the DataTable instance object)
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
2022-07-04 01:11:00 【Xiao Zhong wants to learn!!!】
C# Import .xls Data method summary II ( Save the uploaded file to DataTable In the instance object )
Create database instance objects , And create a studentVo Entity class records all uploaded data
// Query out college , major , grade , class Information about : It is used to get the corresponding ID
List<SYS_Academe> dbAcademe = myModel.SYS_Academe.ToList();
List<SYS_Specialty> dbSpecialty = myModel.SYS_Specialty.ToList();
List<SYS_Grade> dbGrade = myModel.SYS_Grade.ToList();
List<SYS_Class> dbClass = myModel.SYS_Class.ToList();
// establish studentVo The object list -》 Used to store imported students / User data
List<studentVo> listStudentVo = new List<studentVo>();
Get the first payroll . And judge whether there is data in the worksheet, and then carry out the following operations
// Get the first sheet
ISheet sheet = workbook.GetSheetAt(0);
// Determine whether there is data in the worksheet
//PhysicalNumberOfRows Number of physical rows obtained , Not including those empty lines ( Interlace ) The situation of
if (sheet.PhysicalNumberOfRows > 0){
}else{
returnJson.Text = " The worksheet is empty , Please check !";
}
1、 Create a DataTable Object instances
DataTable() A table representing data in memory
// Load data into datatable in
// Definition datatable
DataTable dtExcel = new DataTable();
2、 Get the header information of the uploaded file
The first line is generally the description of the imported data template , It's not header data
// Gets the second row in the worksheet ( The first line is the description , The index for 0)
IRow rowHeader = sheet.GetRow(1);
3、 The basic methods used are as follows
FirstCellNum: Gets the subscript of the first cell in a row
LastCellNum: Get the number of columns in a row
FirstRowNum: Get the subscript of the first actual row
LastRowNum: Get the subscript of the last actual line
4、 Get the number of columns and rows of the uploaded file 
The picture above shows 9 Column 3 That's ok
// Get the number of table columns
int cellCount = rowHeader.LastCellNum;
// Get the number of table rows
int rowCount = sheet.LastRowNum + 1;
Get the number of lines +1 It means to skip the header data in the second row and start counting
5、 Get the header of the second line of the file circularly and give it to DataTable Set the same header
First get the name of the header column of the uploaded file , Then add the obtained name to DataTable As header
The number of cycles is the number of file columns obtained
// Cycle through the values of each cell in the header row -- It is equivalent to defining the fields of the database table
for (var i = rowHeader.FirstCellNum; i < cellCount; i++)
{
// establish dataTable Column in
// By traversing each cell in the row , Get the data of each cell in the header row
DataColumn dtColumn = new DataColumn(rowHeader.GetCell(i).StringCellValue);
// Put the data of the obtained Title row into datatable in ;
dtExcel.Columns.Add(dtColumn);
}
6、 Get the data content of the uploaded file
The number of cycles is ; The number of rows of uploaded data , The description line and header line are not included
Judge that there is one isNull Boolean variables The main function of is to record whether there is empty data in the uploaded file , Empty is true Will not be added to the created DataTable Inside , As a result, a lot of blank data comes out
// Query out college , major , grade , class Information about : It is used to get the corresponding ID
List<SYS_Academe> dbAcademe = myModel.SYS_Academe.ToList();
List<SYS_Specialty> dbSpecialty = myModel.SYS_Specialty.ToList();
List<SYS_Grade> dbGrade = myModel.SYS_Grade.ToList();
List<SYS_Class> dbClass = myModel.SYS_Class.ToList();
// establish studentVo The object list -》 Used to store imported students / User data
List<studentVo> listStudentVo = new List<studentVo>();
// Get the first sheet
ISheet sheet = workbook.GetSheetAt(0);
// Determine whether there is data in the worksheet
//PhysicalNumberOfRows Number of physical rows obtained , Not including those empty lines ( Interlace ) The situation of
if (sheet.PhysicalNumberOfRows > 0){
// obtain Excel Data rows in the table
//(sheet.FirstRowNum) The first line is the description ; The second line is the title ; The third line starts with the data information
for (var j = sheet.FirstRowNum + 2; j < rowCount; j++)
{
// Get data row (3,4,5,6,......) data
IRow row = sheet.GetRow(j);
// establish DataTable That's ok
DataRow dtRow = dtExcel.NewRow();
if (row != null)
{
// De blank line variable
bool isNull = true;
// Traverse all cells in a row , fill dtRow in
for (var n = row.FirstCellNum; n < cellCount; n++)
{
if (row.GetCell(n) != null)
{
string cellVal = row.GetCell(n).ToString();
dtRow[n] = cellVal;
// Go to the airport , If there are non empty cells in the row , Variable isNull by false, Otherwise true
if (!string.IsNullOrEmpty(cellVal))
{
isNull = false;
}
}
}
if (!isNull)
{
// take b Non empty data row dtRow fill dtExcel in
dtExcel.Rows.Add(dtRow);
}
}
}
7、 Complete will Excel Table data into DataTable In the code
#region take Excel Table data into DataTable in
// Load data into datatable in
// Definition datatable
DataTable dtExcel = new DataTable();
// Gets the second row in the worksheet ( The first line is the description , The index for 0)
IRow rowHeader = sheet.GetRow(1);
// Get the number of table columns
int cellCount = rowHeader.LastCellNum;
// Get the number of table rows
int rowCount = sheet.LastRowNum + 1;
// Cycle through the values of each cell in the header row -- It is equivalent to defining the fields of the database table
for (var i = rowHeader.FirstCellNum; i < cellCount; i++)
{
// establish dataTable Column in
// By traversing each cell in the row , Get the data of each cell in the header row
DataColumn dtColumn = new DataColumn(rowHeader.GetCell(i).StringCellValue);
// Put the data of the obtained Title row into datatable in ;
dtExcel.Columns.Add(dtColumn);
}
// obtain Excel Data rows in the table
//(sheet.FirstRowNum) The first line is the description ; The second line is the title ; The third line starts with the data information
for (var j = sheet.FirstRowNum + 2; j < rowCount; j++)
{
// Get data row (3,4,5,6,......) data
IRow row = sheet.GetRow(j);
// establish DataTable That's ok
DataRow dtRow = dtExcel.NewRow();
if (row != null)
{
// De blank line variable
bool isNull = true;
// Traverse all cells in a row , fill dtRow in
for (var n = row.FirstCellNum; n < cellCount; n++)
{
if (row.GetCell(n) != null)
{
string cellVal = row.GetCell(n).ToString();
dtRow[n] = cellVal;
// Go to the airport , If there are non empty cells in the row , Variable isNull by false, Otherwise true
if (!string.IsNullOrEmpty(cellVal))
{
isNull = false;
}
}
}
if (!isNull)
{
// take b Non empty data row dtRow fill dtExcel in
dtExcel.Rows.Add(dtRow);
}
}
}
#endregion
}
else {
returnJson.Text = " The worksheet is empty , Please check !";
}
边栏推荐
- On covariance of array and wildcard of generic type
- Future source code view -juc series
- HackTheBox-baby breaking grad
- 【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
- Network layer - routing
- 功能:编写函数fun求s=1^k+2^k +3^k + ......+N^k的值, (1的K次方到N的K次方的累加和)。
- CLP information - how does the digital transformation of credit business change from star to finger?
- 机器学习基础:用 Lasso 做特征选择
- MySQL uses the view to report an error, explain/show can not be issued; lacking privileges for underlying table
- Employees' turnover intention is under the control of the company. After the dispute, the monitoring system developer quietly removed the relevant services
猜你喜欢

技術實踐|線上故障分析及解决方法(上)

打印菱形图案

“疫”起坚守 保障数据中台服务“不打烊”

【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client

Technical practice online fault analysis and solutions (Part 1)

2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
![[prefix and notes] prefix and introduction and use](/img/a6/a75e287ac481559d8f733e6ca3e59c.jpg)
[prefix and notes] prefix and introduction and use
![Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading](/img/d2/99932660298b4a4cddd7e5e69faca1.png)
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading

Future源码一观-JUC系列

【.NET+MQTT】.NET6 环境下实现MQTT通信,以及服务端、客户端的双边消息订阅与发布的代码演示
随机推荐
Introduction to thread pool
Swagger2 quick start and use
leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
On the day when 28K joined Huawei testing post, I cried: everything I have done in these five months is worth it
mysql使用视图报错,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
功能:将主函数中输入的字符串反序存放。例如:输入字符串“abcdefg”,则应输出“gfedcba”。
数据库表外键的设计
Delete all elements with a value of Y. The values of array elements and y are entered by the main function through the keyboard.
Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
[common error] UART cannot receive data error
Day05 表格
String hash, find the string hash value after deleting any character, double hash
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
Technical practice online fault analysis and solutions (Part 1)
机器学习基础:用 Lasso 做特征选择
Unity Shader入门精要读书笔记 第三章 Unity Shader基础
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
A dichotomy of Valentine's Day
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能