I have a FileData which is a base64 string and FileType such as as
FileData = "UEsDBBQABgAIAAAAI"
FileType = "application/msexcel"
I want to render this Excel (base64 string) into a react component. I found react-doc-viewer is a good package.
I have written a function that first converts the base64 into a blob and creates a URL that must be given to the DOCViewer component of react-doc-viewer.
function renderPOXLSExcelFile(fileData: any, fileType: any) {
// Convert base64 file data back to a Blob
const binary = atob(fileData.split(',')[1]);
const array = [];
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
const blob = new Blob([new Uint8Array(array)], { type: fileType });
// Create URL from blob
const url = URL.createObjectURL(blob);
const docs = [{ uri: url, fileType: fileType }];
return (
<>
<DocViewer documents={docs} config={{header: {disableHeader: false}}}
pluginRenderers={DocViewerRenderers />
</>
);
}
The react-doc-viewer is supposed to take uri (URL) as input and show me the excel right. If there is any other approach to render the Excel file from the blob, please let me know.
I have tried parsing the Excel to JSON and applying styles but it looks really bad on UI. Please help me out in finding an approach to this issue.
TIA