如何在ASP.NET C#中将数据从excel导入到类中 [英] How to import data from excel to class in ASP.NET C#

查看:101
本文介绍了如何在ASP.NET C#中将数据从excel导入到类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我在C#中有一个ASP.NET Web应用程序。



I想要使用linq将数据从excel导入到Repeater。



excel文件包含公司徽标(图像),空行和包含列名和行名的多个表。



示例:

 ------------------- -------------------------------------------------- -------- 
ClientInfo | FieldInfo | RigInfo |
---------------------------------------------- -------------------------------
姓名| abc |领域| f1 |钻机| r1 |
---------------------------------------------- -------------------------------
Div | d1 |领域| f2 |钻机| r3 |
---------------------------------------------- -------------------------------

----------- -------------------------------------------------- ----------------
ClientInfo | FieldInfo | RigInfo |
---------------------------------------------- -------------------------------
姓名| abs |领域| f3 |钻机| r2 |
---------------------------------------------- -------------------------------
Div | d3 |领域| f4 |钻机| r4 |
---------------------------------------------- -------------------------------





我尝试过:



 公开  ImportFromExcel 
{
public string 客户端{ get ; set ; }
public string 分部{ get ; set ; }
public string Superintendent { get ; set ; }
public string GS { get ; set ; }
public string 工程师{ get ; set ; }
public string Foreman { get ; set ; }
public string 字段{ get ; set ; }
public string 好{ get ; set ; }
public string SITE { get ; set ; }
public string 操作{ get ; set ; }
public string Equipment_Name { get ; set ; }
public long 距离{ get ; set ; }
public string Last_24_hrs_Operations { get ; set ; }
public string Next_24_hrs_Operations { get ; set ; }
public string 问题{ get ; set ; }
public DateTime EquipmentMovement_in { get ; set ; }
public DateTime EquipmentMovement_out { get ; set ; }
public long No_OfHours { get ; set ; }
public string 船员{ get ; set ; }
public long TotalNoEmployees { get ; set ; }

}





列表< ImportFromExcel> regCandidates = new List< ImportFromExcel> {}; 
if(fuExcelfile.HasFile)
{
string csvFilePath =;

if(fuExcelfile.HasFile)
{
csvFilePath =+ @+ SaveDocument(fuExcelfile); // Excel文件路径
hdnfilepath.Value = csvFilePath;
}
else
{
csvFilePath = hdnfilepath.Value.ToString();
}


List< string> Fulltext = File.ReadAllLines(csvFilePath).Where(line =>!string.IsNullOrWhiteSpace(line))。ToList(); //获取一些垃圾数据

var strText = Fulltext;
}





我尝试以CSV格式阅读,但我收不到。



任何人都可以帮助我。



感谢adv。

解决方案

< blockquote>我建​​议使用 EPPlus [ ^ ]。



 var package = new ExcelPackage(new FileInfo(@) fullFIleName.xlsx)); 
ExcelWorksheet sheet = package.Workbook.Worksheets [1];

foreach(表格中的var tb)
{
//从表中读取数据
//忽略标题
}





详情请见:

c# - EPPlus - 读取Excel表 [ ^ ]

c# - 如何使用EPPlus将excel行解析回类型 [ ^ ]


Dear All,

I have an ASP.NET web application in C#.

I want to import data from excel to Repeater using linq.

The excel file contains company logo(Image), empty rows and multiple table with column names and row names.

Example:

-----------------------------------------------------------------------------
 ClientInfo         |         FieldInfo              |   RigInfo            |
-----------------------------------------------------------------------------
Name | abc          |         Field | f1             |   Rig | r1           |
-----------------------------------------------------------------------------
Div  | d1           |         Field | f2             |   Rig | r3           |
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
 ClientInfo         |         FieldInfo              |   RigInfo            |
-----------------------------------------------------------------------------
Name | abs          |         Field | f3             |   Rig | r2           |
-----------------------------------------------------------------------------
Div  | d3           |         Field | f4             |   Rig | r4           |
-----------------------------------------------------------------------------



What I have tried:

public class ImportFromExcel
   {
       public string Client { get; set; }
       public string Division { get; set; }
       public string Superintendent { get; set; }
       public string GS { get; set; }
       public string Engineer { get; set; }
       public string Foreman { get; set; }
       public string Field { get; set; }
       public string Well { get; set; }
       public string SITE { get; set; }
       public string Operations { get; set; }
       public string Equipment_Name { get; set; }
       public long Distance { get; set; }
       public string Last_24_hrs_Operations { get; set; }
       public string Next_24_hrs_Operations { get; set; }
       public string Issues { get; set; }
       public DateTime EquipmentMovement_in { get; set; }
       public DateTime EquipmentMovement_out { get; set; }
       public long No_OfHours { get; set; }
       public string Crew { get; set; }
       public long TotalNoEmployees { get; set; }

   }



List<ImportFromExcel> regCandidates = new List<ImportFromExcel> { };
            if (fuExcelfile.HasFile)
            {
                string csvFilePath = "";

                if (fuExcelfile.HasFile)
                {
                    csvFilePath = "" + @"" + SaveDocument(fuExcelfile); //Excel file path
                    hdnfilepath.Value = csvFilePath;
                }
                else
                {
                    csvFilePath = hdnfilepath.Value.ToString();
                }


                List<string> Fulltext=  File.ReadAllLines(csvFilePath).Where(line => !string.IsNullOrWhiteSpace(line)).ToList(); //getting some rubbish data

                var strText = Fulltext;
            }



I tried reading as CSV but i'm getting fail to get.

Can any one please help me.

Thanks in adv.

解决方案

I'd suggest to use EPPlus[^].

var package = new ExcelPackage(new FileInfo(@"fullFIleName.xlsx"));
ExcelWorksheet sheet = package.Workbook.Worksheets[1];

foreach(var tb in sheet.Tables)
{
    //read data from table
    //ignore headers
}



For further details, please see:
c# - EPPlus - Read Excel Table[^]
c# - How to parse excel rows back to types using EPPlus[^]


这篇关于如何在ASP.NET C#中将数据从excel导入到类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆