问题描述
我正在尝试按照客户预定义的模板以 excel 格式导出一些数据,但我无法导出模型中的数据。
这是客户发来的模型
这就是我所做的,但我无法再提高了。
我为任何可以帮助我的人创建了一个关于 stackblitz 的示例。
https://stackblitz.com/edit/angular-ivy-wtbs3z?file=src%2Fapp%2Fapp.module.ts
DATA_EXEMPLE.TS
export const DATA_EXAMPLE = [
{
group_name: 'GROUP 1',product_family: 'Product family X',ibx: 'IBX 1',products: [
{
quantity: 5,product_name: 'Product A',elements: [
{
element_name: 'Element M',description: 'Some description',quantity: 2,sales_quantity: 0
},{
element_name: 'Element N',quantity: 1,sales_quantity: 0
}
]
},{
quantity: 2,product_name: 'Product B',elements: [
{
element_name: 'Element J',sales_quantity: 2
},{
element_name: 'Element K',{
element_name: 'Element L',sales_quantity: 0
}
]
}
]
},{
group_name: 'GROUP 2',product_family: 'Product family Y',ibx: 'IBX 2',product_name: 'Product AZ',elements: [
{
element_name: 'Element A',{
element_name: 'Element B',]
}
]
SERVICE.TS
@Injectable({ providedIn: 'root' })
export class ExcelJSService {
private headers = [
'#','Group Name','Product Family','IBX','POF Qty','POF','POE','Attributes','POE Qty','Sls Qty',]
constructor() {}
public exportToExcel(checklist: ChecklistDTO): void {
// const { product_groups } = checklist
// Here I’m trying to calculate how many lines I need to merge into the group lines
const group_merge_cells_data: GroupMergeCellDTO[] = []
DATA_EXAMPLE.forEach((group,i) => {
const { products } = group
let element_qtd = 0
const product_merge_cells_data: ProductMergeCellDTO[] = []
products.forEach((product,j) => {
const { elements } = product
element_qtd += elements.length
product_merge_cells_data.push({
prod_index: j,prod_merge_qtd: elements.length,})
})
group_merge_cells_data.push({
group_index: i,group_merge_qtd: element_qtd - 1
})
group_merge_cells_data[i].products = product_merge_cells_data
})
// create a workbook with a worksheet
const workbook = new Workbook();
workbook.creator="Our Team"
workbook.created = new Date(Date.now());
const worksheet = workbook.addWorksheet('checklist');
// Adding Header Row
worksheet.addRow(this.headers).eachCell((cell,number) => {
cell.fill = {
type: 'pattern',pattern: 'solid',fgColor: { argb: '4167B8' },bgColor: { argb: '' }
}
cell.font = {
bold: true,color: { argb: 'FFFFFF' },size: 12
}
})
// Adding Group Data
DATA_EXAMPLE.forEach((group,i) => {
const { products } = group
let group_last_cells: Cell[] = []
let group_row = worksheet.addRow([
i + 1,group.group_name,group.product_family,group.ibx,]);
group_row.eachCell((cell,colNumber) => {
worksheet.mergeCells(
+cell.fullAddress.row,+cell.fullAddress.col,+cell.fullAddress.row + group_merge_cells_data[i].group_merge_qtd,+cell.fullAddress.col
)
if (colNumber === 4) {
group_last_cells.push(cell)
}
})
group_last_cells.forEach(cell => {
products.forEach((product,j) => {
const { elements } = product
let quantity_cell = worksheet.getCell(`E${cell.fullAddress.row + j}`)
quantity_cell.value = product.quantity
let prod_name_cell = worksheet.getCell(`F${cell.fullAddress.row + j}`)
prod_name_cell.value = product.product_name
elements.forEach((element,k) => {
console.log(`row: ${cell.fullAddress.row} - j: ${j} - k: ${k}`)
let element_name_cell = worksheet.getCell(`G${cell.fullAddress.row + 1}`)
element_name_cell.value = element.element_name
let attribute_cell = worksheet.getCell(`H${cell.fullAddress.row + 1}`)
attribute_cell.value = element.description
let quantity_cell = worksheet.getCell(`I${cell.fullAddress.row + 1}`)
quantity_cell.value = element.quantity
let sales_quantity_cell = worksheet.getCell(`J${cell.fullAddress.row + 1}`)
sales_quantity_cell.value = element.sales_quantity
})
})
})
});
// Generate & Save Excel File
workbook.xlsx.writeBuffer().then((data) => {
let blob = new Blob([data],{ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
fs.saveAs(blob,'test' + '.xlsx');
})
}
}
StackBlitz 示例: https://stackblitz.com/edit/angular-ivy-wtbs3z?file=src%2Fapp%2Fapp.module.ts
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)