Wednesday, 15 June 2011

c# - How to access data in sheet in my Visual Studio Office Excel workbook? -


I need to create "document level optimization" with C # code (and not Excel add-in!)

I have created a Visual Studio Office Excel 2010 workbook project type. It creates a workbook with 3 sheets in my project, I've added "configuration" information to one of those sheets.

Give me this configuration notification program ( Sheet1 in a button - press it on the button

  1. Load Configuration
  2. Open a WinForm
  3. Current configuration data in that form,

but Either way I do not have to do this ...

If I try to start the Sheet1 class, the compiler is expected to have two parameters - Microsoft.Office.Tools.Excel.Factory and IServiceProvider , Lake And I'm calling it with a button which is placed on sheet 2 - then it later Excel workbook has already been opened ... should not 1 be started automatically?

So, how can I access Sheet1 from code # of my VSTO project?

Edit

I have a button on Sheet 2, which

  1. load some data from Sheet1
  2. Start Win
  3. That WinForm

for a combo box As a datasource, I can not find any way to read data from that sheet 1 ...

It appears that there are not many developers (at least on StackerWrof flow) that are available in Excel workbooks Works with Visual Studio / VSTO), but still it is how I am getting this basic work - if it is helpful for someone else

Since my code worksheet *. The CC file was in, it turned out that I could use the project Xlsx file like this:

  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; // column title while the first line (! String.ISNLLCTI (excel range). Seals [row, 2]) value) {var weight = (excel range) sheet cell [row, 3]). value; Line ++; }  

Some additional things about data processing from excel sheets in C # code, I found out (maybe this is useful for anyone):

  1. .NET is Excel for type Excel cell. Range (at least I did not find any other option)
  2. When Excel is recorded in Excel, its value is NET side is zero, no ""
  3. Values ​​which start to be string on the Excel side - C can be different types of loading on the side. I do not know if this is the best way , But I have solved it like this:

var weight = (Excel.Range) sheet.kel [line, 3]). Values ​​);

  if (weight is doubled) {product.Weight = ((Excel.Range)) sheet. Seals [row, 3].) Value ostring (); } And if (weight is string) {product.Weight = (Excel.Range) sheet. Call [row, 3]). Values; }  

No comments:

Post a Comment