Example Code

Write Data Into New Worksheet of Existing Excel Workbook

Code and Documents

Attachment

Overview
This example opens an existing Excel workbook, inserts a new worksheet, and inserts a numerical table of data into the new worksheet. Then it saves and closes the workbook.

 


Description
This VI opens an existing Microsoft Excel document, adds a new worksheet to that document, writes an array of random data to the new worksheet, and then saves and closes the Excel document. It shows how to use the report generation toolkit to open and modify existing excel documents, and how to use this toolkit to add additional worksheets into an existing Excel workbook.

 


Requirements

  • LabVIEW 2012 (or compatible)
  • Report Generation Toolkit for Microsoft Office


Steps to Implement or Execute Code

  1. Download the attached folder to your computer
  2. Open the VI "Write Data Into New Worksheet of Existing Workbook 2012 NIVerified.vi"
  3. Choose of the path of the worksheet
  4. Run the VI

 

Additional Information or References
VI Snippet

 Block Diagram.PNG

 

 **This document has been updated to meet the current required format for the NI Code Exchange.**

 

Colden

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
301
Member
Member
on

i write these codes in Labview 8.6 , there is an error in it , i cannot understand it , could you tell how to solve it ?

ColdenR
NI Employee (retired)
on

If you need help getting this example to work for you, or getting some code that you've written to work for you, then you should post on the forums (http://forums.ni.com/).

Colden
Drouilhet
Member
Member
on

Colden, thanks for posting this.  Being able to write to a specific worksheet is a capability that I've been seeking.  Do these VIs actually launch Excel to accomplish this?  My organization uses Open Office, an open source equivalent of Microsoft Office.  The spreadsheet program can read and write xls files, so we have no problem if the spreadsheet VIs don't actually rely on Microsoft Excel.  Can you please advise on this?

Steve Drouilhet
ColdenR
NI Employee (retired)
on

Hello Drouilhet,

I believe the Report Generation Toolkit requires Microsoft Office to work (see http://sine.ni.com/nips/cds/view/p/lang/en/nid/209050). I also looked through the actual function calls, and they reference Excel specifically. The functions I use above do open and close Excel, and in fact can leave Excel open if necessary.

So I would say that they most likely do rely on Microsoft Excel. That said, there might be a way to work around those issues - I'm not sure.

Colden
Cor_Kuin
Member
Member
on

3. "Excel Add worksheet VI" Adds a new worksheet to the workset collection. The new worksheet becomes the current worksheet.

So I think 2. and 4. are not really neccesary unless you want to know the number of the active worksheet.

janybasha
Member
Member
on

hello, thanks for uploading such a usefull file...i am working on reading data from serial port to excel sheet ...as you said replace this code as your data (but I don't know how to replace with serial port code) can you also post some example file to read data from serial port to excel sheet ...please thank you in advance

ColdenR
NI Employee (retired)
on

Hey janybasha!

The best way to get your question answered is to post on forums.ni.com the details of your question. You can link to this example if you want, but really the first hurdle is to get serial data read into LabVIEW. I'd also like to keep this example focused on a single task.

When you post, make sure you include details on what type of data you're reading in and what hardware you're using to read the data.

When you transition into pushing the data into excel, you'll need to think about whether you want to send all the data to excel at once, or if you want to send smaller chunks of data as your program is running. If you choose the latter, you'll probably want to use the producer consumer architecture (http://www.ni.com/white-paper/3023/en/), and you'll want to make sure you move the initialization and closing portions of my code outside of your loop.

Colden
drmotsei
Member
Member
on

Hi.

when using this example in a continuos data logging system, this code keeps on adding a worksheet instead of appending to the existing worksheet. can anyone assist in terms of as to how can one monipulate this code to add a worksheet only once and thereafter append addittion data to it?

Regards

Drmotsei

Cor_Kuin
Member
Member
on

Write last row.png

Cor_Kuin
Member
Member
on

When I have new data available I send it to a loop with this code. Right now i run it every second and it works great. Just put the file path and report reference in shift registers.