using BBWY.Client.Extensions; using BBWY.Client.Models.PackTask; using BBWY.Controls; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.Printing; using System.IO; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; namespace BBWY.Client.Views.SealBox { /// /// SetSealBoxWindow.xaml 的交互逻辑 /// public partial class SetSealBoxWindow : BWindow { public SetSealBoxWindow(SealBoxModel sealBoxModel) { SealBoxModel = sealBoxModel; InitializeComponent(); LoadPrints(); } SealBoxModel SealBoxModel { get; set; } /// /// 获取打印机名称 /// private void LoadPrints() { var printingNames = PrinterSettings.InstalledPrinters;//获取本机的打印机数据 int index = -1; int selectIndex = 0; foreach (string name in printingNames) { if (name == "Microsoft XPS Document Writer" || name == "Microsoft Print to PDF" || name == "Fax") { continue; } index++; if (name.Contains("Deli")) { selectIndex = index; } cbPrint.Items.Add(name); } if (cbPrint.Items.Count > selectIndex) { cbPrint.SelectedIndex = selectIndex; } } public Action SendBoxCount { get; set; } private void BButton_Click(object sender, RoutedEventArgs e) { int boxCount = 0; try { boxCount = Convert.ToInt32(tbSealBoxCount.Text); } catch (Exception) { MessageBox.Show("请输入数字!"); return; } PrintSealboxModel(cbPrint.Text, boxCount); //todo: 打印单子 if (SendBoxCount != null) SendBoxCount(boxCount); this.Close(); } private void PrintSealboxModel(string printName, int boxCount) { var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var excel = $"{applicationPath}/Resources/ExccelModel/sealbox.xlsx"; var newExccel = $"{applicationPath}/Resources/ExccelModel/newsealbox.xlsx"; if (File.Exists(newExccel)) { File.Delete(newExccel); } FileStream fs = new FileStream(excel, FileMode.Open, FileAccess.Read); XSSFWorkbook wb = new XSSFWorkbook(fs); var sheet = wb.GetSheetAt(0); // 得到第一个sheet var nameCell = sheet.GetRow(0).GetCell(0); // name列,第2行 var ageCell = sheet.GetRow(3).GetCell(0); // age列,第2行 XSSFWorkbook wb2 = new XSSFWorkbook(); //wb2.CreateSheet("Sheet1"); sheet.CopyTo(wb2, "Sheet1", true, false); var sheet2 = wb2.GetSheet("Sheet1"); var nameCell2 = sheet2.GetRow(0).GetCell(1); nameCell2.SetCellValue(SealBoxModel.ShopName); sheet2.GetRow(3).GetCell(1).SetCellValue(SealBoxModel.WareName); StringBuilder sb = new StringBuilder(); int totalCount = 0; for (int i = 0; i < SealBoxModel.SealBoxSkus.Count; i++) { var title = SealBoxModel.SealBoxSkus[i].SkuTitle; //if (title.Length>5) //{ // title = title.Substring(0,5); //} totalCount += SealBoxModel.SealBoxSkus[i].SkuCount; sheet2.GetRow(i + 6).GetCell(1).SetCellValue($"名称:{title}"); sheet2.GetRow(i + 6).GetCell(2).SetCellValue($"SKU:{SealBoxModel.SealBoxSkus[i].SkuId}"); sheet2.GetRow(i + 6).GetCell(3).SetCellValue($"数量:{SealBoxModel.SealBoxSkus[i].SkuCount}"); } sheet2.GetRow(25).GetCell(1).SetCellValue(totalCount); sheet2.GetRow(28).GetCell(1).SetCellValue(boxCount); FileStream fs1 = new FileStream(newExccel, FileMode.OpenOrCreate, FileAccess.ReadWrite); wb2.Write(fs1); fs1.Close(); fs1.Dispose(); } } public class PrintDocumentAdapter : PrintDocument { FileStream fs; public PrintDocumentAdapter(FileStream fs) { this.fs = fs; } protected override void OnPrintPage(PrintPageEventArgs e) { if (fs == null) return; IWorkbook workbook = null; if (fs.Name.EndsWith(".xls")) // XLS文件 { workbook = new HSSFWorkbook(fs); } else if (fs.Name.EndsWith(".xlsx")) // XLSX文件 { workbook = new XSSFWorkbook(fs); } if (workbook != null) { ISheet sheet = workbook.GetSheetAt(0); // 第一个sheet // 使用NPOI的ExcelPrinting类打印sheet ExcelPrinting printer = new ExcelPrinting(sheet); printer.PrintArea(e.Graphics, e.MarginBounds, sheet.SheetName); fs.Close(); } base.OnPrintPage(e); } } }