這是上傳的excel表格。
在運行我的代碼后,根據代碼,圖像的黑色是當前的輸出。藍色的勾號表示它工作良好,但是我想實現紅色的輸出。
.table{
border:solid 1px;
//width: 50mm;
//height: 30mm;
table-layout: fixed;
border-collapse:collapse; //in table css, collapse is equivalent to cellspacing in table css and paddling is :enabled in td
padding:0mm 0mm 0mm 0mm;
}
td {
//border:solid 1px;
}
.itemname{
//border:solid 1px;
height: 10mm;
width: 48mm;
margin: 0mm;
font-family: Helvetica, sans-serif;
font-size: 3mm;
line-height: 3mm;
z-index: 1;
padding-top: 0mm;
padding-bottom: 0mm;
padding-left: 1mm;
padding-right: 1mm;
text-align: center;
vertical-align: top;
overflow: hidden;
max-width: 100%;
max-height: 100%;
}
.price{
//border: solid 1px;
height: 8mm;
width: 28mm;
margin: 0mm;
margin-right: 0mm;
font-family: Helvetica, sans-serif;
font-size: 1.5em;
font-weight: 600;
padding-top: 0mm;
padding-bottom: 0mm;
padding-left: 0mm;
padding-right: 1mm;
text-align: right;
vertical-align: top;
overflow: hidden;
max-width: 100%;
max-height: 100%;
}
.suppliers{
//border:solid 1px;
height: 5mm;
width: 15mm;
margin: 0mm;
font-family: times new roman;
font-size: 2mm;
line-height: 2mm;
z-index: 1;
padding-top: 0mm;
padding-bottom: 0mm;
padding-left: 0mm;
padding-right: 0mm;
text-align: right;
vertical-align: top;
overflow: hidden;
max-width: 100%;
max-height: 100%;
}
.pagebreak {
page-break-inside:avoid;
}
<html>
<head>
<body>
<script>
// Method to upload a valid excel file
function upload() {
var files = document.getElementById('file_upload').files;
if(files.length==0){
alert("Please choose any file...");
return;
}
var filename = files[0].name;
var extension = filename.substring(filename.lastIndexOf(".")).toUpperCase();
if (extension == '.XLS' || extension == '.XLSX') {
//Here calling another method to read excel file into json
excelFileToJSON(files[0]);
}else{
alert("Please select a valid excel file.");
}
}
//Method to read excel file and convert it into JSON
function excelFileToJSON(file){
try {
var reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = function(e) {
var data = e.target.result;
var workbook = XLSX.read(data, {
type : 'binary'
});
var result = {};
var firstSheetName = workbook.SheetNames[0];
//reading only first sheet data
var jsonData = XLSX.utils.sheet_to_json(workbook.Sheets[firstSheetName]);
//displaying the json result into HTML table
displayJsonToHtmlTable(jsonData);
}
}catch(e){
console.error(e);
}
}
//Method to display the data in HTML Table
function displayJsonToHtmlTable(jsonData){
var table=document.getElementById("display_excel_data");
if(jsonData.length>0){
var htmlData=''
for(var i=0;i<jsonData.length;i++){
var row=jsonData[i];
var itemname = row["Item Name"] || '';
var price = row["Price"] || '';
var suppliers = row["Suppliers"] || '';
htmlData+='<table class="table">'
+'<tr><td class="itemname" colspan=3>'+itemname+'</td></tr>'
+'<td class="price">'+price+' </td></tr>'
+'<td class="price">'+suppliers+' </td></tr>'
+'</table>';
}
table.innerHTML=htmlData;
}else{
table.innerHTML='There is no data in Excel';
}
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.min.js"></script>
<h1>Upload an excel file to display in HTML Table</h1>
<!-- Input element to upload an excel file -->
<input type="file" id="file_upload" />
<button onclick="upload()">Upload</button>
<br>
<br>
<!-- table to display the excel data -->
<p style="page-break-after: always;"> </p>
<div id="display_excel_data"></div>
</body>
</head>
</html>