/// <summary> /// 导出excel /// </summary> /// <param name="list"></param> private void ExportExcel(IList<CV_SaaS_POM_ProductionOrder> list) { try { SaveFileDialog Dialog = new SaveFileDialog(); Dialog.Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls"; Dialog.FileName = "SAP生产工单_" + DateTime.Now.ToString("MMdd") + ".xlsx"; if (Dialog.ShowDialog() == DialogResult.OK) { Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = workbook.Worksheets[0]; //工作表 Cells cells = sheet.Cells;//单元格 #region 文字样式 Style style = new Style(); style.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style.VerticalAlignment = TextAlignmentType.Center; style.Font.Name = "宋体";//文字字体 style.Font.Size = 13; // Style style1 = new Style(); style1.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style1.VerticalAlignment = TextAlignmentType.Center; style1.Font.Name = "宋体";//文字字体 style1.Font.Size = 13; style1.Custom = "MM/dd"; // Style style2 = new Style(); style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中 style2.VerticalAlignment = TextAlignmentType.Center; style2.Font.Name = "宋体";//文字字体 style2.Font.Size = 13; style2.Custom = "yyyy/mm/dd"; #endregion //生成行2 列名行 cells[0, 0].PutValue("SAP工单号"); cells[0, 0].SetStyle(style); cells[0, 1].PutValue("物料编码"); cells[0, 1].SetStyle(style); cells[0, 2].PutValue("物料描述"); cells[0, 2].SetStyle(style); cells[0, 3].PutValue("BOM版本"); cells[0, 3].SetStyle(style); cells[0, 4].PutValue("工单组件"); cells[0, 4].SetStyle(style); cells[0, 5].PutValue("工单类型"); cells[0, 5].SetStyle(style); cells[0, 6].PutValue("工艺路径"); cells[0, 6].SetStyle(style); cells[0, 7].PutValue("工单数量"); cells[0, 7].SetStyle(style); cells[0, 8].PutValue("计划开始时间"); cells[0, 8].SetStyle(style); cells[0, 9].PutValue("计划完成时间(交期)"); cells[0, 9].SetStyle(style); //根据当前日期生成一年的日期列 int cellIndex = 9; DateTime today = Convert.ToDateTime(DateTime.Now.ToString(@"yyyy-MM-dd")); DateTime endday = today.AddMonths(2); for (; today < endday; today = today.AddDays(1)) { cellIndex++; cells[0, cellIndex].PutValue(today); cells[0, cellIndex].SetStyle(style1);//定义后续两个月日期的格式 } //设置下拉列表框格式 ValidationCollection validations = sheet.Validations; Validation validation = validations[validations.Add()]; validation.Type = Aspose.Cells.ValidationType.List; validation.Operator = OperatorType.None; validation.InCellDropDown = true; validation.Formula1 = GetOrderFamilyName();//逗号分隔字符串 validation.ShowError = true; validation.AlertStyle = ValidationAlertType.Stop; validation.ErrorTitle = "Error"; validation.ErrorMessage = "请选择工单组件"; CellArea area; area.StartRow = 1; area.EndRow = 9999; area.StartColumn = 4; area.EndColumn = 4; validation.AreaList.Add(area); //写入工单数据 CV_SaaS_POM_ProductionOrder order = null; for (int i = 0; i < list.Count; i++) { order = list[i]; cells[i + 1, 0].PutValue(order.ProductionOrderNO); cells[i + 1, 0].SetStyle(style); cells[i + 1, 1].PutValue(order.DefID); cells[i + 1, 1].SetStyle(style); cells[i + 1, 2].PutValue(order.DefName); cells[i + 1, 2].SetStyle(style); cells[i + 1, 3].PutValue(""); cells[i + 1, 3].SetStyle(style); cells[i + 1, 4].PutValue(order.OrderGroup); cells[i + 1, 4].SetStyle(style); cells[i + 1, 5].PutValue(order.OderType); cells[i + 1, 5].SetStyle(style); cells[i + 1, 6].PutValue(order.PPRCode); cells[i + 1, 6].SetStyle(style); cells[i + 1, 7].PutValue(order.Quantity - order.AssignedQuantity); cells[i + 1, 7].SetStyle(style); cells[i + 1, 8].PutValue(order.StartTime); cells[i + 1, 8].SetStyle(style2); cells[i + 1, 9].PutValue(order.EndTime); cells[i + 1, 9].SetStyle(style2); } int columnCount = cells.MaxColumn; //获取表页的最大列数 int rowCount = cells.MaxRow; //获取表页的最大行数 for (int col = 0; col < columnCount; col++) { sheet.AutoFitColumn(col, 0, rowCount); } for (int col = 0; col < columnCount; col++) { cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30); } workbook.Save(Dialog.FileName); MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception exception) { SaMessageBox.ShowError(exception); } }