简体   繁体   English

如何在Visual Studio Office Excel工作簿中访问工作表中的数据?

[英]How to access data in sheet in my Visual Studio Office Excel workbook?

I need to create a "Document level customization" with C# code (not and Excel Add-in!) 我需要使用C#代码创建一个“文档级自定义”(不是Excel加载项!)

I created a Visual Studio Office Excel 2010 Workbook project type. 我创建了Visual Studio Office Excel 2010工作簿项目类型。 This creates a workbook with 3 sheets in my project. 这将在我的项目中创建一个包含3张纸的工作簿。 I added some "configuration" information to one of those sheets. 我向其中的一张纸添加了一些“配置”信息。

I need to access this configuration information programmatically ( Sheet1 contains a button - pressing on that button should 我需要以编程方式访问此配置信息( Sheet1包含一个button -按下该按钮应

  1. load configuration data 加载配置数据
  2. open a WinForm 打开一个WinForm
  3. present configuration data on that form, 在该表格上显示配置数据,

but somehow I can't find how to do that... 但是我不知道该怎么做...

If I try to initialize Sheet1 class, compiler expects two parameters - Microsoft.Office.Tools.Excel.Factory and IServiceProvider , but I am calling this from a button that is placed on Sheet2 - so it's after Excel Workbook is already opened... shouldn't Sheet1 be initialized automatically? 如果我尝试初始化Sheet1类,则编译器需要两个参数Microsoft.Office.Tools.Excel.FactoryIServiceProvider ,但是我是通过Sheet2上的按钮调用此参数的-因此它是 Excel Workbook已打开之后... Sheet1是否不应该自动初始化?

So, how can I access Sheet1 from my VSTO project's c# code? 那么,如何从VSTO项目的c#代码访问Sheet1

EDIT 编辑

Please see project sample screencast here 请在此处查看项目样本截屏

I have a button on Sheet2, that should 我在Sheet2上有一个按钮,应该

  1. load some data from Sheet1 从Sheet1加载一些数据
  2. initialize WinForm 初始化WinForm
  3. add it as a DataSource for a ComboBox on that WinForm 将其添加为该WinForm上ComboBox的数据源

I can not find a way how to read data from that Sheet1... 我找不到一种方法来从该Sheet1中读取数据...

It seems, that there are not a lot developers (at least at stackoverflow) that work with Excel workbooks in Visual Studio / VSTO), but still this is is how I got this basic stuff working - in case if this is helpful to someone else 似乎没有很多开发人员(至少在stackoverflow上)可以在Visual Studio / VSTO中使用Excel工作簿),但这仍然是我使这些基本知识起作用的方式-以防万一这对其他人有帮助

Since my code was in the Worksheet's *.cs file it turned out I can access project's xlsx file this way: 由于我的代码在工作表的* .cs文件中,因此我可以通过以下方式访问项目的xlsx文件:

var excel = (Excel.Application)this.Application;
var xlbook = (Excel.Workbook)excel.ActiveWorkbook; 

var worksheets = xlbook.Worksheets;
var sheet = (Excel.Worksheet)worksheets["Sheet3"];
int row = 2;//1st row for column titles

while (!string.IsNullOrEmpty(((Excel.Range)sheet.Cells[row, 2]).Value))
{
    var weight = ((Excel.Range)sheet.Cells[row, 3]).Value;
    row++;
}

Some additional things about processing data from Excel sheet in c# code, I found out (maybe that's helpful for someone): 我发现了一些有关使用C#代码处理Excel工作表中的数据的其他事项(可能对某人有用):

  1. the .NET type for Excel Cell is Excel.Range (at least I didn't find any other option) Excel Cell的.NET类型是Excel.Range(至少我没有找到其他选项)
  2. when reading cell that is empty in Excel file, it's value on .NET side is null, not "" 读取Excel文件中为空的单元格时,.NET端的值为null,而不是“”
  3. values that seem to be strings on Excel side - can turn to be different types when loaded on c# side. 在Excel端看来是字符串的值-在c#端加载时可以变成不同的类型。 I don't know if it's the best way, but I solved it like this: 我不知道这是否是最好的方法,但是我这样解决了:

var weight = (((Excel.Range)sheet.Cells[row, 3]).Value); var weight =((((Excel.Range)sheet.Cells [row,3])。Value);

if (weight is double)
{
    product.Weight = ((double)((Excel.Range)sheet.Cells[row, 3]).Value).ToString();
}
else if (weight is string)
{
    product.Weight = ((Excel.Range)sheet.Cells[row, 3]).Value;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel工作表和MS Access表加入Visual Studio C# - Excel sheet and MS Access table Join visual studio C# 如何在 microsoft.office.interop.excel 中格式化我的导出到 excel 工作簿? - How do I format my export to excel workbook in microsoft.office.interop.excel? 如何检查 Excel 工作簿或工作表是否受密码保护? - how to check Excel Workbook or sheet is password protected or not? 如何将Excel工作表从工作簿A复制到工作簿B - How to copy an excel sheet from workbook A to workbook B C# Visual Studio Excel 加载项:如何检测 Excel Office 主题更改? - C# Visual Studio Excel Add-in: How can I detect Excel Office Theme Change? 通过Visual Studio桌面应用程序创建Excel工作簿按钮单击并填充sql数据 - Create Excel Workbook via Visual Studio desktop application Button Click and fill sql data 我已经使用Visual Studio Express C#为2010 Office(64位)编写了Excel代码,如何在2010 Office(32位)上运行它? - I have written excel code for 2010 Office (64 bit) with visual studio express c#, how to run it on 2010 office (32 bit)? 如何“清理” Microsoft.Office.Interop.Excel.Workbook - How to “clean up” Microsoft.Office.Interop.Excel.Workbook VSTO问题-无法创建Visual Studio Excel工作簿项目 - VSTO problem - cannot create visual studio excel workbook project 如何在未安装Office 2010的计算机上安装带有MS Access数据库的Visual Studio 2010安装项目? - How to install visual studio 2010 setup project with MS Access database on a computer which is not having Office 2010 installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM