I need to keep the date time format in Excel but it is saving as a plain text in excel.
Please find the code as follows,
use strict;
use warnings;
use Spreadsheet::WriteExcel;
my #array = ("Name","2014-04-01 00:00:00AM","2014-04-01 22:00:00AM","Value");
my $filename = "report.xls";
my $workbook = Spreadsheet::WriteExcel->new($filename);
my $worksheet = $workbook->add_worksheet();
$worksheet->write_row($row++,$col,\#array);
Thanks in advance!
The documentation states:
$worksheet->write_row(0, 0, $array_ref);
# The above example is equivalent to:
$worksheet->write(0, 0, $array[0]);
$worksheet->write(0, 1, $array[1]);
$worksheet->write(0, 2, $array[2]);
If you pass an array ref for batch writing, I guess it's not possible to specify what format to use for each column. However, there are other methods like
write_date_time($row, $col, $date_string, $format)
You would have to write each row cell separately, but you would get the desired format. (I guess)
Related
I'm trying to parse a csv file and convert it to XML. The .csv file consists of a list of entries, separated by commas. So, two sample entries look like this:
AP,AB,A123,B123,,,
MA,NA,M123,TEXT,TEXT,TEXT_VALUE
Some field are blank in file and file has no header rows.
Any suggestions how to go with this. Haven't figured out how to go with this.
Thanks.
print "<myXML>\n";
while (<>) {
print "<aRow>$_</aRow>\n";
}
print "</myXML>\n";
Or, using XML::LibXML, something like this:
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->createDocument;
my $root = $doc->createElement('myXML');
$doc->setDocumentElement($root);
$root->appendChild($doc->createTextNode("\n"));
while (<>) {
chomp;
my $row = $doc->createElement('aRow');
$root->appendChild($row);
$row->appendChild($doc->createTextNode($_));
$root->appendChild($doc->createTextNode("\n"));
}
print $doc->toString;
Of course, if you told us what the output should look like, we could probably come up with something a little more sophisticated!
Im new bee to perl trying to read excel sheet with simple condition..
problem is reading all records and checking whether b columns has "Always null" string if yes then display output from Column "C"
Example : For Always Null condtion
Expected output : a,b,c,f,g
#!/usr/bin/perl
use strict;
use warnings;
use v5.14;
use Spreadsheet::ParseExcel;
use Data::Dumper;
my $ws = Spreadsheet::ParseExcel
->new()
->parse('yourfile.xls')
->worksheet(0);
my ($i, $r_max) = $ws->row_range;
my ($cell_B, $cell_C);
while ($i <= $r_max) {
$cell_B = $ws->get_cell($i,1);
$cell_C = $ws->get_cell($i,2);
say $cell_C->value
if $cell_B
and $cell_C
and $cell_B->value eq 'Always null';
} continue { $i++ }
I am trying to write an existing XLSX file using the win32::OLE module.I want to insert the values cell by cell. I would also like to format the cell (font, colour, alignment etc). my requirement is to use win32::OLE module only,can you please suggest me which method i have to use,thanks in advance....
**formatstring.pl**
use strict;
use warnings;
use win32::OLE;
our $Excel1 = Win32::OLE->new('Excel.Application');
our $Workbook1= $Excel1->Workbooks->Open($outputfilepath);
my $sheet1 = $Workbook1->Worksheets(1);
our $RowCount=$sheet1->Usedrange->Rows->{Count};
my $g_DS_TestCaseID="sd123";
print "RowCount of outputfile: $RowCount";
$sheet1->Cells($RowCount+1,1)->{value}=$g_DS_TestCaseID;#here i want to format the text as bold with colorindex as green
I did something like this a long, long time ago. This isn't really an answer, but a pointer:
Try doing it in Excel while recording a macro. Most times the methods and values are available without change over the COM interface. The recorded Macro gives you at least pointers for the direction in which to look, often times it gives you the exact methods/variables you need to use.
You can check msdn, they have the answer:
http://msdn.microsoft.com/en-us/library/ff846982(v=office.14).aspx
It's simple:
use FindBin qw($Bin);
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
our $Excel = Win32::OLE->new('Excel.Application');
$Excel->{'Visible'} = 1;
$Excel->{DisplayAlerts} = 0;
our $Workbook = $Excel->Workbooks->Open("$Bin/Test.xls");
my $Sheet = $Workbook->Worksheets(1);
$Sheet->Range("A2")->{Font}->{Bold} = 1;
$Sheet->Range("A2")->{Interior}->{ColorIndex} = 36;
Script works fine, but ftp code uploading the xls but uploading with 0 byte, but if ftp code present in before of the following snippet, FTP Works Fine,
What is the error in code,
my $workbook = Spreadsheet::WriteExcel->new('TestRPT.xls');
my $worksheet = $workbook->add_worksheet('TestRPT Report');
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Spreadsheet::WriteExcel;
use POSIX qw(strftime);
my $CurTimeStamp=time;
my $LastSunTimestamp=($CurTimeStamp - 168*60*60);
my $row;
my $PinNumber;
my $PinAmount;
my $get_date;
my $get_time;
my $get_time_stamp;
my $DoFTPFlg = "yes";
# Create a new workbook and add a worksheet.
my $workbook = Spreadsheet::WriteExcel->new('TestRPT.xls');
my $worksheet = $workbook->add_worksheet('TestRPT Report');
# Write some text. in write function First Argument for ROW, Second Argument for COLUMN, Third Argument for Title/Text to display
$worksheet->write(0, 0, 'val1');
$worksheet->write(0, 1, 'val2');
$worksheet->write(0, 2, 'val3');
$worksheet->write(0, 3, 'val4');
my $cnt = 1;
$get_time_stamp = time;
$get_date = strftime("%m/%d/%y",localtime($get_time_stamp));
$get_time = strftime("%H:%M",localtime($get_time_stamp));
# Write some numbers.
$worksheet->write($cnt, 0, "val1");
$worksheet->write($cnt, 1, "val2");
$worksheet->write($cnt, 2, "val3");
$worksheet->write($cnt, 3, "val4");
if ($DoFTPFlg eq "yes") {
print "DO FTP";
use Net::FTP;
my $ftp;
$ftp = Net::FTP->new("mysite.in", Debug => 0);
$ftp->login("user",'pass');
$ftp->cwd("/www/");
$ftp->put("TestRPT.xls");
$ftp->quit;
}
You should close your $workbook object before trying to do anything with the file.
From the documentation:
An explicit close() is required if the file must be closed prior to performing some external action on it such as copying it, reading its size or attaching it to an email.
Try a slight modification on your code. Instead of
$ftp->put("TestRPT.xls");
Create another file in the www directory and try to ftp that file. If that file is called test.txt, change your line to:
$ftp->put("TestRPT.xls");
Thus, the only change in your code is the name of the file being FTP'd. If your FTP works, the problem isn't with the FTP, but with the Spreadsheet::WriteExcel module. As Mat has already stated, you need to do an explicit close on your object.
If your FTP doesn't work, it probably is an issue with the FTP call (although it looks fine to me).
I want to save a range of cells in XLS sheet as a HTML file, using a Perl script. I have googled for solutions to this problem. But haven't found any.
However there is a solution similar to this using Excel VBA:
With ActiveWorkbook.PublishObjects.Add(xlSourceRange, _
"C:\Documents and Settings\Tim\Desktop\Page.htm", _
Selection.Parent.Name, _
Selection.Address(), _
xlHtmlStatic, "divExcelExport", _
"TestTitle")
.Publish (True)
.AutoRepublish = False
End With
So, I tried converting this to Perl code, but it gives me the following errors:
H:\test_code\data>perl a.pl
Win32::OLE(0.1702) error 0x800a03ec
in METHOD/PROPERTYGET "Add"
H:\test_code\data>
use strict;
use warnings "all";
use Win32::OLE;
my $Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application','Quit');
my $Book = $Excel->Workbooks->Open("H:\\test_code\\data\\test.xls");
my $Sheet = $Book->Worksheets(1);
$Book->PublishObjects->Add({SourceType=>5, # int value for xlSourceRange
FileName => "H:\\test_code\\data\\test2.html",
Source => $Sheet->Range("A1:B2")->Address,
HtmlType => 0, # xlHtmlStatic s int value.
});
print Win32::OLE->LastError();
$Book->Close(0);
exit();
Can some one please suggest a solution for this problem.
Note, that I want to preserve all the formatting of the columns, (Decimal, number of digits, $ Sign, color etc..). Any solution which doesn't involve usage of a commercial library is appreciated.
Thanks,
Rizwan.
This works for me, at least the HTML file is being generated:
use strict; use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
my $Excel = Win32::OLE->new('Excel.Application','Quit');
my $Book = $Excel->Workbooks->Open("C:\\test.xls");
$Book->PublishObjects->Add(xlSourceRange,
"C:\\output_file.htm", 'Sheet1', '$C$3:$C$10', xlHtmlStatic
)->Publish(1);
$Book->Close(0);
$Excel->Quit;