danfo.toExcel
Converts a DataFrame or Series to Excel file, and write file to disk or download in browser.
danfo.toExcel(data, options)
Parameters
Type
Description
Default
data
Series or DataFrame
The Series or DataFrame to write to CSV
options
object, optional
Configuration object:
{
filePath: Local file path to write the CSV file to. If not specified, the CSV will be returned as a string. Only needed in Nodejs version fileName: The name of the file to download as. Only needed in the browser environment. sheetName: Name to call the excel sheet.
}
{ filePath: "./output.xlsx", sheetName: "Sheet1" }
The toExcel function can be used to write out a DataFrame or Series to Excel (.xlsx) file. The output format will depend on the environment. In the following examples, we show you how to write/download an Excel file from Node and Browser environments.

Convert DataFrame to Excel and write to file path

Writing an Excel file to a local file path is only supported in the Nodejs environment
Node.js
1
const dfd = require("danfojs-node")
2
3
let data = {
4
Abs: [20.2, 30, 47.3],
5
Count: [34, 4, 5],
6
"country code": ["NG", "FR", "GH"],
7
};
8
9
let df = new dfd.DataFrame(data);
10
11
dfd.toExcel(df, { filePath: "testOut.xlsx"});
Copied!

Convert DataFrame to Excel and download the file in Client-side lib

You can automatically convert and download an Excel file in a browser environment, by specifying a filename and setting download to true
1
let data = {
2
Abs: [20.2, 30, 47.3],
3
Count: [34, 4, 5],
4
"country code": ["NG", "FR", "GH"],
5
};
6
7
let df = new DataFrame(data);
8
9
dfd.toExcel(df, { fileName: "testOut.xlsx", download: true});
Copied!