Skip to content

support for xlsx #1666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react-bootstrap-table2-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"react-dom": "^16.3.0"
},
"dependencies": {
"file-saver": "2.0.2"
"file-saver": "2.0.2",
"xlsx": "0.17.0"
}
}
114 changes: 68 additions & 46 deletions packages/react-bootstrap-table2-toolkit/src/csv/exporter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint no-unneeded-ternary: 0 */
import FileSaver from 'file-saver';
import FileSaver from "file-saver";
import XLSX from "xlsx";

export const getMetaInfo = columns =>
export const getMetaInfo = (columns) =>
columns
.map(column => ({
.map((column) => ({
field: column.dataField,
type: column.csvType || String,
formatter: column.csvFormatter,
Expand All @@ -14,69 +15,90 @@ export const getMetaInfo = columns =>
rowSpan: Number(column.rowSpan) || 1,
colSpan: Number(column.colSpan) || 1,
footer: column.footer,
footerFormatter: column.footerFormatter
footerFormatter: column.footerFormatter,
}))
.filter(_ => _.export);
.filter((_) => _.export);

export const transform = (
data,
meta,
columns,
_,
{
separator,
ignoreHeader,
ignoreFooter
}
{ separator, ignoreHeader, ignoreFooter }
) => {
const visibleColumns = meta.filter(m => m.export);
let content = '';
const visibleColumns = meta.filter((m) => m.export);
let content = "";
// extract csv header
if (!ignoreHeader) {
content += visibleColumns.map(m => `"${m.header}"`).join(separator);
content += '\n';
content += visibleColumns.map((m) => `"${m.header}"`).join(separator);
content += "\n";
}
// extract csv body
if (data.length === 0) return content;
content += data
.map((row, rowIndex) =>
visibleColumns.map((m) => {
let cellContent = _.get(row, m.field);
if (m.formatter) {
cellContent = m.formatter(cellContent, row, rowIndex, m.formatExtraData);
}
if (m.type === String) {
return `"${`${cellContent}`.replace(/"/g, '""')}"`;
}
return cellContent;
}).join(separator)).join('\n');
visibleColumns
.map((m) => {
let cellContent = _.get(row, m.field);
if (m.formatter) {
cellContent = m.formatter(
cellContent,
row,
rowIndex,
m.formatExtraData
);
}
if (m.type === String) {
return `"${`${cellContent}`.replace(/"/g, '""')}"`;
}
return cellContent;
})
.join(separator)
)
.join("\n");

if (!ignoreFooter) {
content += '\n';
content += visibleColumns.map((m, i) => {
if (typeof m.footer === 'function') {
const columnData = _.pluck(data, columns[i].dataField);
return `"${m.footer(columnData, columns[i], i)}"`;
} else if (m.footerFormatter) {
return `"${m.footerFormatter(columns[i], i)}"`;
}
return `"${m.footer}"`;
}).join(separator);
content += "\n";
content += visibleColumns
.map((m, i) => {
if (typeof m.footer === "function") {
const columnData = _.pluck(data, columns[i].dataField);
return `"${m.footer(columnData, columns[i], i)}"`;
} else if (m.footerFormatter) {
return `"${m.footerFormatter(columns[i], i)}"`;
}
return `"${m.footer}"`;
})
.join(separator);
}
return content;
};

export const save = (
content,
{
noAutoBOM,
fileName,
blobType
export const save = (content, { noAutoBOM, fileName, blobType }) => {
function csvJSON(csv) {
const lines = csv.split("\n");
const result = [];
const headers = lines[0].split(",");

for (let i = 1; i < lines.length; i++) {
if (!lines[i]) continue;
const obj = {};
const currentline = lines[i].split(",");

for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result;
}
) => {
FileSaver.saveAs(
new Blob([content], { type: blobType }),
fileName,
noAutoBOM
);

const data1 = csvJSON(content);

const ws = XLSX.utils.json_to_sheet(data1);
const wb = { Sheets: { data: ws }, SheetNames: ["data"] };
const excelBuffer = XLSX.write(wb, { bookType: "xlsx", type: "array" });
const data2 = new Blob([excelBuffer], { type: blobType });

FileSaver.saveAs(new Blob([data2], { type: blobType }), fileName, noAutoBOM);
};