从Excel工作表中获取数据 [英] Fetch data from excel sheet

查看:60
本文介绍了从Excel工作表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私有 无效 ReadExcelFile( int  ProjectID, int  NumberOfCrawlInstances)
        {
            字符串 fileType = Path.GetExtension(file);
            字符串 SourceConstr;
            System.Data.DataTable dtExcel =  System.Data.DataTable();
            dtExcel.TableName = " ;
            如果(fileType == " )
            {
                SourceConstr =  @"  +文件+ " ;
            }
            其他
            {
                SourceConstr =  @"  + file + " ;
            }
            OleDbConnection con =  OleDbConnection(SourceConstr);
           < big>  字符串查询= "  ;  big   OleDbDataAdapter(query,con);
            data.Fill(dtExcel);

             int  RowsCount = dtExcel.Rows.Count;
            如果(RowsCount >   0 )
            {
                数据表dt = ConvertInput(dtExcel,ProjectID);
                AttributesBulkCopy(dt,RowsCount,InstanceId);
                SplitInstances(InstanceId,NumberOfCrawlInstances);
                ManageInstance(InstanceId,RowsCount, 1 );
            }
        } 




这是控制台应用程序...这里选择查询"是从工作表1.中获取数据.我想甚至获取工作表2或工作表3中的数据.我如何更改该代码..pls帮助我

解决方案方案

" ; big > OleDbDataAdapter数据= OleDbDataAdapter(query,con); data.Fill(dtExcel); int RowsCount = dtExcel.Rows.Count; 如果(RowsCount > 0 ) { 数据表dt = ConvertInput(dtExcel,ProjectID); AttributesBulkCopy(dt,RowsCount,InstanceId); SplitInstances(InstanceId,NumberOfCrawlInstances); ManageInstance(InstanceId,RowsCount, 1 ); } }




这是控制台应用程序......................................................... blockquote>字符串查询=从[ Sheet2


选择*";


private void ReadExcelFile(int InstanceId, int ProjectID, string file, int NumberOfCrawlInstances)
        {
            string fileType = Path.GetExtension(file);
            string SourceConstr;
            System.Data.DataTable dtExcel = new System.Data.DataTable();
            dtExcel.TableName = "MyExcelData";
            if (fileType == ".xls")
            {
                SourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + file + "';;Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
            }
            else
            {
                SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + file + "';;Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1'";
            }
            OleDbConnection con = new OleDbConnection(SourceConstr);
           <big> string query = "Select * from [Sheet1$]";</big>
            OleDbDataAdapter data = new OleDbDataAdapter(query, con);
            data.Fill(dtExcel);

            int RowsCount = dtExcel.Rows.Count;
            if (RowsCount > 0)
            {
                DataTable dt = ConvertInput(dtExcel, ProjectID);
                AttributesBulkCopy(dt, RowsCount, InstanceId);
                SplitInstances(InstanceId, NumberOfCrawlInstances);
                ManageInstance(InstanceId, RowsCount, 1);
            }
        }




This is console application...Here the Select querey is fetch data from sheet 1..i want to fetch even the data in sheet2 or sheet3 how can i change that code..pls help me

解决方案

";</big> OleDbDataAdapter data = new OleDbDataAdapter(query, con); data.Fill(dtExcel); int RowsCount = dtExcel.Rows.Count; if (RowsCount > 0) { DataTable dt = ConvertInput(dtExcel, ProjectID); AttributesBulkCopy(dt, RowsCount, InstanceId); SplitInstances(InstanceId, NumberOfCrawlInstances); ManageInstance(InstanceId, RowsCount, 1); } }




This is console application...Here the Select querey is fetch data from sheet 1..i want to fetch even the data in sheet2 or sheet3 how can i change that code..pls help me


string query = "Select * from [Sheet2


";


这篇关于从Excel工作表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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