当前位置:网站首页>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 !";
}
边栏推荐
- What insurance products should be bought for the elderly?
- Interview script of Software Test Engineer
- Thinkphp6 integrated JWT method and detailed explanation of generation, removal and destruction
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
- Introduction to unity shader essentials reading notes Chapter III unity shader Foundation
- 0 basic learning C language - nixie tube dynamic scanning display
- Day05 table
- 不得不会的Oracle数据库知识点(四)
- Cesiumjs 2022^ source code interpretation [8] - resource encapsulation and multithreading
- It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
猜你喜欢
Interview script of Software Test Engineer
Since the "epidemic", we have adhered to the "no closing" of data middle office services
长文综述:大脑中的熵、自由能、对称性和动力学
Network layer - routing
Technical practice online fault analysis and solutions (Part 1)
It's OK to have hands-on 8 - project construction details 3-jenkins' parametric construction
功能:求出菲波那契数列的前一项与后一项之比的极限的 近似值。例如:当误差为0.0001时,函数值为0.618056。
Regular expression of shell script value
What is the GPM scheduler for go?
AI 助力艺术设计抄袭检索新突破!刘芳教授团队论文被多媒体顶级会议ACM MM录用
随机推荐
Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
不得不会的Oracle数据库知识点(三)
Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience
Oracle database knowledge points (IV)
The difference between fetchtype lazy and eagle in JPA
Severity code description the project file line prohibits the display of status error c4996 fopen ('fscanf ', StrCmp): this function or variable may be unsafe The most comprehensive solution
@EnableAsync @Async
Long article review: entropy, free energy, symmetry and dynamics in the brain
[common error] UART cannot receive data error
老姜的特点
AI helps make new breakthroughs in art design plagiarism retrieval! Professor Liu Fang's team paper was employed by ACM mm, a multimedia top-level conference
Makefile judge custom variables
leetcode 121 Best Time to Buy and Sell Stock 买卖股票的最佳时机(简单)
PMP 考试常见工具与技术点总结
HackTheBox-baby breaking grad
Some other configurations on Huawei's spanning tree
Avoid playing with super high conversion rate in material minefields
GUI 应用:socket 网络聊天室
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
Summary of common tools and technical points of PMP examination