Here's a simple HTML page that can read and display Excel file content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel File Reader</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.upload-area {
border: 2px dashed #ccc;
border-radius: 10px;
padding: 40px;
text-align: center;
margin-bottom: 20px;
cursor: pointer;
transition: border-color 0.3s;
}
.upload-area:hover {
border-color: #007bff;
}
.upload-area.dragover {
border-color: #007bff;
background-color: #f8f9fa;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
.sheet-selector {
margin: 20px 0;
}
select {
padding: 5px 10px;
font-size: 16px;
}
.error {
color: red;
margin: 10px 0;
}
</style>
</head>
<body>
<h1>Excel File Reader</h1>
<div class="upload-area" id="uploadArea">
<p>Click here to select an Excel file or drag and drop it</p>
<input type="file" id="fileInput" accept=".xlsx,.xls" style="display: none;">
</div>
<div class="sheet-selector" id="sheetSelector" style="display: none;">
<label for="sheetSelect">Select Sheet: </label>
<select id="sheetSelect"></select>
</div>
<div id="error" class="error"></div>
<div id="tableContainer"></div>
<script>
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const sheetSelector = document.getElementById('sheetSelector');
const sheetSelect = document.getElementById('sheetSelect');
const tableContainer = document.getElementById('tableContainer');
const errorDiv = document.getElementById('error');
let workbook = null;
// Click to upload
uploadArea.addEventListener('click', () => {
fileInput.click();
});
// Drag and drop functionality
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const files = e.dataTransfer.files;
if (files.length > 0) {
handleFile(files[0]);
}
});
// File input change
fileInput.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
handleFile(e.target.files[0]);
}
});
// Sheet selection change
sheetSelect.addEventListener('change', () => {
displaySheet(sheetSelect.value);
});
function handleFile(file) {
errorDiv.textContent = '';
if (!file.name.match(/\.(xlsx|xls)$/)) {
errorDiv.textContent = 'Please select a valid Excel file (.xlsx or .xls)';
return;
}
const reader = new FileReader();
reader.onload = function(e) {
try {
const data = new Uint8Array(e.target.result);
workbook = XLSX.read(data, { type: 'array' });
// Populate sheet selector
sheetSelect.innerHTML = '';
workbook.SheetNames.forEach(name => {
const option = document.createElement('option');
option.value = name;
option.textContent = name;
sheetSelect.appendChild(option);
});
sheetSelector.style.display = 'block';
// Display first sheet by default
if (workbook.SheetNames.length > 0) {
displaySheet(workbook.SheetNames[0]);
}
} catch (error) {
errorDiv.textContent = 'Error reading file: ' + error.message;
}
};
reader.readAsArrayBuffer(file);
}
function displaySheet(sheetName) {
if (!workbook || !workbook.Sheets[sheetName]) {
return;
}
const worksheet = workbook.Sheets[sheetName];
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
if (jsonData.length === 0) {
tableContainer.innerHTML = '<p>This sheet is empty.</p>';
return;
}
// Create table
let tableHTML = '<table>';
jsonData.forEach((row, rowIndex) => {
tableHTML += '<tr>';
const maxCols = Math.max(...jsonData.map(r => r.length));
for (let colIndex = 0; colIndex < maxCols; colIndex++) {
const cellValue = row[colIndex] || '';
const tag = rowIndex === 0 ? 'th' : 'td';
tableHTML += `<${tag}>${cellValue}</${tag}>`;
}
tableHTML += '</tr>';
});
tableHTML += '</table>';
tableContainer.innerHTML = tableHTML;
}
</script>
</body>
</html>
This HTML page provides a complete Excel file reader with the following features:
Key functionality:
- Click or drag-and-drop file upload
- Support for both .xlsx and .xls files
- Sheet selection dropdown for multi-sheet workbooks
- Displays data in a formatted HTML table
- Error handling for invalid files
How it works:
- Uses the SheetJS library (xlsx) to parse Excel files
- Reads the file as an ArrayBuffer
- Converts worksheet data to JSON format
- Dynamically generates an HTML table to display the content
Usage:
- Save the code as an HTML file
- Open it in a web browser
- Click the upload area or drag an Excel file onto it
- Select which sheet to view (if multiple sheets exist)
- The content will display in a table format
The page handles common Excel file formats and provides a clean, responsive interface for viewing spreadsheet data in the browser.