好的,
关于 Excel VSTO 编程的一个更难掌握的概念是,您不能像数组一样引用单元格,Worksheet[0][0] 不会给您单元格 A1,它会在您身上出错。即使您在 Excel 打开时在 A1 中键入,您实际上是在将数据输入到 Range A1 中。因此,您将单元格称为命名范围。这是一个例子:
Excel.Worksheet sheet = workbook.Sheets["Sheet1"] as Excel.Worksheet;
Excel.Range range = sheet.get_Range("A1", Missing.Value)
您现在可以直接输入:
range.Text // this will give you the text the user sees
range.Value2 // this will give you the actual value stored by Excel (without rounding)
如果你想做这样的事情:
Excel.Range range = sheet.get_Range("A1:A5", Missing.Value)
if (range1 != null)
foreach (Excel.Range r in range1)
{
string user = r.Text
string value = r.Value2
}
可能有更好的方法,但这对我有用。
您需要使用Value2 而不是Value 的原因是因为Value 属性是参数化的,而C# 还不支持它们。
至于清理代码,我会在明天上班时发布,我没有代码,但它是非常样板的。您只需按照创建对象的相反顺序关闭和释放对象。不能使用 Using() 块,因为 Excel.Application 或 Excel.Workbook 没有实现 IDisposable,如果不清理,您将在内存中留下一个悬挂的 Excel 对象。
注意:
- 如果您不设置
Visibility 属性 Excel 不会显示,这可能会让您的用户感到不安,但如果您只想提取数据,那可能就足够了
- 你可以使用 OleDb,它也可以。
我希望这可以帮助您入门,如果您需要进一步说明,请告诉我。我会发一个完整的
这是一个完整的示例:
using System;
using System.IO;
using System.Reflection;
using NUnit.Framework;
using ExcelTools = Ms.Office;
using Excel = Microsoft.Office.Interop.Excel;
namespace Tests
{
[TestFixture]
public class ExcelSingle
{
[Test]
public void ProcessWorkbook()
{
string file = @"C:\Users\Chris\Desktop\TestSheet.xls";
Console.WriteLine(file);
Excel.Application excel = null;
Excel.Workbook wkb = null;
try
{
excel = new Excel.Application();
wkb = ExcelTools.OfficeUtil.OpenBook(excel, file);
Excel.Worksheet sheet = wkb.Sheets["Data"] as Excel.Worksheet;
Excel.Range range = null;
if (sheet != null)
range = sheet.get_Range("A1", Missing.Value);
string A1 = String.Empty;
if( range != null )
A1 = range.Text.ToString();
Console.WriteLine("A1 value: {0}", A1);
}
catch(Exception ex)
{
//if you need to handle stuff
Console.WriteLine(ex.Message);
}
finally
{
if (wkb != null)
ExcelTools.OfficeUtil.ReleaseRCM(wkb);
if (excel != null)
ExcelTools.OfficeUtil.ReleaseRCM(excel);
}
}
}
}
明天我会从 ExcelTools 发布函数,我也没有那个代码。
编辑:
正如所承诺的,这里是您可能需要的 ExcelTools 中的函数。
public static Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly, bool editable,
bool updateLinks) {
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
public static void ReleaseRCM(object o) {
try {
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
} catch {
} finally {
o = null;
}
}
坦率地说,如果您使用 VB.NET,这些东西会容易得多。它在 C# 中,因为我没有编写它。 VB.NET 可以很好地处理选项参数,而 C# 没有,因此 Type.Missing。一旦你连续输入 Type.Missing 两次,你就会尖叫着跑出房间!
至于你的问题,你可以尝试以下:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.find(VS.80).aspx
我会在会议回来后发布一个示例...干杯
编辑:这是一个例子
range = sheet.Cells.Find("Value to Find",
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Excel.XlSearchDirection.xlNext,
Type.Missing,
Type.Missing, Type.Missing);
range.Text; //give you the value found
这是受site启发的另一个示例:
range = sheet.Cells.Find("Value to find", Type.Missing, Type.Missing,Excel.XlLookAt.xlWhole,Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlNext,false, false, Type.Missing);
有助于理解参数。
附:我是那些喜欢学习 COM 自动化的怪人之一。所有这些代码都来自我为工作而编写的工具,该工具需要我每周一处理来自实验室的 1000 多个电子表格。