hi guys,
below is a script that i need yr help on
i got this to print out a csv to excel
i'm trying to modify it to read a .txt file to convert an excel
hm...doesn't seem to work still any idea why it can't run?
is it cos i should not be open my file in the win32::OLE module? -
-
**************************************************************
-
# Start Excel and create new workbook with 9 sheets
-
use Win32::OLE qw(in valof with);
-
use Win32::OLE::Const 'Microsoft Excel';
-
use Win32::OLE::NLS qw(:DEFAULT :LANG :SUBLANG);
-
-
my $lgid = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
-
$Win32::OLE::LCID = MAKELCID($lgid);
-
-
$Win32::OLE::Warn = 3;
-
-
my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
-
$Excel->{SheetsInNewWorkbook} = 9;
-
my $Book = $Excel->Workbooks->Add;
-
my $Sheet = $Book->Worksheets(1);
-
my $sheet_count = 0;
-
-
# **************************************************************
-
# Read each 'selected' file and load them into an array
-
-
-
my $txtName;
-
my $xlsRange;
-
-
foreach $file (@txtFiles) {
-
open (txtFILE, $file) || die "Cannot open $file $!\n";
-
my @rows;
-
$txtFile = $file;
-
$#rows = -1;
-
$#fields = -1;
-
-
# Skip the first record (too long) of each file, we'll deal with it later
-
my $inBuf;
-
$sheet_count++;
-
-
while ($inBuf = <txtFILE>) {
-
my @fields;
-
chomp($inBuf);
-
if (scalar @fields > $line_limit) {last;} # Runaway safety net
-
-
$inBuf =~ s/^\"//; # Take out any LEADING or
-
$inBuf =~ s/\"$//; # TRAILING double quotes
-
-
# OK, Process this record
-
@fields = split(/\,/,$inBuf);
-
# print "\nProcessing record ".scalar(@rows)."\n @fields";
-
push @rows, [@fields];
-
-
} # End of while inBuf, or we exceeded our Runaway Safety Net
-
-
close csvFILE;
-
# *******************************************************************
-
# Build the Spreadsheet from the CSV Array
-
#
-
print "\nLoading Sheet \($sheet_count\) of $xlsFile from $txtFile \n";
-
$Sheet = $Book->Worksheets($sheet_count);
-
$csvName = substr($txtFile,0,6);
-
$Sheet->{Name} = "$txtName";
-
-
-
# Add csv data to spreadsheet
-
print "\n\nAdding data from $csvFile to Sheet $xlsFile\\$csvName\n";
-
print "\tUsing a range of A4:J50 \n";
-
$xlsRange = sprintf("A1:J%d", 2+$#rows);
-
$Range = $Sheet->Range("$xlsRange");
-
$Range->{Value} = \@rows;
-
-
} # End of Foreach csvFile
-
-
# *******************************************************************
-
-
# Save workbook to file $xlsFile
-
unlink $xlsFile if -f $xlsFile;
-
$Book->SaveAs("$directory\\$xlsFile");
-
$Book->Close;
-
-
print "End of import.\n";
-
-
11 4516
What format are you expecting? I am getting the output from your script, with data from each text file written in each sheet of the excel file.
What format are you expecting? I am getting the output from your script, with data from each text file written in each sheet of the excel file.
hm..yeah
that's what i want...but there seems to be some error
it's not working the script
i'm not gettng any output
ok i've got it working thanks
erm..but there a problem
my excel sheet has numbers that are too long
like for example:
526236273282392323
they output in excel :
5.26+E14
any idea how to display it as the original format?
and not as the E14 format?
hm..okok another better idea of the problem
it outputting as a general type..
i need to format it to a text type for the cell using win32::OLE
anyone know how?
hm..okok another better idea of the problem
it outputting as a general type..
i need to format it to a text type for the cell using win32::OLE
anyone know how?
The problem is not with the module or the perl script. It is the property of Excel to truncate large numbers in the exponential format. Only way is to print all the numbers into excel sheet within single/double quotes. You would invariably end up having all numbers printed within quotes.
If that is okay, replace the line: -
@fields = split(/\,/,$inBuf);
-
with: -
my @tempfields = split(/\,/,$inBuf);
-
foreach(@tempfields) {
-
if($_=~/^\s*\d+\s*$/) {
-
push @fields,"\'$_\'"; ##put single-quotes around numbers
-
}
-
else { push @fields,$_;} ## no modification for non-digit string
-
}
-
If you want, you can refine this further to include quotes only around large numbers(greater than 11 digits).
right i just place my recent code for yr reference:
let me define it a bit further cos i tried yr method, doesn't seem to work
i got a .txt file containing the following information:
91000000,34526812233930
72788171, 345268812233931
112332332, 34526800812233932
1 rows selected
and i've done some functions in my script to get rid of the 1st column in text file and the "1 row selected"
so basically i'm left with the long number, still unable to get the original format out..
hm..pls help? thanks -
# **************************************************************
-
# Start Excel and create new workbook with 9 sheets
-
use Win32::OLE qw(in valof with);
-
use Win32::OLE::Const 'Microsoft Excel';
-
use Win32::OLE::NLS qw(:DEFAULT :LANG :SUBLANG);
-
-
my $lgid = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
-
$Win32::OLE::LCID = MAKELCID($lgid);
-
-
$Win32::OLE::Warn = 3;
-
-
my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
-
$Excel->{SheetsInNewWorkbook} = 9;
-
my $Book = $Excel->Workbooks->Add;
-
my $Sheet = $Book->Worksheets(1);
-
my $sheet_count = 0;
-
-
# **************************************************************
-
# Read each 'selected' file and load them into an array
-
-
-
my $csvName;
-
my $xlsRange;
-
-
foreach $file (@csvFiles) {
-
open (csvFILE, $file) || die "Cannot open $file $!\n";
-
my @rows;
-
$csvFile = $file;
-
$#rows = -1;
-
$#fields = -1;
-
-
$#fields = -1;
-
-
# Skip the 1st record of each file & deal with it later
-
# Declare the input buffer
-
my $inBuf;
-
-
-
$sheet_count++;
-
-
while ($inBuf = <csvFILE>) {
-
my @fields;
-
chomp($inBuf);
-
if (scalar @fields >= $line_limit) {last;} # Runaway safety net
-
-
$inBuf =~ s/^\"//; # Take out any LEADING or
-
$inBuf =~ s/\"$//; # TRAILING double quotes
-
-
# OK, Process this record
-
@fields = split(/\,/,$inBuf);
-
-
-
$ind_del=0;
-
splice(@fields,$ind_del,1); #delete one element from that position
-
$ind_del=2;
-
splice(@fields,$ind_del,1);
-
$ind_del=3;
-
splice(@fields,$ind_del,1);
-
$ind_del=7;
-
splice(@fields,$ind_del,1);
-
-
-
# print "\nProcessing record ".scalar(@rows)."\n @fields";
-
push @rows, [@fields];
-
-
} # End of while inBuf, or we exceeded our Runaway Safety Net
-
-
close csvFILE;
-
# *******************************************************************
-
# Build the Spreadsheet from the CSV Array
-
#
-
print "\nLoading Sheet \($sheet_count\) of $xlsFile from $csvFile \n";
-
$Sheet = $Book->Worksheets($sheet_count);
-
$csvName = substr($csvFile,0,6);
-
$Sheet->{Name} = "$csvName";
-
-
-
# Add csv data to spreadsheet
-
print "\n\nAdding data from $csvFile to Sheet $xlsFile\\$csvName\n";
-
print "\tUsing a range of A1:J50 \n";
-
$xlsRange = sprintf("A4:A%d", 3+$#rows);
-
$Range = $Sheet->Range("$xlsRange");
-
$Range->{Value} = \@rows;
-
-
-
} # End of Foreach csvFile
-
-
# *******************************************************************
-
-
# Save workbook to file $xlsFile
-
unlink $xlsFile if -f $xlsFile;
-
$Book->SaveAs("$directory\\@csvFiles.XLS");
-
$Book->Close;
-
-
print "End of import.\n";
-
ok sorrie
i got it..
hm..but any idea how i can remove the single quotes?
i won't want the single quotes there
ok sorrie
i got it..
hm..but any idea how i can remove the single quotes?
i won't want the single quotes there
As I said before, excel uses scientific notation to display large numbers. If you don't want single quotes around large numbers, then you have to bear with this notation.
There is one workaround, that is to change the cell to text format.
In Spreadsheet::WriteExcel , you get methods to change format of cells like: -
my $format = $workbook->add_format();
-
$format->set_num_format('text');
-
But, I don't know if Win32::OLE supports it. I haven't explored much on the SetProperty() method available in Win32::OLE.
But, even in this case when you have number formatted as text, you will get a green triangle at left corner of the cell indicating a possible error for putting number in text formatted cell.
So, you have to choose between green triangle next to large numbers and single-quotes. I am stressing again that this is more to do with Excel application and not with your script.
hm...i think there's some sort like format in win32::OLe
i aren't too sure and i'm trying to find out too
yeah i know about the green triangle thingy
cos when i actually use this script on a csv file it works, but when i use it on a text file it doesn't so i'm pretty puzzle by it
in any case i can't have the quotes 'cos this inputs is gonna be read by another system which apparrently can't read the quotes..haha
but in any case thanks for yr help
ok done found it..
just in case ppl who want to know how it's done in script using win32::OLE
here's how: -
-
-
$Sheet->Range("A:A")->{NumberFormat} = '@';
-
$Sheet->Cells(4,%d)->{NumberFormat} = "\@";
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: M. Uppal |
last post by:
How do i convert fraction to time in c#. I need to convert 0.25 to time.
Excel does this by using Selection.NumberFormat = "h:mm:ss;@" of the Cell
Format function.
thanks,
M. Uppal
|
by: Ramakrishnan Nagarajan |
last post by:
Hi,
I am converting Excel data into a Dataset in C#. There are around 24 columns
in the Excel Sheet.
First I tried to insert one row with correct values in the Excel sheet. i.e.
for text columns...
|
by: dleh |
last post by:
I have been tasked with producing a server application that will convert
excel files to html.
I have no experience of developing under Windows, (but plenty of UNIX/C++
experience) but I picked...
|
by: UKuser |
last post by:
Hi Guys,
I have a program which converts Excel spreadsheets to Javascript and
allows interactivity. However it can't convert it to PHP, which is
obviously better for users to view (in case J/S...
|
by: rwiegel |
last post by:
I'm trying to read rows from an Excel file and display them in an
ASP.NET DataGridview. I am using C# for the code file. I am using
OleDb to read from the Excel file. The columns that contain...
|
by: chrspta |
last post by:
I am new to Visual basic. I need a program using VB6 that converts txt files to excel file.Description is in the below:
The form should have the Drive list, Dir list, file list and cmdConvert...
|
by: Dirk Hagemann |
last post by:
Hello,
From a zone-file of a Microsoft Active Directory integrated DNS server
I get the date/time of the dynamic update entries in a format, which
is as far as I know the hours since january 1st...
|
by: PW |
last post by:
Hi,
When I run the following command, some fields are ending up blank when
the clearly have values them in Excel. I have tried converting the
columns to general and text. The ones that are...
|
by: =?Utf-8?B?bGF3ODc4Nw==?= |
last post by:
i am looking for some MS Excel formula or MS Visal Basic Marco for converting
Date to Lunar Date.
tks
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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...
| |