Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取Excel文件(仅标题)数据_C#_Excel - Fatal编程技术网

C# 如何读取Excel文件(仅标题)数据

C# 如何读取Excel文件(仅标题)数据,c#,excel,C#,Excel,我想从excel的列标题中读取数据。 每个excel文件都有相同的标题名,但标题名在不同的行中。 请帮我用c代码。 我的代码如下 var textfile = Request.Files[0]; string profileimage = Path.GetExtension(textfile.FileName); string ProfileName = path.GetFileNameWithoutExtension(textfile.F

我想从excel的列标题中读取数据。 每个excel文件都有相同的标题名,但标题名在不同的行中。 请帮我用c代码。 我的代码如下

 var textfile = Request.Files[0];
        string profileimage = Path.GetExtension(textfile.FileName);           
        string ProfileName = path.GetFileNameWithoutExtension(textfile.FileName);
        IExcelDataReader excelReader;     
     excelReader=ExcelReaderFactory.CreateBinaryReader(textfile.InputStream);
         DataSet result = excelReader.AsDataSet();
         DataTable dt = result.Tables[0];
         int Tcount = dt.Rows.Count;
         for (int i = 2; i < dt.Rows.Count; i++)
            {}
var textfile=Request.Files[0];
string profileimage=Path.GetExtension(textfile.FileName);
string ProfileName=path.GetFileNameWithoutExtension(textfile.FileName);
IExcelDataReader-excelReader;
excelReader=ExcelReaderFactory.CreateBaryReader(textfile.InputStream);
数据集结果=excelReader.AsDataSet();
DataTable dt=结果表[0];
int Tcount=dt.Rows.Count;
对于(int i=2;i

…一种方法是使用互操作。这是office自带的,所以你不需要任何第三方的东西

using Excel=Microsoft.Office.Interop.Excel; 
然后,您可以通过行/列索引或字符串比较对行进行排序以查找标题。 最后,您需要使用此命令释放COM对象(此处的顺序很重要),否则Excel.exe将继续运行:

Marshal.ReleaseComObject(MyCell);
Marshal.ReleaseComObject(MySheet);
Marshal.ReleaseComObject(MyBook);
Marshal.ReleaseComObject(MyApp);

只需确保先阅读文档,因为在使用interop时会遇到一些问题,比如释放对象。

一种方法是使用interop。这是office自带的,所以你不需要任何第三方的东西

using Excel=Microsoft.Office.Interop.Excel; 
然后,您可以通过行/列索引或字符串比较对行进行排序以查找标题。 最后,您需要使用此命令释放COM对象(此处的顺序很重要),否则Excel.exe将继续运行:

Marshal.ReleaseComObject(MyCell);
Marshal.ReleaseComObject(MySheet);
Marshal.ReleaseComObject(MyBook);
Marshal.ReleaseComObject(MyApp);

请确保先阅读文档,因为在使用interop时会遇到一些问题,比如释放对象。

尝试使用OpenXMLSDK。在这里,您可以找到有关以下方面的更多信息:


  • 尝试使用OpenXMLSDK。在这里,您可以找到有关以下方面的更多信息:


  • 到目前为止你试过什么?请显示您迄今为止已实现的代码。编辑完成。请检查我的问题到目前为止您尝试了什么?请显示您迄今为止实现的代码。编辑完成。请检查我的问题