gpt4 book ai didi

c# - 如何使用C#读取Excel文件的数据?

转载 作者:IT王子 更新时间:2023-10-29 03:44:23 24 4
gpt4 key购买 nike

如何使用 C# 读取 Excel 文件?我打开一个 Excel 文件进行阅读并将其复制到剪贴板以搜索电子邮件格式,但我不知道该怎么做。

FileInfo finfo;
Excel.ApplicationClass ExcelObj = new Excel.ApplicationClass();
ExcelObj.Visible = false;

Excel.Workbook theWorkbook;
Excel.Worksheet worksheet;

if (listView1.Items.Count > 0)
{
foreach (ListViewItem s in listView1.Items)
{
finfo = new FileInfo(s.Text);
if (finfo.Extension == ".xls" || finfo.Extension == ".xlsx" || finfo.Extension == ".xlt" || finfo.Extension == ".xlsm" || finfo.Extension == ".csv")
{
theWorkbook = ExcelObj.Workbooks.Open(s.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);

for (int count = 1; count <= theWorkbook.Sheets.Count; count++)
{
worksheet = (Excel.Worksheet)theWorkbook.Worksheets.get_Item(count);
worksheet.Activate();
worksheet.Visible = false;
worksheet.UsedRange.Cells.Select();
}
}
}
}

最佳答案

好的,

关于 Excel VSTO 编程,一个更难掌握的概念是您不像数组那样引用单元格,Worksheet[0][0] 不会给您单元格 A1,它会出错。即使您在 Excel 打开时输入 A1,您实际上也是在 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() block ,因为 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 中的函数,我也没有那个代码。

编辑:正如所 promise 的,这里是您可能需要的 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 自动化的怪人之一。所有这些代码都源 self 为工作编写的工具,该工具要求我每周一处理来自实验室的 1000 多个电子表格。

关于c# - 如何使用C#读取Excel文件的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/657131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com