lets say i have an array of 5 values, but when iterate each row i end up only with 4 rows leave alone the headers, meant the data rows are just 4 . am i doing anything wrong here? and also on pc i can save file as xlsl which opens well but on mobile its saved as .xlsx.xls which warning pops up due to extension. let me copy the code
<button class="btn btn-success mb-1" @click.prevent="ExportExcel(Projects, Headers)">Export <i class="fas fa-plus fw"></i> </button>
`<script/>
methods:(){
ToLocaleDateString(value) {
var date = new Date(value);
var customdate=date.toLocaleDateString('en-GB', {day: 'numeric', month: 'numeric', year: 'numeric'}).replace(/ /g, '/');
return customdate;
},
ToLocaleDateString2(value) {
var date = new Date(value);
var customdate=date.toLocaleDateString('en-GB', {day: 'numeric', month: 'numeric', year: 'numeric'});
return customdate;
},
sortedArray(projects){
return projects.sort((a, b) => a.id - b.id );
},
phoneformat(phone){
var prefix = phone.substring(0,4);
var suffix = phone.substring(4);
return prefix +' '+ suffix
},
// ExportExcel(projects, headers) {
ExportExcel(projects, headers) {
var excel = $JExcel.new("Calibri light 10 #333333"); // Default font
// excel.set is the main function to generate content:
// We can use parameter notation excel.set(sheetValue,columnValue,rowValue,cellValue,styleValue)
// Or object notation excel.set({sheet:sheetValue,column:columnValue,row:rowValue,value:cellValue,style:styleValue })
// null or 0 are used as default values for undefined entries
excel.set( {sheet:0,value:"This is Sheet 1" } );
excel.addSheet("Sheet 2");
excel.set(0,8,1,15);
excel.set(0,8,2,13);
excel.set(0,7,3,"15+13");
excel.set(0,8,3,"=I2+I3");
var evenRow=excel.addStyle (
{
border: "none,none,none,thin #333333"
}); // Borders are LEFT,RIGHT,TOP,BOTTOM. Check $JExcel.borderStyles for a list of valid border styles
var oddRow=excel.addStyle ( { // Style for odd ROWS
fill: "#ECECEC" , // Background color, plain #RRGGBB, there is a helper $JExcel.rgbToHex(r,g,b)
border: "none,none,none,thin #333333"
});
for (var i=1;i<projects.length;i++) excel.set({row:i,style: i%2==0 ? evenRow: oddRow }); // Set style for the first 50 rows
// excel.set({row:3,value: 30 }); // We want ROW 3 to be EXTRA TALL
var headers= headers; // This array holds the HEADERS text
var formatHeader=excel.addStyle ( { // Format for headers
border: "none,none,none,thin #333333", // Border for header
font: "Calibri 12 #0000AA B"}); // Font for headers
for (var i=0;i<headers.length;i++){ // Loop all the haders
excel.set(0,i,0,headers[i],formatHeader); // Set CELL with header text, using header format
excel.set(0,i,undefined,"auto"); // Set COLUMN width to auto (according to the standard this is only valid for numeric columns)
}
// Now let's write some data
var today = (new Date());
var initDate = new Date(2000, 0, 1);
var endDate = new Date(2016, 0, 1);
var dateStyle = excel.addStyle ( { // Format for date cells
align: "R", // aligned to the RIGHT
format: "yyyy.mm.dd hh:mm:ss", // using DATE mask, Check $JExcel.formats for built-in formats or provide your own
font: "#00AA00"});
var sortedprojects = this.sortedArray(projects)
for (var i = 1; i <projects.length; i++){
var idx = projects[i].id;
var client = projects[i].owner? projects[i].owner.name:'Not Set';
var email = projects[i].owner? projects[i].owner.email_address:'Not Set';
var phone = projects[i].owner? this.phoneformat(projects[i].owner.phone_number):'Not Set';
var name = projects[i].name? projects[i].name:'Not Set';
var status = projects[i].status? projects[i].status:'Not Set';
var amount = projects[i].amount? projects[i].amount: 0;
var managementfee = projects[i].management_fee? projects[i].management_fee: 0;
var completion = projects[i].completion? projects[i].completion: 0;
var proposedstart = projects[i].proposed_start? this.ToLocaleDateString(projects[i].proposed_start) : 'Not Set';
var proposedend = projects[i].proposed_end? this.ToLocaleDateString(projects[i].proposed_end) : 'Not Set';
var actualstart = projects[i].actual_start? this.ToLocaleDateString(projects[i].actual_start) : 'Not Set';
var actualend = projects[i].actual_end? this.ToLocaleDateString(projects[i].actual_end) : 'Not Set';
var resumedate = projects[i].resume_date? this.ToLocaleDateString(projects[i].resume_date) : 'Not Set';
var terminationdate = projects[i].termination_date? this.ToLocaleDateString(projects[i].termination_date) : 'Not Set';
var onholddate = projects[i].on_hold_date? this.ToLocaleDateString(projects[i].on_hold_date) : 'Not Set';
var landmark = projects[i].nearest_landmark? projects[i].nearest_landmark:'Not Set';
var county = projects[i].ward? projects[i].ward.constituency.county.name:'Not Set';
var subcounty = projects[i].ward? projects[i].ward.constituency.name:'Not Set';
var ward = projects[i].ward? projects[i].ward.name:'Not Set';
//data entry
excel.set(0,0,i, i);
excel.set(0,1,i, client);
excel.set(0,2,i, email);
excel.set(0,3,i, phone);
excel.set(0,4,i, name);
excel.set(0,5,i, status);
excel.set(0,6,i, 'Kshs.' +amount);
excel.set(0,7,i, managementfee +''+'%');
excel.set(0,8,i, completion +''+'%');
excel.set(0,9,i, proposedstart);
excel.set(0,10,i, proposedend);
excel.set(0,11,i, actualstart);
excel.set(0,12,i, actualend);
excel.set(0,13,i, resumedate);
excel.set(0,14,i, terminationdate);
excel.set(0,15,i, onholddate);
excel.set(0,16,i, county);
excel.set(0,17,i, subcounty);
excel.set(0,18,i, ward);
}
// const mergedCellStyle = excel.addStyle( {align:"C", font: "Calibri 12 #0000AA B"});
// excel.set(0, 0, 50, "This cells are merged!", mergedCellStyle, 5); // This cells are merged using colspan
excel.set(0,0,undefined,3, excel.addStyle( {align:"L"})); // Set COLUMN 1 to 30 chars width
excel.set(0,1,undefined,20, excel.addStyle( {align:"L"})); // Set COLUMN 1 to 30 chars width
excel.set(0,2,undefined,30, excel.addStyle( {align:"L"})); // Set COLUMN 1 to 30 chars width
excel.set(0,3,undefined,20, excel.addStyle( {align:"L"})); // Set COLUMN 1 to 30 chars width
excel.set(0,4,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,5,undefined,20, excel.addStyle( {align:"L"}));
excel.set(0,6,undefined,20, excel.addStyle( {align:"L"}));
excel.set(0,7,undefined,20, excel.addStyle( {align:"L"}));
excel.set(0,8,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,9,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,10,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,11,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,12,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,13,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,14,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,15,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,16,undefined,30, excel.addStyle( {align:"L"}));
excel.set(0,17,undefined,30, excel.addStyle( {align:"L"})); // Set COLUMN 1 to 30 chars width
// excel.set(0,3,undefined,30); // Set COLUMN 3 to 20 chars width
// excel.set(0,4,undefined,20, excel.addStyle( {align:"R"})); // Align column 4 to the right
// excel.set(0,1,3,undefined,excel.addStyle( {align:"L T"})); // Align cell 1-3 to LEFT-TOP
// excel.set(0,2,3,undefined,excel.addStyle( {align:"C C"})); // Align cell 2-3 to CENTER-CENTER
// excel.set(0,3,3,undefined,excel.addStyle( {align:"R B"})); // Align cell 3-3 to RIGHT-BOTTOM
excel.generate(today +'-'+"ProjectReport.xlsx");
},