Browse Source

1

AddValidOverTime
506583276@qq.com 2 years ago
parent
commit
ad5bbdf88b
  1. 1
      BBWY.Client/BBWY.Client.csproj
  2. 111
      BBWY.Client/Helpers/MyPrintHelper.cs
  3. 101
      BBWY.Client/Helpers/UnitConverHelper.cs
  4. 2
      BBWY.Client/Models/QualityTask/PurchaseSku.cs
  5. 111
      BBWY.Client/Views/PackTask/ServiceWindow.xaml.cs
  6. 15
      BBWY.Client/Views/QualityTask/QualityWindow.xaml
  7. 10
      BBWY.Client/Views/SealBox/SetSealBoxWindow.xaml
  8. 91
      BBWY.Client/Views/SealBox/SetSealBoxWindow.xaml.cs

1
BBWY.Client/BBWY.Client.csproj

@ -35,6 +35,7 @@
<ItemGroup>
<PackageReference Include="BarcodeLib" Version="2.4.0" />
<PackageReference Include="FreeSpire.XLS" Version="12.7.0" />
<PackageReference Include="HandyControl" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.28" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />

111
BBWY.Client/Helpers/MyPrintHelper.cs

@ -1,6 +1,9 @@
using BarcodeLib;
using BBWY.Client.Extensions;
using BBWY.Client.Models;
using BBWY.Client.Models.PackTask;
using NPOI.XSSF.UserModel;
using Spire.Xls;
using System;
using System.Collections.Generic;
using System.Drawing;
@ -9,6 +12,7 @@ using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Media.Imaging;
using WebSocketSharp;
@ -618,5 +622,112 @@ namespace BBWY.Client.Helpers
}
}
public static void SetDefaultPrint(string printName)
{
SetDefaultPrinter(printName);
}
/// <summary>
/// 调用win api将指定名称的打印机设置为默认打印机
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
[DllImport("winspool.drv")]
public static extern bool SetDefaultPrinter(string Name);
public static void PrintSealBoxData(SealBoxModel sealBoxModel, string printName, int boxCount, int printCount )
{
// 100*180
UnitConverHelper unitConverHelper = new UnitConverHelper();
var data = unitConverHelper.MmToPx(100, 180);
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string printNamePath = $"{applicationPath}/printSealName.init";
if (File.Exists(printNamePath))
{
File.Delete(printNamePath);
}
File.WriteAllText(printNamePath, printName);
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();
Workbook workbook = new Workbook();
workbook.LoadFromFile(newExccel);
Worksheet wsheet = workbook.Worksheets[0];
wsheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
wsheet.PageSetup.Orientation = PageOrientationType.Portrait;
wsheet.PageSetup.PrintArea = "A1:D31";
var print = workbook.PrintDocument;
print.PrinterSettings.PrinterName = printName;
print.PrinterSettings.Copies = (short)printCount;//打印份数
print.Print();
}
}
}

101
BBWY.Client/Helpers/UnitConverHelper.cs

@ -0,0 +1,101 @@
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
namespace BBWY.Client.Helpers
{
public class UnitConverHelper
{
int dpiX = 0;
int dpiY = 0;
public UnitConverHelper()
{
GetDpi();
}
private void GetDpi()
{
var hDc = GetDC(IntPtr.Zero);
dpiX = GetDeviceCaps(hDc, LOGPIXELSX);
dpiY = GetDeviceCaps(hDc, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hDc);
}
/// <summary>
/// 毫米转像素(获取打印的宽和高)
/// </summary>
/// <param name="mm"></param>
/// <returns></returns>
public PrintPx MmToPx(double mWidth, double mHeight)
{
if(dpiX<=0||dpiY<=0) GetDpi();
return new PrintPx
{
Width = mWidth * dpiX / 25.4,
Height = mHeight * dpiY / 25.4,
};
}
/// <summary>
/// 像素转毫米
/// </summary>
/// <param name="pxWidth"></param>
/// <param name="pxHeight"></param>
/// <returns></returns>
public PrintPx PxToMm(double pxWidth, double pxHeight)
{
if (dpiX <= 0 || dpiY <= 0) GetDpi();
return new PrintPx
{
Width = pxWidth * 25.4 / dpiX,
Height = pxHeight * 25.4 / dpiY,
};
}
private const int LOGPIXELSX = 88;
private const int LOGPIXELSY = 90;
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int index);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
}
public class PrintPx
{
public double Width { get; set; }
public double Height { get; set; }
}
}

2
BBWY.Client/Models/QualityTask/PurchaseSku.cs

@ -69,5 +69,7 @@ namespace BBWY.Client.Models.QualityTask
}
}
}
}

111
BBWY.Client/Views/PackTask/ServiceWindow.xaml.cs

@ -1,14 +1,18 @@
using BarcodeLib;
using BBWY.Client.APIServices;
using BBWY.Client.Helpers;
using BBWY.Client.ViewModels;
using BBWY.Client.ViewModels.PackTask;
using BBWY.Controls;
using HandyControl.Controls;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Printing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Windows;
@ -31,33 +35,28 @@ namespace BBWY.Client.Views.PackTask
{
InitializeComponent();
}
private void BButton_Click(object sender, RoutedEventArgs e)
{
var localPrintServer = new LocalPrintServer();
string printName = cbPrintName.Text.Trim();
if (string.IsNullOrEmpty(printName))
{
MessageBox.Show("选择打印机");
System.Windows.MessageBox.Show("选择打印机");
return;
}
var printQueue = localPrintServer.GetPrintQueue(printName);
if (printQueue.IsInError)
{
MessageBox.Show("打印机处于错误状态");
System.Windows.MessageBox.Show("打印机处于错误状态");
return;
}
//Print(this.printArea, cbPrintName.Text, "打印任务",1);
MyPrintHelper.SetDefaultPrint(printName);//设置默认的打印机
this.printArea.Arrange(new Rect(new Point(0, 0), new Size(printArea.ActualWidth, printArea.ActualHeight)));
PrintDialog printDialog = new PrintDialog();
System.Windows.Controls.PrintDialog printDialog = new PrintDialog();
//设置纸张大小
var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth);
var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight);
@ -76,99 +75,7 @@ namespace BBWY.Client.Views.PackTask
/// <summary>
/// 打印
/// </summary>
/// <param name="document">流文档</param>
/// <param name="printer">打印机名称</param>
/// <param name="description">打印描述</param>
/// <param name="copyCount">打印个数</param>
public static void Print(Visual document, string printer, string description, int copyCount)
{
var localPrintServer = new LocalPrintServer();
var printQueue = localPrintServer.GetPrintQueue(printer);
if (printQueue.IsInError)
{
throw new Exception("打印机处于错误状态");
}
var printDialog = new PrintDialog
{
PrintQueue = printQueue, //打印队列
PrintTicket = { CopyCount = copyCount } //打印个数
};
//设置纸张大小
var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth); //小标签:114
var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight); //小标签:227
printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight);
//设置纸张边距
var paperSize = GetPaperSize(printer); //小标签:118*246
//var offsetX = (int)Math.Ceiling((paperSize.Width - pageWidth) / 2f);
//var offsetY = (int)Math.Ceiling((paperSize.Height - pageHeight) / 2f);
//document.PagePadding = new Thickness(offsetX, offsetY, offsetX, offsetY);
//打印
var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
printDialog.PrintDocument(paginator, description);
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string printNames = System.IO.Path.Combine(applicationPath, "printName.init");
File.WriteAllText(printNames, printer);
}
private static object GetPaperSize(string printer)
{
return null;
}
public interface IDocumentRenderer
{
void Render(FlowDocument doc, object data);
}
public class CommonDocumentRenderer : IDocumentRenderer
{
public void Render(FlowDocument doc, object data)
{
var model = data as PrintModel;
if (model == null)
{
throw new ArgumentException("data is not PrintModel");
}
var type = typeof(PrintModel);
var properties = type.GetProperties();
foreach (var property in properties)
{
//文本赋值
if (doc.FindName(property.Name) is TextBlock textBlock)
{
textBlock.Text = property.GetValue(model)?.ToString();
}
}
}
}
public class PrintModel
{
/// <summary>
/// 批号
/// </summary>
public string BatchNumber { get; set; }
/// <summary>
/// 订单号
/// </summary>
public string OrderNumber { get; set; }
/// <summary>
/// 物料代码
/// </summary>
public string MaterialNumber { get; set; }
}
}
}

15
BBWY.Client/Views/QualityTask/QualityWindow.xaml

@ -205,7 +205,7 @@
ItemContainerStyle="{StaticResource NoBgListBoxItemStyle}"
ItemsSource="{Binding PurchaseSkuList,Mode=TwoWay}"
BorderBrush="{StaticResource Border.Brush}"
BorderThickness="0" Visibility="{Binding OrderId,Mode=TwoWay,Converter={StaticResource objConverter},ConverterParameter=#null:Collapsed:Visible}"
BorderThickness="0" Visibility="{Binding SkuPurchaseSchemeId,Mode=TwoWay,Converter={StaticResource objConverter},ConverterParameter=#null:Collapsed:Visible}"
Foreground="{StaticResource Text.Color}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
@ -221,17 +221,6 @@
Height="150" Width="150" Stretch="Fill"
VerticalAlignment="Top" Margin="20 0 0 0"
Cursor="Hand">
<!--<b:Interaction.Triggers>
<b:EventTrigger EventName="PreviewMouseLeftButtonDown">
<b:InvokeCommandAction Command="{Binding DataContext.OpenSkuDetailCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}">
<b:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource mptConverter}">
<Binding Path="SkuId"/>
</MultiBinding>
</b:InvokeCommandAction.CommandParameter>
</b:InvokeCommandAction>
</b:EventTrigger>
</b:Interaction.Triggers>-->
</c:BAsyncImage>
<DockPanel VerticalAlignment="Top" Width="190" Height="150">
@ -309,7 +298,7 @@
ItemContainerStyle="{StaticResource NoBgListBoxItemStyle}"
ItemsSource="{Binding PurchaseSkuList,Mode=TwoWay}"
BorderBrush="{StaticResource Border.Brush}"
BorderThickness="0" Visibility="{Binding OrderId,Mode=TwoWay,Converter={StaticResource objConverter},ConverterParameter=#null:Visible:Collapsed}"
BorderThickness="0" Visibility="{Binding SkuPurchaseSchemeId,Mode=TwoWay,Converter={StaticResource objConverter},ConverterParameter=#null:Visible:Collapsed}"
Foreground="{StaticResource Text.Color}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>

10
BBWY.Client/Views/SealBox/SetSealBoxWindow.xaml

@ -12,10 +12,12 @@
xmlns:sys="clr-namespace:System;assembly=mscorlib"
WindowStartupLocation="CenterScreen"
CloseButtonVisibility="Visible"
CloseButtonColor="{StaticResource WindowButtonColor}"
MinButtonVisibility="Collapsed"
MaxButtonVisibility="Collapsed"
CloseButtonVisibility ="Collapsed" Width="378" Height="219"
Width="378" Height="219"
RightButtonGroupMargin="0,5,5,0">
<!--CloseButtonColor="{StaticResource WindowButtonColor}" -->
<Grid>
@ -41,7 +43,7 @@
</StackPanel>
</StackPanel>
</Grid>
@ -49,4 +51,4 @@
/>
</Grid>
</c:BWindow>
</c:BWindow>

91
BBWY.Client/Views/SealBox/SetSealBoxWindow.xaml.cs

@ -1,14 +1,17 @@
using BBWY.Client.Extensions;
using BBWY.Client.Helpers;
using BBWY.Client.Models.PackTask;
using BBWY.Controls;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Spire.Xls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Printing;
using System.Reflection;
using System.Text;
using System.Windows;
@ -49,16 +52,22 @@ namespace BBWY.Client.Views.SealBox
{
continue;
}
index++;
if (name.Contains("Deli"))
{
selectIndex = index;
}
//index++;
//if (name.Contains("Deli"))
//{
// selectIndex = index;
//}
cbPrint.Items.Add(name);
}
if (cbPrint.Items.Count > selectIndex)
//if (cbPrint.Items.Count > selectIndex)
//{
// cbPrint.SelectedIndex = selectIndex;
//}
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string printNamePath = $"{applicationPath}/printSealName.init";
if (File.Exists(printNamePath))
{
cbPrint.SelectedIndex = selectIndex;
cbPrint.Text = File.ReadAllText(printNamePath);
}
}
@ -81,18 +90,40 @@ namespace BBWY.Client.Views.SealBox
MessageBox.Show("请输入数字!");
return;
}
string printName = cbPrint.Text;
var localPrintServer = new LocalPrintServer();
var printQueue = localPrintServer.GetPrintQueue(printName);
if (printQueue.IsInError)
{
MessageBox.Show("打印机处于错误状态");
return;
}
PrintSealboxModel(cbPrint.Text, boxCount);
//PrintSealboxModel(printName, boxCount);
MyPrintHelper.PrintSealBoxData(SealBoxModel, printName, boxCount, 1);
//todo: 打印单子
if (SendBoxCount != null) SendBoxCount(boxCount);
this.Close();
}
private void PrintSealboxModel(string printName, int boxCount)
private void PrintSealboxModel(string printName, int boxCount, int printCount = 1)
{
//
}
private void PrintSealboxModel1(string printName, int boxCount)
{
var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string printNamePath = $"{applicationPath}/printSealName.init";
if (File.Exists(printNamePath))
{
File.Delete(printNamePath);
}
File.WriteAllText(printNamePath, printName);
var excel = $"{applicationPath}/Resources/ExccelModel/sealbox.xlsx";
@ -141,44 +172,22 @@ namespace BBWY.Client.Views.SealBox
wb2.Write(fs1);
fs1.Close();
fs1.Dispose();
}
//Workbook workbook = new Workbook();
//workbook.loa
//workbook.LoadFromFile(newExccel);
//var print = workbook.PrintDocument;
//print.PrinterSettings.PrinterName = printName;
//print.Print();
}
//
public class PrintDocumentAdapter : PrintDocument
{
FileStream fs;
public PrintDocumentAdapter(FileStream fs)
{
this.fs = fs;
}
protected override void OnPrintPage(PrintPageEventArgs e)
void SpirePrint()
{
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);
}
}
}

Loading…
Cancel
Save