Perl | Creating Excel Files
Last Updated :
07 May, 2019
Excel files are the most commonly used office application to communicate between computers. It is used to create rows and columns of text, numbers, and formulas for calculations. It is a good way to send reports. This demonstration works on Linux, Windows and other platforms as well. In excel, rows are numbered from 1 to n… and columns are marked by letters from A, B, C and so on.. hence, A1 refers to the top left corner. For creating excel files with Perl you can use padre IDE, we will also use Excel::Writer::XLSX module.
Perl uses write() function to add content to the excel file.
Syntax: write(cell_address, content)
Parameters:
cell_address: Address of the cell where content is to be added.
content: which is to be added to the worksheet.
Creating an Excel File
Excel Files can be created using Perl command line but first we need to load Excel::Writer::XLSX module.
perl
#!/usr/bin/perl
use Excel::Writer::XLSX;
my $Excelbook = Excel::Writer::XLSX->new( 'GFG_Sample.xlsx' );
my $Excelsheet = $Excelbook->add_worksheet();
$Excelsheet->write( "A1", "Hello!" );
$Excelsheet->write( "A2", "GeeksForGeeks" );
$Excelsheet->write( "B1", "Next_Column" );
$Excelbook->close;
Output:

Here is how the Program works:
Step 1: Load the module Excel::Writer::XLSX.
Step 2: Create an object $Excelbook which represents the whole Excel File.
Step 3: Call write() method to add data to the worksheet.
Step 4: Now, save the file with
.pl extension.
Step 5: Run your .pl file on command line and Excelsheet will be created.
Use of Basic Formulas
Excel provides the use of various Mathematical Formulae for the ease of calculations on the excelsheets like balance sheet, business records, etc.
Here is the description of two basic formulas of Excel:
- Addition:
Excel provides a method 'SUM' for the addition of values on specific cells.
Syntax: =SUM(Start, End)
Parameter:
Start: Address of the starting cell
End: Address of the Ending cell
Returns: the summation of values between the Starting and Ending cell.
perl
#!/usr/bin/perl
use Excel::Writer::XLSX;
my $Excelbook = Excel::Writer::XLSX->new( 'GFG_Sample.xlsx' );
my $Excelsheet = $Excelbook->add_worksheet();
# Writing values at A1 and A2
$Excelsheet->write( "A1", 55 );
$Excelsheet->write( "A2", 47 );
# Adding without use of SUM method
$Excelsheet->write( "A3", "= A1 + A2" );
# Addition of a Range of cells
$Excelsheet->write( "A4", " =SUM(A1:A3)" );
Output:
- Count:
This function in Excel is used to count all the cells in the given range which contain only numeric value.
Syntax: =COUNT(Start, End)
Returns: count of all cells containing numeric value
Perl
#!/usr/bin/perl
use Excel::Writer::XLSX;
my $Excelbook = Excel::Writer::XLSX->new( 'GFG_Sample.xlsx' );
my $Excelsheet = $Excelbook->add_worksheet();
# Writing values
$Excelsheet->write( "A1", 5 );
$Excelsheet->write( "A2", 40 );
$Excelsheet->write( "A3", "Hello" );
$Excelsheet->write( "A4", 10 );
# Addition of a Range of cells
$Excelsheet->write( "A5", "Count =");
$Excelsheet->write( "B5", "=COUNT(A1:A4)" );
Output:
Adding Colors to ExcelSheet
Colors can be used in ExcelSheets to mark specific values separately. These colors are specified with the use of add_format() method.
Syntax: add_format(color=> 'color_name')
perl
#!/usr/bin/perl
use Excel::Writer::XLSX;
my $Excelbook = Excel::Writer::XLSX->new( 'GFG_Sample.xlsx' );
my $Excelsheet = $Excelbook->add_worksheet();
# Setting value of color
my $color1 = $Excelbook->add_format(color=> 'blue',);
my $color2 = $Excelbook->add_format(color=> 'red',);
my $color3 = $Excelbook->add_format(color=> 'green',);
$Excelsheet->write( "A2", "Geeks", $color1 );
$Excelsheet->write( "B2", "For", $color2 );
$Excelsheet->write( "C2", "Geeks", $color3 );
$Excelbook->close;
Output:
Adding values at specific coordinates
Values can be added at specific coordinates by providing address of cells at which value is to be added.
Syntax: write(R,C, "value")
Parameters:
R and C are the coordinates of the Row and Column respectively.
perl
#!/usr/bin/perl
use Excel::Writer::XLSX;
my $Excelbook = Excel::Writer::XLSX->new( 'GFG_Sample.xlsx' );
my $Excelsheet = $Excelbook->add_worksheet();
$Excelsheet->write( 0, 0, "Hello!" );
$Excelsheet->write( 1, 0, "GeeksForGeeks" );
$Excelsheet->write( 3, 2, "Welcome!!!" );
$Excelbook->close;
Output:
Similar Reads
Perl | Reading Excel Files Excel sheets are one of the most commonly used methods for maintaining office records, especially to work on applications where non-developers and even managers can provide input to the systems in batches. However, the issue is to read the content from a file created by Microsoft Excel using Perl. F
4 min read
Upload and Read Excel File in Flask In this article, we will look at how to read an Excel file in Flask. We will use the Python Pandas library to parse this excel data as HTML to make our job easier. Pandas additionally depend on openpyxl library to process Excel file formats. Before we begin, make sure that you have installed both Fl
2 min read
Editing Excel Macros in Excel Excel Macro is a set of actions that can be recorded, saved, used multiple times. This feature saves us a lot of time when dealing with repetitive tasks and huge data sets. We can always make changes to an existing Macro in Excel. There are two ways to record and run macros: Excel Commands Excel Vis
1 min read
Excel to PDF Converter An Excel to PDF converter is a tool that simplifies the process of converting Excel file to Pdf, allowing users to preserve the layout and data of their spreadsheets in a universally accessible format. #content-iframe {width: 100%; height: 380px;} @media (max-width: 768px) {.article--viewer .a-wrapp
3 min read
What is a Excel Spreadsheet Excel works like other spreadsheet programs but offers more features. Each Excel file is called a workbook, which contains one or more worksheets. You can start with a blank workbook or use a template.A worksheet is a grid of 1,048,576 rows and 16,384 columns, over 17 billion cells, for entering and
7 min read
Power BI - How to upload Excel Files to Dashboard? Power BI Provides a function to upload excel data into Dashboard form. The dashboard in Power BI is a single page that shows all data and visualization of reports, and datasets in a specific structure way. For Uploading data in Dashboard first have to upload our excel file in the workbook and then w
2 min read