Blog Post

How to build a React.js Excel (XLS/XLSX) viewer

Illustration: How to build a React.js Excel (XLS/XLSX) viewer
Information

This article was first published in November 2023 and was updated in August 2024.

In this blog post, learn how to build a React Excel viewer using the Nutrient Web SDK. You’ll open and view XLS and XLSX files directly in your web browser using client-side processing (no server required).

The image below shows what you’ll be building.

resulting image

You can check out the demo to see it in action.

Opening and rendering Office documents in the browser

Nutrient Web SDK brings support for Word, Excel, and PowerPoint formats to your application, without you or your users needing any MS Office software, MS Office licenses, or third-party open source software. The technology works by converting an Office document to PDF directly in the browser, and the document is then rendered in our JavaScript viewer.

Unlocking more capabilities with Office-to-PDF conversion

​​By converting an Office document to PDF using client-side JavaScript, you have the option to support a rich array of additional Office document functionality, such as:

  • Text editing — Edit text directly in the displayed Office document.

  • Page manipulation — Organize documents by adding, removing, or rearranging pages.

  • Annotations — Boost collaboration by adding text highlights, comments, or stamps.

  • Adding signatures — Draw, type, or upload a signature directly to an Office document.

Explore Demo

Requirements to get started

To get started, you’ll need:

  • The latest version of Node.js.

  • A package manager compatible with npm. This post contains usage examples for Yarn and the npm client (installed with Node.js by default).

Setting up a new React project with Vite

  1. To get started, create a new React project using Vite:

# Using Yarn
yarn create vite pspdfkit-react-example --template react

# Using npm
npm create vite@latest pspdfkit-react-example -- --template react
  1. Navigate to your project directory:

cd pspdfkit-react-example

Adding Nutrient to Your Project

  1. First, install Nutrient as a dependency:

# Using Yarn
yarn add pspdfkit

# Using npm
npm install pspdfkit
  1. Next, copy the Nutrient Web library assets to the public directory:

cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib

This ensures Nutrient can access its assets from the public/pspdfkit-lib directory.

Information

Make sure your development server supports the application/wasm MIME type. This can be crucial for WebAssembly to function correctly.

Displaying a PDF in your React app

  1. Add an Excel (XLS, XLSX) document you want to display to the public directory. You can use our demo document as an example.

  2. Create a new component to handle PDF rendering. In the src/components/ directory, create a file named PdfViewerComponent.jsx with the following content:

import { useEffect, useRef } from 'react';

export default function PdfViewerComponent(props) {
	const containerRef = useRef(null);

	useEffect(() => {
		const container = containerRef.current;
		let PSPDFKit, instance;

		(async function () {
			PSPDFKit = await import('pspdfkit');

			PSPDFKit.unload(container);

			instance = await PSPDFKit.load({
				container,
				document: props.document,
				baseUrl: `${window.location.protocol}//${
					window.location.host
				}/${import.meta.env.BASE_URL}`,
			});
		})();

		return () => PSPDFKit && PSPDFKit.unload(container);
	}, []);

	return (
		<div
			ref={containerRef}
			style={{ width: '100%', height: '100vh' }}
		/>
	);
}
  1. Integrate this component into your app by updating the src/App.jsx file:

import PdfViewerComponent from './components/PdfViewerComponent';

function App() {
	return (
		<div className="App" style={{ width: '100vw' }}>
			<div className="PDF-viewer">
				<PdfViewerComponent document={'chart.xlsx'} />
			</div>
		</div>
	);
}

export default App;

Ensure your project structure looks like this:

pspdfkit-react-example/
├── public/
│   ├── pspdfkit-lib/
│   └── chart.xlsx
├── src/
│   ├── components/
│   │   └── PdfViewerComponent.jsx
│   └── App.jsx
├── package.json
└── yarn.lock
  1. Start your development server:

# Using Yarn
yarn dev

# Using npm
npm run dev

Once the server is running, you can view and interact with your PDF directly in the browser.

A note about fonts

In client-side web applications for Microsoft Office-to-PDF conversion, Nutrient addresses font licensing constraints through font substitutions, typically replacing unavailable fonts with their equivalents — like Arial with Noto. For precise font matching, you can provide your own fonts, embed them into source files, or designate paths to your .ttf fonts for custom solutions.

Adding even more capabilities

Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular React.js guides:

Conclusion

In this blog post, you learned how to build an Excel viewer using React.js with the Nutrient SDK.

If you’re looking for a way to render Office documents in your web application, then Nutrient Web SDK is a great option. It’s a powerful and flexible library that can help you provide your users with a seamless and enjoyable experience.

To get started, you can either:

  • Start your free trial to test out the library and see how it works in your application.

  • Launch our demo to see the viewer in action.

FAQ

Here are a few frequently asked questions about building a React Excel viewer.

How can I integrate Nutrient into a React.js project?

To integrate Nutrient, install the pspdfkit package with npm or Yarn, copy the library assets to your public directory, and create a React component to initialize and configure Nutrient.

How do I display Excel (XLS/XLSX) files using Nutrient in React?

Import the Nutrient library in your React component, provide the path to your Excel document, and use PSPDFKit.load() to render the document.

Can I customize the Nutrient viewer in my React application?

Yes, you can customize the Nutrient viewer with options for initial view state, annotations, text editing, and additional functionalities.

Where can I find a demo of the React.js Excel viewer with Nutrient?

You can check out a demo of the React.js Excel viewer with Nutrient by visiting the Nutrient demo page.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Related products
Share post
Free trial Ready to get started?
Free trial