- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
设置环境:
我正在使用 vb.net 和 .NET Framework 4 开发 Excel 2010 应用程序级插件。
我的目标:
性能是我的首要任务。我想知道利用 .NET 框架实现此目的的最快方法。
在我的代码中使用 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')
以下是表格的一部分:
我现在正在使用此代码创建 ADO 连接对象来检索我的结果:
'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 电子表格以 Open XML 格式存储,并且 .NET 框架包含对解析 XML 的 native 支持。
经过研究,我发现了一些不同的选择:
有人可以提供有关最佳使用方法的指示吗?我真的很感激。
附加说明:
现在我只是将电子表格作为项目资源嵌入。
然后,在运行时我创建文件,运行查询,将结果存储在内存中,然后删除文件。
'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())
最佳答案
您使用 VSTO 的方式是错误的;)不要在 Excel 中使用 SQL。如果您需要速度,请利用 VSTO 和 native Excel API。您可以跳过 ADODB/OLEDB 层的开销,直接进入 Excel 对象模型,使用 Excel 中超快的自动筛选功能,即 SpecialCells
方法,仅将可见单元格放入多区域范围中,以及用于快速将范围复制到数组的 Value
方法。
这是一个示例 VSTO 2010 自定义工作簿,可快速搜索 a list of 58k words for words包含“aba”、“cat”或“zon”。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
namespace ExcelWorkbook1
{
public partial class ThisWorkbook
{
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
const int Sheet1 = 1; // you can use Linq to find a sheet by name if needed
const int ColumnB = 2;
List<List<object>> results = Query(Sheet1, ColumnB, "aba", "cat", "zon");
foreach (List<object> record in results)
{
System.Diagnostics.Debug.Print("{0,-10} {1,30} {2}", record[0], record[1], record[2]);
}
}
private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
{
}
/// <summary>
/// Removes any existing Excel autofilters from the worksheet
/// </summary>
private void ClearFilter(Microsoft.Office.Interop.Excel._Worksheet worksheet)
{
if (worksheet.AutoFilter != null)
{
worksheet.Cells.AutoFilter();
}
}
/// <summary>
/// Applies an Excel Autofilter to the worksheet for search for an array of substring predicates
/// </summary>
private void ApplyFilter(Microsoft.Office.Interop.Excel._Worksheet worksheet, int column, params string[] predicates)
{
string[] criteria = new string[predicates.Length];
int i = 0;
ClearFilter(worksheet);
foreach (string value in predicates)
{
criteria[i++] = String.Concat("=*", value, "*");
}
worksheet.Cells.AutoFilter(column, criteria, Excel.XlAutoFilterOperator.xlOr);
}
/// <summary>
/// Returns a list of rows that are hits on a search for an array of substrings in Column B of Sheet1
/// </summary>
private List<List<object>> Query(int sheetIndex, int columnIndex, params string[] words)
{
Microsoft.Office.Interop.Excel._Worksheet worksheet;
Excel.Range range;
List<List<object>> records = new List<List<object>>();
List<object> record;
object[,] cells;
object value;
int row, column, rows, columns;
bool hit;
try
{
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)Globals.ThisWorkbook.Sheets[sheetIndex];
if (null == worksheet)
{
return null;
}
// apply the autofilter
ApplyFilter(worksheet, columnIndex, words);
// get the
range = worksheet.Range["$A:$C"].SpecialCells(Excel.XlCellType.xlCellTypeVisible);
foreach (Excel.Range subrange in range.Areas)
{
// copy the cells to a multidimensional array for perfomance
cells = subrange.Value;
// transform the multidimensional array to a List
for (row = cells.GetLowerBound(0), rows = cells.GetUpperBound(0); row <= rows; row++)
{
record = new List<object>();
hit = false;
for (column = cells.GetLowerBound(1), columns = cells.GetUpperBound(1); column <= columns; column++)
{
value = cells[row, column];
hit = hit || (null != value);
if (hit)
{
record.Add(cells[row, column]);
}
}
if (hit)
{
records.Add(record);
}
}
}
}
catch { }
finally
{
// use GC.Collect over Marshal.ReleaseComObject() to release all RCWs per http://stackoverflow.com/a/17131389/1995977 and more
cells = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
return records;
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisWorkbook_Startup);
this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
}
#endregion
}
}
关于.net - 使用 vb.net 对大型 Excel 文件执行 SQL 查询的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22409637/
我有一个“有趣”的问题,即以两种不同的方式运行 wine 会导致: $> wine --version /Applications/Wine.app/Contents/Resources/bin/wi
我制作了这个网络抓取工具来获取网页中的表格。我使用 puppeteer (不知道 crontab 有问题)、Python 进行清理并处理数据库的输出 但令我惊讶的是,当我执行它时 */50 * * *
JavaScript 是否被调用或执行取决于什么?准确地说,我有两个函数,它们都以相同的方式调用: [self.mapView stringByEvaluatingJavaScriptFromStri
我目前正在使用 python 做一个机器学习项目(这里是初学者,从头开始学习一切)。 只是想知道 statsmodels 的 OLS 和 scikit 的 PooledOlS 使用我拥有的相同面板数据
在使用集成对象模型 (IOM) 后,我可以执行 SAS 代码并将 SAS 数据集读入 .Net/C# 数据集 here . 只是好奇,使用 .Net 作为 SAS 服务器的客户端与使用 Enterpr
有一些直接的 jQuery 在单击时隐藏打开的 div 未显示,但仍将高度添加到导航中以使其看起来好像要掉下来了。 这个脚本工作正常: $(document).ready(funct
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 这里是 Java 新手,我正在使用 NetBeans 尝试一些简单的代
如果我将它切换到 Python 2.x,它执行 10。这是为什么? 训练逻辑回归模型 import keras.backend as
我有两个脚本,它们包含在 HTML 正文中。在第一个脚本中,我初始化一个 JS 对象,该对象在第二个脚本标记中引用。 ... obj.a = 1000; obj.
每当我运行该方法时,我都会收到一个带有数字的错误 以下是我的代码。 public String getAccount() { String s = "Listing the accounts";
我已经用 do~while(true) 创建了我的菜单;但是每次用户输入一个数字时,它不会运行程序,而是再次显示菜单!你怎么看? //我的主要方法 public static void main(St
执行命令后,如何让IPython通知我?我可以使用铃声/警报还是通过弹出窗口获取它?我正在OS X 10.8.5的iTerm上运行Anaconda。 最佳答案 使用最新版本的iTerm,您可以在she
您好,我刚刚使用菜单栏为 Swing 编写了代码。但是问题出现在运行中。我输入: javac Menu.java java Menu 它没有给出任何错误,但 GUI 没有显示。这是我的源代码以供引用:
我觉得这里缺少明显的东西,但是我看不到它写在任何地方。 我使用Authenticode证书对可执行文件进行签名,但是当我开始学习有关它的更多信息时,我对原样的值(value)提出了质疑。 签名的exe
我正在设计一个应用程序,它使用 DataTables 中的预定义库来创建数据表。我想对数据表执行删除操作,为此应在按钮单击事件上执行 java 脚本。 $(document).ready(functi
我是 Haskell 新手,如果有人愿意帮助我,我会很高兴!我试图让这个程序与 do while 循环一起工作。 第二个 getLine 命令的结果被放入变量 goGlenn 中,如果 goGlenn
我有一个用 swing 实现迷你游戏的程序,在主类中我有一个循环,用于监听游戏 map 中的 boolean 值。使用 while 实现的循环不会执行一条指令,如果它是唯一的一条指令,我不知道为什么。
我正在尝试开发一个连接到 Oracle 数据库并执行函数的 Java 应用程序。如果我在 Eclipse 中运行该应用程序,它可以工作,但是当我尝试在 Windows 命令提示符中运行 .jar 时,
我正在阅读有关 Java 中的 Future 和 javascript 中的 Promises 的内容。下面是我作为示例编写的代码。我的问题是分配给 future 的任务什么时候开始执行? 当如下行创
我有一个常见的情况,您有两个变量(xSpeed 和 ySpeed),当它们低于 minSpeed 时,我想将它们独立设置为零,并在它们都为零时退出。 最有效的方法是什么?目前我有两种方法(方法2更干净
我是一名优秀的程序员,十分优秀!