Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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
什么';使用vb.net在大型excel文件上执行SQL查询的最佳方法是什么?_.net_Vb.net_Excel_Vsto_Openxml - Fatal编程技术网

什么';使用vb.net在大型excel文件上执行SQL查询的最佳方法是什么?

什么';使用vb.net在大型excel文件上执行SQL查询的最佳方法是什么?,.net,vb.net,excel,vsto,openxml,.net,Vb.net,Excel,Vsto,Openxml,设置环境: SELECT * FROM wells WHERE padgroup in (SELECT padgroup FROM wells WHERE name LIKE 'TOMCHUCK 21-30' OR name LIKE 'FEDERAL 41-25PH') 'Create Recordset Object rsCon = CreateObject("ADODB.Connec

设置环境:

    SELECT * 
    FROM wells 
    WHERE padgroup in 

    (SELECT padgroup 
     FROM wells 
     WHERE name LIKE 'TOMCHUCK 21-30'
             OR name LIKE 'FEDERAL 41-25PH')
    'Create Recordset Object
    rsCon = CreateObject("ADODB.Connection")
    rsData = CreateObject("ADODB.Recordset")

    rsCon.Open(szConnect)
    rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)

    'Check to make sure data is received, then copy the data
    If Not rsData.EOF Then

        TargetRange.Cells(1, 1).CopyFromRecordset(rsData)

    Else

        MsgBox("No records returned from : " & SourceFile, vbCritical)

    End If

    'Clean up the Recordset object
    rsData.Close()
    rsData = Nothing
    rsCon.Close()
    rsCon = Nothing
我正在使用vb.net和.net framework 4开发一个Excel 2010应用程序级外接程序


我的目标:

    SELECT * 
    FROM wells 
    WHERE padgroup in 

    (SELECT padgroup 
     FROM wells 
     WHERE name LIKE 'TOMCHUCK 21-30'
             OR name LIKE 'FEDERAL 41-25PH')
    'Create Recordset Object
    rsCon = CreateObject("ADODB.Connection")
    rsData = CreateObject("ADODB.Recordset")

    rsCon.Open(szConnect)
    rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)

    'Check to make sure data is received, then copy the data
    If Not rsData.EOF Then

        TargetRange.Cells(1, 1).CopyFromRecordset(rsData)

    Else

        MsgBox("No records returned from : " & SourceFile, vbCritical)

    End If

    'Clean up the Recordset object
    rsData.Close()
    rsData = Nothing
    rsCon.Close()
    rsCon = Nothing
  • 让用户键入多个要搜索的名称
  • 使用名称列表在大型电子表格(30000多行)上执行SQL查询
  • 返回记录集并粘贴到新工作表中
  • 性能是我的首要任务。我想知道利用.NET framework实现这一点的最快方法

    在我的代码中使用ADO连接对象是可行的,但过程太长(5-8秒)


    这是我对名为wells的表使用的SQL查询:

        SELECT * 
        FROM wells 
        WHERE padgroup in 
    
        (SELECT padgroup 
         FROM wells 
         WHERE name LIKE 'TOMCHUCK 21-30'
                 OR name LIKE 'FEDERAL 41-25PH')
    
        'Create Recordset Object
        rsCon = CreateObject("ADODB.Connection")
        rsData = CreateObject("ADODB.Recordset")
    
        rsCon.Open(szConnect)
        rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)
    
        'Check to make sure data is received, then copy the data
        If Not rsData.EOF Then
    
            TargetRange.Cells(1, 1).CopyFromRecordset(rsData)
    
        Else
    
            MsgBox("No records returned from : " & SourceFile, vbCritical)
    
        End If
    
        'Clean up the Recordset object
        rsData.Close()
        rsData = Nothing
        rsCon.Close()
        rsCon = Nothing
    

    这是表格的一部分内容:

        SELECT * 
        FROM wells 
        WHERE padgroup in 
    
        (SELECT padgroup 
         FROM wells 
         WHERE name LIKE 'TOMCHUCK 21-30'
                 OR name LIKE 'FEDERAL 41-25PH')
    
        'Create Recordset Object
        rsCon = CreateObject("ADODB.Connection")
        rsData = CreateObject("ADODB.Recordset")
    
        rsCon.Open(szConnect)
        rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)
    
        'Check to make sure data is received, then copy the data
        If Not rsData.EOF Then
    
            TargetRange.Cells(1, 1).CopyFromRecordset(rsData)
    
        Else
    
            MsgBox("No records returned from : " & SourceFile, vbCritical)
    
        End If
    
        'Clean up the Recordset object
        rsData.Close()
        rsData = Nothing
        rsCon.Close()
        rsCon = Nothing
    


    我正在使用这段代码创建一个ADO连接对象来检索我的结果:

        SELECT * 
        FROM wells 
        WHERE padgroup in 
    
        (SELECT padgroup 
         FROM wells 
         WHERE name LIKE 'TOMCHUCK 21-30'
                 OR name LIKE 'FEDERAL 41-25PH')
    
        'Create Recordset Object
        rsCon = CreateObject("ADODB.Connection")
        rsData = CreateObject("ADODB.Recordset")
    
        rsCon.Open(szConnect)
        rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)
    
        'Check to make sure data is received, then copy the data
        If Not rsData.EOF Then
    
            TargetRange.Cells(1, 1).CopyFromRecordset(rsData)
    
        Else
    
            MsgBox("No records returned from : " & SourceFile, vbCritical)
    
        End If
    
        'Clean up the Recordset object
        rsData.Close()
        rsData = Nothing
        rsCon.Close()
        rsCon = Nothing
    

    据我所知,Excel电子表格是以开放式XML格式存储的,.NET framework包含对解析XML的本机支持

    研究之后,我发现了几个不同的选择:

        SELECT * 
        FROM wells 
        WHERE padgroup in 
    
        (SELECT padgroup 
         FROM wells 
         WHERE name LIKE 'TOMCHUCK 21-30'
                 OR name LIKE 'FEDERAL 41-25PH')
    
        'Create Recordset Object
        rsCon = CreateObject("ADODB.Connection")
        rsData = CreateObject("ADODB.Recordset")
    
        rsCon.Open(szConnect)
        rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)
    
        'Check to make sure data is received, then copy the data
        If Not rsData.EOF Then
    
            TargetRange.Cells(1, 1).CopyFromRecordset(rsData)
    
        Else
    
            MsgBox("No records returned from : " & SourceFile, vbCritical)
    
        End If
    
        'Clean up the Recordset object
        rsData.Close()
        rsData = Nothing
        rsCon.Close()
        rsCon = Nothing
    

    有人能提供一个指针,指出什么是最好的使用方法吗?我真的很感激


    附加说明:

        SELECT * 
        FROM wells 
        WHERE padgroup in 
    
        (SELECT padgroup 
         FROM wells 
         WHERE name LIKE 'TOMCHUCK 21-30'
                 OR name LIKE 'FEDERAL 41-25PH')
    
        'Create Recordset Object
        rsCon = CreateObject("ADODB.Connection")
        rsData = CreateObject("ADODB.Recordset")
    
        rsCon.Open(szConnect)
        rsData.Open(mySQLQueryToExecute, rsCon, 0, 1, 1)
    
        'Check to make sure data is received, then copy the data
        If Not rsData.EOF Then
    
            TargetRange.Cells(1, 1).CopyFromRecordset(rsData)
    
        Else
    
            MsgBox("No records returned from : " & SourceFile, vbCritical)
    
        End If
    
        'Clean up the Recordset object
        rsData.Close()
        rsData = Nothing
        rsCon.Close()
        rsCon = Nothing
    
    • 所有查询都需要能够在不连接到数据库的情况下执行 在线数据库
    • 我只需要访问电子表格一次就可以从行中提取原始数据

    现在我只是将电子表格嵌入到项目资源中

    然后,在运行时创建文件,运行查询,将结果存储在内存中,然后删除文件

       'Create temp file path in the commonapplicationdata folder
        Dim excelsheetpath As StringBuilder
    
        excelsheetpath = New StringBuilder(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
    
        excelsheetpath.Append("\MasterList.xlsm")
    
        'Save resources into temp location in HD
        System.IO.File.WriteAllBytes(excelsheetpath.ToString, My.Resources.MasterList)
    
        'Now call the function to use ADO to get records from the MasterList.xlsm file just created
        GetData(excelsheetpath.ToString, "Sheet1", "A1:S40000", True, False)
    
        'Store the results in-memory and display by adding to a datagridview control (in a custom task pane)
    
        'Delete the spreadsheet
        System.IO.File.Delete(excelsheetpath.ToString())
    

    Excel 2010文件不完全是XML。获取一个XLSX(或XMSM)文件,并将其重命名为.zip扩展名。然后将其解压缩到新文件夹。子文件夹中的文件将是XML文件,是的,但实际的XLSX文件是包含包含XML文件的文件夹集合的zip文件


    我认为,您最好使用ACE驱动程序(JET不再受支持)并通过ODBC访问它。如果速度不够快,您可以在特定时间提取数据,并将其上传到数据库,以便运行查询;查询速度应该更快,但可能会过时。

    您的方式不对;)不要将SQL与Excel一起使用。如果需要速度,请利用VSTO和本机Excel API。您可以跳过ADODB/OLEDB层的开销,直接转到Excel对象模型,使用Excel中的快速自动筛选、
    SpecialCells
    方法仅将可见单元格复制到多区域范围,以及
    Value
    方法将范围快速复制到阵列

    下面是一个VSTO 2010定制工作簿示例,它可以快速搜索包含“aba”、“cat”或“zon”的文档

    使用系统;
    使用System.Collections.Generic;
    使用系统数据;
    使用System.Linq;
    使用系统文本;
    使用System.Windows.Forms;
    使用System.Xml.Linq;
    使用Microsoft.Office.Tools.Excel;
    使用Microsoft.VisualStudio.Tools.Applications.Runtime;
    使用Excel=Microsoft.Office.Interop.Excel;
    使用Office=Microsoft.Office.Core;
    命名空间Excel工作簿1
    {
    公共部分类工作簿
    {
    私有无效此工作簿\u启动(对象发送方,System.EventArgs e)
    {
    const int Sheet1=1;//如果需要,可以使用Linq按名称查找工作表
    const int ColumnB=2;
    列表结果=查询(表1,B列,“aba”、“cat”、“zon”);
    foreach(在结果中列出记录)
    {
    System.Diagnostics.Debug.Print(“{0,-10}{1,30}{2}”,记录[0],记录[1],记录[2]);
    }
    }
    私有无效此工作簿\u关闭(对象发送方,System.EventArgs e)
    {
    }
    /// 
    ///从工作表中删除任何现有的Excel自动筛选
    /// 
    专用void ClearFilter(Microsoft.Office.Interop.Excel.\u工作表)
    {
    如果(sheet.AutoFilter!=null)
    {
    worksheet.Cells.AutoFilter();
    }
    }
    /// 
    ///将Excel自动筛选应用于工作表以搜索子字符串谓词数组
    /// 
    私有void ApplyFilter(Microsoft.Office.Interop.Excel.\u工作表工作表,int列,参数字符串[]谓词)
    {
    string[]条件=新字符串[predicates.Length];
    int i=0;
    ClearFilter(工作表);
    foreach(谓词中的字符串值)
    {
    条件[i++]=String.Concat(“=*”,值“*”;
    }
    工作表.Cells.AutoFilter(列、条件、Excel.XlAutoFilterOperator.xlOr);
    }
    /// 
    ///返回在Sheet1的B列中搜索子字符串数组时命中的行列表
    /// 
    私有列表查询(int sheetIndex、int columnIndex、params string[]words)
    {
    Microsoft.Office.Interop.Excel.\u工作表;
    范围;
    列表记录=新列表();
    列表记录;
    对象[,]细胞;
    目标价值;
    int行、列、行、列;
    布尔击中;
    尝试
    {
    工作表=(Microsoft.Office.Interop.Excel.工作表)Globals.ThisWorkbook.Sheets[sheetIndex];
    如果(空==工作表)
    {
    返回null;
    }
    //应用自动过滤器
    ApplyFilter(工作表、列索引、单词);
    //得到
    范围=工作表。范围[“$A:$C”]。特殊单元格(Excel.XlCellType.xlCellTypeVisible);
    foreach(范围A中的Excel.Range子范围