public string GeneratePreallocatingReport(string container) { var cartonDetails = _context.RegularCartonDetails .Include(x => x.POSummary) .Where(x => x.POSummary.Container == container && x.Cartons != 0); _ws = _wb.Worksheets[1]; _ws.Cells[1, 2] = container; var startIndex = 4; var maxLocs = 6; var parser = new StringParser(); foreach (var c in cartonDetails) { _ws.Cells[startIndex, 1] = c.CartonRange; _ws.Cells[startIndex, 2] = c.PurchaseOrder; _ws.Cells[startIndex, 3] = c.Style; _ws.Cells[startIndex, 4] = c.Customer; _ws.Cells[startIndex, 5] = c.Color; _ws.Cells[startIndex, 6] = c.Cartons; if (c.PreLocation == null) { startIndex++; continue; } var list = parser.ParseStrToPreLoc(c.PreLocation).ToList(); for (var i = 0; i < list.Count(); i++) { if (list[i].Plts == 1) { _ws.Cells[startIndex, 8 + (i * 2)] = list[i].Ctns; } else { _ws.Cells[startIndex, 8 + (i * 2)] = list[i].Ctns + "X" + list[i].Plts; } _ws.Cells[startIndex, 9 + (i * 2)] = list[i].Location; } maxLocs = Math.Max(maxLocs, list.Count()); startIndex++; } //补全Loc在表中显示的数量 if (maxLocs > 6) { var dif = maxLocs - 6; for (int i = 0; i < dif; i++) { _ws.Cells[3, 20 + i] = (6 + i + 1).ToString() + "th"; _ws.Cells[3, 21 + i] = "LOC"; } } var fullPath = @"D:\OtherReport\Pre-allocatingReport-" + DateTime.Now.ToString("yyyyMMddhhmmssffff") + ".xlsx"; _wb.SaveAs(fullPath, Type.Missing, "", "", Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, 1, false, Type.Missing, Type.Missing, Type.Missing); _excel.Quit(); var killer = new ExcelKiller(); killer.Dispose(); return(fullPath); }
//生成Label文件至内存,并下载 public void GenerateLabelPdf(string container) { var cartonDetails = _context.RegularCartonDetails .Include(x => x.POSummary.RegularCartonDetails) .Where(x => x.Container == container && x.Cartons != 0) .ToList(); //var poSummariesInDb = _context.POSummaries // .Include(x => x.RegularCartonDetails) // .Where(x => x.Container == container); var parser = new StringParser(); //定义字体 var BF_light = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //设置页面大小 var rec = new Rectangle(432, 288, 90); //定义内存流 using (var ms = new MemoryStream()) //在doc构造函数中传入页面大小和设置页边距(左,右,上,下,单位是pt) using (var doc = new Document(rec, 0, 0, 14, 14)) //实际写入文件到内存流中 using (var pw = PdfWriter.GetInstance(doc, ms)) { doc.Open(); //打开文件 foreach (var c in cartonDetails) { var font = new Font(BF_light, 20); var font2 = new Font(BF_light, 28); var size = CombineSize(c); var containerCell = CreateTableCell("CTNR#: " + c.Container, font2, 1); containerCell.Colspan = 2; containerCell.HorizontalAlignment = 1; var sizeCell = CreateTableCell("SIZE: " + size, font, 0); var rangeCell = CreateTableCell("CTN RANGE: " + c.CartonRange, font, 0); var poCell = CreateTableCell("PO#: " + c.PurchaseOrder, font, 0); var styleCell = CreateTableCell("STYLE: " + c.Style, font, 0); var colorCell = CreateTableCell("COLOR: " + c.Color + "/" + c.ColorCode, font, 0); var remarkCell = CreateTableCell("REMARK:", font, 0); if (c.PreLocation != null) { var locations = parser.ParseStrToPreLoc(c.PreLocation); foreach (var l in locations) { var infoTable = new PdfPTable(2); var ctnCell = CreateTableCell("CTNS: " + (l.Plts == 1 ? l.Ctns.ToString() : l.Ctns + "X" + l.Plts), font, 0); var locationCell = CreateTableCell("LOC: " + l.Location, font2, 1); locationCell.Colspan = 2; locationCell.HorizontalAlignment = 1; //单元格顺序 infoTable.AddCell(containerCell); infoTable.AddCell(styleCell); infoTable.AddCell(poCell); infoTable.AddCell(colorCell); infoTable.AddCell(sizeCell); infoTable.AddCell(ctnCell); infoTable.AddCell(remarkCell); infoTable.AddCell(locationCell); //infoTable.AddCell(rangeCell); infoTable.WidthPercentage = 90f; doc.Add(infoTable); doc.NewPage(); } } else { var infoTable = new PdfPTable(2); var ctnCell = CreateTableCell("CTNS: " + c.Cartons, font, 0); var locationCell = CreateTableCell("LOC: N/A", font2, 1); locationCell.Colspan = 2; locationCell.HorizontalAlignment = 1; //单元格顺序 infoTable.AddCell(containerCell); infoTable.AddCell(styleCell); infoTable.AddCell(poCell); infoTable.AddCell(colorCell); infoTable.AddCell(sizeCell); infoTable.AddCell(ctnCell); infoTable.AddCell(remarkCell); infoTable.AddCell(locationCell); //infoTable.AddCell(rangeCell); infoTable.WidthPercentage = 90f; doc.Add(infoTable); doc.NewPage(); } } //循环到这里结束 doc.Close(); pw.Close(); ms.Close(); //输出到客户端 var response = HttpContext.Current.Response; response.Clear(); response.ContentType = "Application/pdf"; response.AddHeader("content-disposition", "attachment; filename=" + container + "-Labels.pdf"); response.BinaryWrite(ms.ToArray()); //ms.WriteTo(response.OutputStream); //这样也行 response.Flush(); response.Close(); response.End(); } }