473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Writing into an Existing Excel file

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 3756
eWish
971 Expert 512MB
Ravi,

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

--Kevin
Jan 29 '08 #2
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
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
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...
2
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". ...
10
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...
2
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...
4
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...
1
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...
5
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...
3
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...
20
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...

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.