|
User Guide EasyXLS Basics Export files Export List to Excel file in .NET, Java and other programming languages
|
|
Export List to Excel file in .NET, Java and other programming languages
|
Concept
EasyXLS™
library allows you to export a list to an Excel file. The data in cells can be formatted using predefined formats or user-defined formats.
EasyXLS can be successfully used inclusively to export large Excel files having big volume of data with fast exporting time.
Concept in action
The below example shows how to export list to Excel in .NET, Java and export lists of data extracted from database in PHP, ASP, C++, VB6, VBS and Coldfusion.
|
Source code sample
|
|
|
|
// Create an instance of the class that exports Excel files
ExcelDocument workbook = new ExcelDocument();
// Create the list used to store the data
EasyXLS.Util.List listRows = new EasyXLS.Util.List();
// Add the header row to the list
EasyXLS.Util.List listHeaderRow = new EasyXLS.Util.List();
listHeaderRow.Add("ID");
listHeaderRow.Add("Data");
listRows.Add(listHeaderRow);
// Populate the list
EasyXLS.Util.List listRow1 = new EasyXLS.Util.List();
EasyXLS.Util.List listRow2 = new EasyXLS.Util.List();
listRow1.Add("1");
listRow1.Add("Data 1");
listRow2.Add("2");
listRow2.Add("Data 2");
listRows.Add(listRow1);
listRows.Add(listRow2);
// Create an instance of the class used to format the cells
EasyXLS.ExcelAutoFormat xlsAutoFormat = new ExcelAutoFormat();
xlsAutoFormat.InitAs(Styles.AUTOFORMAT_EASYXLS1);
// Export list to Excel file
workbook.easy_WriteXLSFile_FromList("C:\\Samples\\List to Excel.xls",
listRows, xlsAutoFormat, "List");
|
|
' Create an instance of the class that exports Excel files
Dim workbook As New ExcelDocument
' Create the list used to store the data
Dim listRows As New EasyXLS.Util.List
' Add the header row to the list
Dim listHeaderRow As New EasyXLS.Util.List
listHeaderRow.Add("ID")
listHeaderRow.Add("Data")
listRows.Add(listHeaderRow)
' Populate the list
Dim listRow1 As New EasyXLS.Util.List
Dim listRow2 As New EasyXLS.Util.List
listRow1.Add("1")
listRow1.Add("Data 1")
listRow2.Add("2")
listRow2.Add("Data 2")
listRows.Add(listRow1)
listRows.Add(listRow2)
' Create an instance of the class used to format the cells
Dim xlsAutoFormat As New ExcelAutoFormat
xlsAutoFormat.InitAs(Styles.AUTOFORMAT_EASYXLS1)
' Export list to Excel file
workbook.easy_WriteXLSFile_FromList("C:\\Samples\\List to Excel.xls", _
listRows, xlsAutoFormat, "List")
|
|
// Create an instance of the class that exports Excel files
ExcelDocument *workbook = new ExcelDocument();
// Create the list used to store the data
EasyXLS::Util::List *listRows = new EasyXLS::Util::List();
// Add the header row to the list
EasyXLS::Util::List *listHeaderRow = new EasyXLS::Util::List();
listHeaderRow->Add(new String("ID"));
listHeaderRow->Add(new String("Data"));
listRows->Add(listHeaderRow);
// Populate the list
EasyXLS::Util::List *listRow1 = new EasyXLS::Util::List();
EasyXLS::Util::List *listRow2 = new EasyXLS::Util::List();
listRow1->Add(new String("1"));
listRow1->Add(new String("Data 1"));
listRow2->Add(new String("2"));
listRow2->Add(new String("Data 2"));
listRows->Add(listRow1);
listRows->Add(listRow2);
// Create an instance of the class used to format the cells
EasyXLS::ExcelAutoFormat *xlsAutoFormat = new ExcelAutoFormat();
xlsAutoFormat->InitAs(Styles::AUTOFORMAT_EASYXLS1);
// Export list to Excel file
workbook->easy_WriteXLSFile_FromList("C:\\Samples\\List to Excel.xls",
listRows, xlsAutoFormat, "List");
|
|
// Create an instance of the class that exports Excel files
ExcelDocument workbook = new ExcelDocument();
// Create the list used to store the data
EasyXLS.Util.List listRows = new EasyXLS.Util.List();
// Add the header row to the list
EasyXLS.Util.List listHeaderRow = new EasyXLS.Util.List();
listHeaderRow.Add("ID");
listHeaderRow.Add("Data");
listRows.Add(listHeaderRow);
// Populate the list
EasyXLS.Util.List listRow1 = new EasyXLS.Util.List();
EasyXLS.Util.List listRow2 = new EasyXLS.Util.List();
listRow1.Add("1");
listRow1.Add("Data 1");
listRow2.Add("2");
listRow2.Add("Data 2");
listRows.Add(listRow1);
listRows.Add(listRow2);
// Create an instance of the class used to format the cells
EasyXLS.ExcelAutoFormat xlsAutoFormat = new ExcelAutoFormat();
xlsAutoFormat.InitAs(Styles.AUTOFORMAT_EASYXLS1);
// Export list to Excel file
workbook.easy_WriteXLSFile_FromList("C:\\Samples\\List to Excel.xls",
listRows, xlsAutoFormat, "List");
|
|
// Create an instance of the class that exports Excel files
ExcelDocument workbook = new ExcelDocument();
// Create the list used to store the data
EasyXLS.Util.List listRows = new EasyXLS.Util.List();
// Add the header row to the list
EasyXLS.Util.List listHeaderRow = new EasyXLS.Util.List();
listHeaderRow.addElement("ID");
listHeaderRow.addElement("Data");
listRows.addElement(listHeaderRow);
// Populate the list
EasyXLS.Util.List listRow1 = new EasyXLS.Util.List();
EasyXLS.Util.List listRow2 = new EasyXLS.Util.List();
listRow1.addElement("1");
listRow1.addElement("Data 1");
listRow2.addElement("2");
listRow2.addElement("Data 2");
listRows.addElement(listRow1);
listRows.addElement(listRow2);
// Create an instance of the class used to format the cells
EasyXLS.ExcelAutoFormat xlsAutoFormat = new ExcelAutoFormat();
xlsAutoFormat.InitAs(Styles.AUTOFORMAT_EASYXLS1);
// Export list to Excel file
workbook.easy_WriteXLSFile_FromList("C:\\Samples\\List to Excel.xls",
listRows, xlsAutoFormat, "List");
|
|
// Connect to the database
$db_conn = mssql_connect("(local)","sa","")
or die( "<strong>ERROR: Connection to the SQL Server failed</strong>" );
mssql_select_db( "northwind", $db_conn )
or die( "<strong>ERROR: Selecting database failed</strong>" );
// Query the database
$query_result = mssql_query("SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) +
'/' + CAST(Day(ord.OrderDate) AS varchar) + '/' +
CAST(year(ord.OrderDate) AS varchar) AS 'Order Date',
P.ProductName AS 'Product Name', O.UnitPrice AS Price,
CAST(O.Quantity AS varchar) AS Quantity,
O.UnitPrice * O. Quantity AS Value
FROM Orders AS ord, [Order Details] AS O, Products AS P
WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID", $db_conn)
or die( "<strong>ERROR: Query failed</strong>" );
// Create the list used to store the values
$lstRows = new COM("EasyXLS.Util.List");
// Add the header row to the list
$lstHeaderRow = new COM("EasyXLS.Util.List");
$lstHeaderRow->addElement("Order Date");
$lstHeaderRow->addElement("Product Name");
$lstHeaderRow->addElement("Price");
$lstHeaderRow->addElement("Quantity");
$lstHeaderRow->addElement("Value");
$lstRows->addElement($lstHeaderRow);
// Add the values from the database to the list
while ($row=mssql_fetch_array($query_result))
{
$RowList = new COM("EasyXLS.Util.List");
$RowList->addElement("" . $row["Order Date"]);
$RowList->addElement("" . $row["Product Name"]);
$RowList->addElement("" . $row["Price"]);
$RowList->addElement("" . $row["Quantity"]);
$RowList->addElement("" . $row["Value"]);
$lstRows->addElement($RowList);
}
// Create an instance of the object used to format the cells
$xlsAutoFormat = new COM("EasyXLS.ExcelAutoFormat");
$xlsAutoFormat->InitAs($AUTOFORMAT_EASYXLS1);
// Export list to Excel file
echo "Writing file: C:\Samples\List to Excel.xls";
$workbook->easy_WriteXLSFile_FromList_2("C:\Samples\List to Excel.xls",
$lstRows, $xlsAutoFormat, "List");
|
Click here to see Continuous Code Listing
|
|
|
' Connect to the database
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = _
"Provider=SQLOLEDB;Server=(local);Database=northwind;User ID=sa;Password=;"
objConn.Open
Dim sQueryString
sQueryString = "SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + '/' + " & _
"CAST(Day(ord.OrderDate) AS varchar) + '/' + " & _
"CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', " & _
"P.ProductName AS 'Product Name', O.UnitPrice AS Price, " & _
"CAST(O.Quantity AS varchar) AS Quantity, " & _
"O.UnitPrice * O. Quantity AS Value " & _
"FROM Orders AS ord, [Order Details] AS O, Products AS P " & _
"WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID"
' Create the record set object
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open sQueryString, objConn
' Create the list used to store the values
Dim lstRows
Set lstRows = CreateObject("EasyXLS.Util.List")
' Add the header row to the list
Dim lstHeaderRow
Set lstHeaderRow = Server.CreateObject("EasyXLS.Util.List")
lstHeaderRow.addElement("Order Date")
lstHeaderRow.addElement("Product Name")
lstHeaderRow.addElement("Price")
lstHeaderRow.addElement("Quantity")
lstHeaderRow.addElement("Value")
lstRows.addElement(lstHeaderRow)
' Add the values from the database to the list
Do Until objRS.EOF = True
set RowList = Server.CreateObject("EasyXLS.Util.List")
RowList.addElement("" & objRS("Order Date"))
RowList.addElement("" & objRS("Product Name"))
RowList.addElement("" & objRS("Price"))
RowList.addElement("" & objRS("Quantity"))
RowList.addElement("" & objRS("Value"))
lstRows.addElement(RowList)
' Move to the next record
objRS.MoveNext
Loop
' Create an instance of the object used to format the cells
Dim xlsAutoFormat
set xlsAutoFormat = Server.CreateObject("EasyXLS.ExcelAutoFormat")
xlsAutoFormat.InitAs(AUTOFORMAT_EASYXLS1)
' Export list to Excel file
response.write("Writing file: C:\Samples\List to Excel.xls")
workbook.easy_WriteXLSFile_FromList_2 "C:\Samples\List to Excel.xls", _
lstRows, xlsAutoFormat, "List"
|
Click here to see Continuous Code Listing
|
|
|
// Connect to the database
_ConnectionPtr objConn;
objConn.CreateInstance(__uuidof(Connection));
objConn->Open("driver={sql server};server=(local);Database=Northwind;UID=sa;PWD=;",
(BSTR) NULL, (BSTR) NULL, -1);
WCHAR* sQueryString = L"SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + \
'/' + CAST(Day(ord.OrderDate) AS varchar) + \
'/' + CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', \
P.ProductName AS 'Product Name', O.UnitPrice AS Price, \
CAST(O.Quantity AS varchar) AS Quantity, \
O.UnitPrice * O. Quantity AS Value \
FROM Orders AS ord, [Order Details] AS O, Products AS P \
WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID";
_variant_t sqlQueryString = sQueryString ;
// Create the record set object
_RecordsetPtr objRS = NULL;
objRS.CreateInstance(__uuidof(Recordset));
objRS->Open( sqlQueryString, _variant_t((IDispatch*)objConn,true),
adOpenStatic, adLockOptimistic, adCmdText);
// Create the list used to store the values
EasyXLS::IListPtr lstRows;
CoCreateInstance(__uuidof(EasyXLS::List), NULL, CLSCTX_ALL,
__uuidof(EasyXLS::IList), (void**) &lstRows);
// Add the header row to the list
EasyXLS::IListPtr lstHeaderRow;
CoCreateInstance(__uuidof(EasyXLS::List), NULL, CLSCTX_ALL,
__uuidof(EasyXLS::IList), (void**) &lstHeaderRow);
lstHeaderRow->addElement("Order Date");
lstHeaderRow->addElement("Product Name");
lstHeaderRow->addElement("Price");
lstHeaderRow->addElement("Quantity");
lstHeaderRow->addElement("Value");
lstRows->addElement(_variant_t((IDispatch*)lstHeaderRow,true));
VARIANT index;
index.vt=VT_I4;
FieldPtr field;
// Add the values from the database to the list
while (!(objRS->EndOfFile))
{
EasyXLS::IListPtr RowList;
CoCreateInstance(__uuidof(EasyXLS::List), NULL, CLSCTX_ALL,
__uuidof(EasyXLS::IList), (void**) &RowList) ;
VARIANT value;
for (int nIndex = 0; nIndex < 5; nIndex++)
{
index.lVal = nIndex;
objRS->Fields->get_Item(index, &field);
field->get_Value (&value);
RowList->addElement(&value);
}
lstRows->addElement(_variant_t((IDispatch*)RowList,true));
//Move to the next record
objRS->MoveNext();
}
// Create an instance of the object used to format the cells
EasyXLS::IExcelAutoFormatPtr xlsAutoFormat;
CoCreateInstance(__uuidof(EasyXLS::ExcelAutoFormat), NULL, CLSCTX_ALL,
__uuidof(EasyXLS::IExcelAutoFormat), (void**) &xlsAutoFormat) ;
xlsAutoFormat->InitAs(AUTOFORMAT_EASYXLS1);
// Export list to Excel file
printf("Writing file C:\\Samples\\List to Excel.xls.");
hr = workbook->easy_WriteXLSFile_FromList_2("C:\\Samples\\List to Excel.xls",
_variant_t((IDispatch*)lstRows,true),
_variant_t((IDispatch*)xlsAutoFormat,true),
"List");
|
Click here to see Continuous Code Listing
|
|
|
' Connect to the database
Dim objConn
Set objConn = CreateObject("ADODB.Connection")
objConn.ConnectionString = _
"Provider=SQLOLEDB;Server=(local);Database=northwind;User ID=sa;Password=;"
objConn.Open
Dim sQueryString
sQueryString = "SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + " & _
"'/' + CAST(Day(ord.OrderDate) AS varchar) + " & _
"'/' + CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', " & _
"P.ProductName AS 'Product Name', O.UnitPrice AS Price, " & _
"CAST(O.Quantity AS varchar) AS Quantity, " & _
"O.UnitPrice * O. Quantity AS Value " & _
"FROM Orders AS ord, [Order Details] AS O, Products AS P " & _
"WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID"
' Create the record set object
Dim objRS
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open sQueryString, objConn
' Create the list used to store the values
Dim lstRows
Set lstRows = CreateObject("EasyXLS.Util.List")
' Add the header row to the list
Dim lstHeaderRow
Set lstHeaderRow = CreateObject("EasyXLS.Util.List")
lstHeaderRow.addElement ("Order Date")
lstHeaderRow.addElement ("Product Name")
lstHeaderRow.addElement ("Price")
lstHeaderRow.addElement ("Quantity")
lstHeaderRow.addElement ("Value")
lstRows.addElement (lstHeaderRow)
' Add the values from the database to the list
Do Until objRS.EOF = True
Set RowList = CreateObject("EasyXLS.Util.List")
RowList.addElement ("" & objRS("Order Date"))
RowList.addElement ("" & objRS("Product Name"))
RowList.addElement ("" & objRS("Price"))
RowList.addElement ("" & objRS("Quantity"))
RowList.addElement ("" & objRS("Value"))
lstRows.addElement (RowList)
' Move to the next record
objRS.MoveNext
Loop
' Create an instance of the object used to format the cells
Dim xlsAutoFormat
Set xlsAutoFormat = CreateObject("EasyXLS.ExcelAutoFormat")
xlsAutoFormat.InitAs (Styles.AUTOFORMAT_EASYXLS1)
' Export list to Excel file
Me.Label1.Caption = Me.Label1.Caption & vbCrLf & _
"Writing file C:\Samples\List to Excel.xls."
workbook.easy_WriteXLSFile_FromList_2 "c:\Samples\List to Excel.xls", _
lstRows, xlsAutoFormat, "List"
|
Click here to see Continuous Code Listing
|
|
|
' Connect to the database
Dim objConn
Set objConn = CreateObject("ADODB.Connection")
objConn.ConnectionString = _
"Provider=SQLOLEDB;Server=(local);Database=northwind;User ID=sa;Password=;"
objConn.Open
Dim sQueryString
sQueryString = "SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + '/' + " & _
"CAST(Day(ord.OrderDate) AS varchar) + '/' + " & _
"CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', " & _
"P.ProductName AS 'Product Name', O.UnitPrice AS Price, " & _
"CAST(O.Quantity AS varchar) AS Quantity, " & _
"O.UnitPrice * O. Quantity AS Value " & _
"FROM Orders AS ord, [Order Details] AS O, Products AS P " & _
"WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID"
' Create the record set object
Dim objRS
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open sQueryString, objConn
' Create the list used to store the values
Dim lstRows
Set lstRows = CreateObject("EasyXLS.Util.List")
' Add the header row to the list
Dim lstHeaderRow
Set lstHeaderRow = CreateObject("EasyXLS.Util.List")
lstHeaderRow.addElement("Order Date")
lstHeaderRow.addElement("Product Name")
lstHeaderRow.addElement("Price")
lstHeaderRow.addElement("Quantity")
lstHeaderRow.addElement("Value")
lstRows.addElement(lstHeaderRow)
' Add the values from the database to the list
Do Until objRS.EOF = True
set RowList = CreateObject("EasyXLS.Util.List")
RowList.addElement("" & objRS("Order Date"))
RowList.addElement("" & objRS("Product Name"))
RowList.addElement("" & objRS("Price"))
RowList.addElement("" & objRS("Quantity"))
RowList.addElement("" & objRS("Value"))
lstRows.addElement(RowList)
' Move to the next record
objRS.MoveNext
Loop
' Create an instance of the object used to format the cells
Dim xlsAutoFormat
set xlsAutoFormat = CreateObject("EasyXLS.ExcelAutoFormat")
xlsAutoFormat.InitAs(AUTOFORMAT_EASYXLS1)
' Export list to Excel file
WScript.StdOut.WriteLine("Writing file C:\Samples\List to Excel.xls.")
workbook.easy_WriteXLSFile_FromList_2 "c:\Samples\List to Excel.xls", _
lstRows, xlsAutoFormat, "List"
|
Click here to see Continuous Code Listing
|
|
|
!-- Extract the values from the database -->
<cfquery name="myQuery" datasource="northwind">
SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) +
'/' + CAST(Day(ord.OrderDate) AS varchar) +
'/' + CAST(year(ord.OrderDate) AS varchar) AS 'Order_Date',
P.ProductName AS 'Product_Name', O.UnitPrice AS Price, O.Quantity,
O.UnitPrice * O. Quantity AS Value
FROM Orders AS ord, [Order Details] AS O, Products AS P
WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID
</cfquery>
<!-- Create the list used to store the values -->
<cfobject type="java" class="EasyXLS.Util.List" name="lstRows" action="CREATE">
<!-- Add the header row to the list -->
<cfobject type="java" class="EasyXLS.Util.List" name="lstHeaderRow" action="CREATE">
<cfset lstHeaderRow.addElement("Order Date")>
<cfset lstHeaderRow.addElement("Product Name")>
<cfset lstHeaderRow.addElement("Price")>
<cfset lstHeaderRow.addElement("Quantity")>
<cfset lstHeaderRow.addElement("Value")>
<cfset lstRows.addElement(lstHeaderRow)>
<!-- Add the values from the database to the list -->
<cfloop query="myQuery" >
<cfobject type="java" class="EasyXLS.Util.List" name="RowList" action="CREATE">
<cfset RowList.addElement(#Order_Date#)>
<cfset RowList.addElement(#Product_Name#)>
<cfset RowList.addElement(#Price#)>
<cfset RowList.addElement(#Quantity#)>
<cfset RowList.addElement(#Value#)>
<cfset lstRows.addElement(RowList)>
</cfloop>
<!-- Create an instance of the object used to format the cells -->
<cfobject type="java" class="EasyXLS.ExcelAutoFormat"
name="xlsAutoFormat" action="CREATE">
<cfset xlsAutoFormat.InitAs(Styles.AUTOFORMAT_EASYXLS1)>
<!-- Export list to Excel file -->
Writing file C:\Samples\List to Excel.xls
<cfset ret = workbook.easy_WriteXLSFile_FromList("C:\Samples\List to Excel.xls",
lstRows, xlsAutoFormat, "List")>
|
Click here to see Continuous Code Listing
|
|
|
|
|
The screen shot below provides an example of an exported Excel file from a list.
Formatting cells
EasyXLS™ enables you to format cells, rows and columns in order to set the fonts and colors.
|
Export List to XLSX, XLSB, XLSM and XLS files
This code sample shows how to export a list to an XLS file. In a similar way, you can export a List to XLSX file using ExcelDocument.easy_WriteXLSXFile_FromList method or export a List to XLSB file using ExcelDocument.easy_WriteXLSBFile_FromList method.
|
Export List to Excel file with multiple sheets
For an Excel file having multiple sheets, the List can be inserted using ExcelWorksheet.easy_insertList method and the Excel file must be exported using ExcelDocument.easy_WriteXLSFile method.
|
Getting started with EasyXLS Excel library
To download the trial version of EasyXLS Excel Library, press the below button:

|
If you already own a license key, you may login and download EasyXLS from your account.
|
|
Available for: Professional, Excel Writer, Express Excel Writer
|
|
|