473,544 Members | 263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing into an Existing Excel file

68 New Member
Hi,


I had problems writing in an existing Excel File.

There is an excel file with a worksheet. I would like to open this existing Excel file and add another worksheet and write data into it. But I dont know how to open an existing excel file.


can anyone give me some hint or help?



Thanks


Ravi
Jan 28 '08 #1
3 3767
eWish
971 Recognized Expert Contributor
Ravi,

Have a look at the Spreadsheet::Wr iteExcel Module. Also, I believe that there are others as well.

--Kevin
Jan 29 '08 #2
WinblowsME
58 New Member
Here's a sample.

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. use Cwd;
  4. use OLE;
  5. use Win32::OLE::Const "Microsoft Excel";
  6.  
  7. &init;
  8.  
  9. sub init
  10. {
  11.    my $directory = &getcwd;
  12.  
  13.    &edit_excel ( "$directory/Temp.xls" );
  14. }
  15.  
  16. sub edit_excel
  17. {
  18.    my ( $xl_report ) = @_;
  19.    my ( $excel, $workbook, $sheet, $row );
  20.  
  21.    $excel = CreateObject OLE 'Excel.Application';
  22.    $excel -> {Visible} = 1;
  23.  
  24.  
  25.    $workbook = $excel    -> Workbooks  -> Open ( "$xl_report" ) or &terminate ( "open $xl_report" );
  26.    $sheet    = $workbook -> Worksheets -> Add ( { After => $workbook -> Worksheets ( $workbook -> Worksheets -> {Count} ) } );
  27.  
  28.    $sheet -> Activate;
  29.    $sheet -> Range ( "A:D" ) -> Font -> {Size}        = 8;
  30.    $sheet -> Range ( "A:D" ) -> Font -> {Bold}        = "True";
  31.    $sheet -> Range ( "A:D" ) -> {HorizontalAlignment} = xlHAlignLeft;
  32.    $sheet -> Range ( "A:A" ) -> {NumberFormat}        = "\@";
  33.    $sheet -> Range ( "B:D" ) -> {NumberFormat}        = "#,##0_);[Red](#,##0)";
  34.  
  35.    $sheet -> Range ( "A1"  ) -> {Value} = "Column 1";
  36.    $sheet -> Range ( "B1"  ) -> {Value} = "Column 2";
  37.    $sheet -> Range ( "C1"  ) -> {Value} = "Column 3";
  38.    $sheet -> Range ( "D1"  ) -> {Value} = "Column 4";
  39.  
  40.    $row   = 2;
  41.  
  42.    $sheet -> Range ( "A" . $row ) -> {Value} = "1";
  43.    $sheet -> Range ( "B" . $row ) -> {Value} = "2";
  44.    $sheet -> Range ( "C" . $row ) -> {Value} = "3";
  45.    $sheet -> Range ( "D" . $row ) -> {Value} = "4";
  46.  
  47.    $sheet -> Range ( "A:D" ) -> {Columns} -> Autofit;
  48.    $sheet -> Range ( "A2"  ) -> Activate;
  49.    $excel -> ActiveWindow    -> {FreezePanes} = "True";
  50.    $sheet -> {Name} = "New Sheet Name" . $workbook -> Worksheets -> {Count};
  51.  
  52.    $excel    -> {DisplayAlerts} = 0;
  53.  
  54.    # $workbook -> SaveAs ( "$xl_report" );
  55.  
  56.    $workbook -> Save;
  57.    $workbook -> Close;
  58.    $excel    -> Quit;
  59. }
  60.  
  61. sub terminate
  62. {
  63.    print "\a\a\nCould not @_ for the following reason:\n\n$!\n";
  64.    print "This program will now terminate.  Press <ENTER> to exit.";
  65.    <STDIN>;
  66.    exit;
  67. }
Jan 31 '08 #3
thanawala27
68 New Member
Thanks for your replies, I got what I wanted.


Ravi
Feb 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
4030
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it again, Excel hangs. OR if I open Excel again (say from a desktop icon) before I close Access, Excel hangs. (this has happened for both 97 & 2000...
2
10595
by: Anne Sachleben via AccessMonster.com | last post by:
I am using the TransferSpreadsheet function to export a query result to a specific worksheet in an Excel file titled "report". I want the result to be exported to the worksheet titled "facts". Here are the parameters I entered: Transfer Type: Export Spreadsheet Type: Microsoft Excel 8-10 Table Name: facts query File Name: C:\report.xls...
10
52118
by: Aaron | last post by:
Hello, I have a small application that I need to save data from 7 text boxes in to a csv file. This will entail btnNext_Click function that will create a new csv file and enter the 7 data fields in the csv file; as well, each click on next will return carriage to a new line. Client does not what his data in an Access database(he hates...
2
4223
by: Brent Bortnick | last post by:
Hello, Is it possible to send data from ASP and put it into a existing template? i don't want to create an excel document I only want to send one piece of information from ASP to the Exisiting Template. Background: We have created a online quote number genetrator for our sales team and we want it to open the excel quote template and put...
4
9916
by: vancehome | last post by:
I have a series of macros running everynight creating tons of excel files (using transfer spreadsheet). I then have another program that zips them up and emails them all over the place. It then deletes the originals. I would like to move away from the emailing and begin pushing the files out directly via the WAN. The problem is I need to DELETE...
1
1695
by: indrajith_varma1 | last post by:
Hi Can we read from an existing excel file, retrieve the values, take the 1st row, append apostophe to all the values in the first row and write back the appended values to the 1st row of the same file? If there is a solution, please do let me know. its very urgent. In anticipation of an immediate response Thanks & Regards
5
11038
by: rasmitasah25 | last post by:
hi, I am very new to perl.I have written a perl script which is writing data into an excel file.The problem is that it is creating one new excel file while executing but my need id to write into an existing .xls file. currently I am using: my $workbook = Spreadsheet::WriteExcel::Big->new("file.xls"); for that which method Should I use??...
3
1869
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have an sql database and I need to do the following, hoping there are some examples I can look at to impliment. 1. Copy an excel file on the server, just using a new name. For example template.xls is an existing file on the server so the .net application would copy it on the server and name it new_day_year.xls. 2. insert data from an...
20
3529
by: Marin Brkic | last post by:
Hello all, please, let me apologize in advance. English is not my first language (not even my second one), so excuse any errors with which I'm about to embarass myself in front of the general public. Second, I'm relatively new to python, so sorry if this seems like a stupid question. I'm trying to find a way to write data to excel cells...
0
7424
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7709
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5909
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5297
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1841
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
661
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.