回复 1楼zysfaithboy的帖子
目前Spread Silverlight/WPF版本不支持直接打印功能,非常抱歉。
有2个办法:
方法一:
- private void btnPrint_Click(object sender, RoutedEventArgs e)
- {
- this.Cursor = System.Windows.Input.Cursors.Wait;
- SpreadSheetPrinter ssp = new SpreadSheetPrinter(this.gcSpreadSheet1);
- ssp.PrintDocument.EndPrint += (ss, ee) => { this.Cursor = System.Windows.Input.Cursors.Arrow; };
- ssp.PrintActiveSheet("SpreadSheet Print");
- }
- public class SpreadSheetPrinter
- {
- GcSpreadSheet printSource;
- PrintDocument pd;
- string documentName;
- Grid printVisual;
- Grid container;
- TextBlock header;
- GcSpreadSheet printPreview;
- TextBlock footer;
- int vPageBreak;
- int vNextPageBreak;
- int hPageBreak;
- int hNextPageBreak;
- double pageWidth;
- double pageHeight;
- public SpreadSheetPrinter(GcSpreadSheet spread)
- {
- printSource = spread;
- pd = new PrintDocument();
- pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);
- this.printVisual = new Grid();
- this.container = new Grid();
- this.container.RowDefinitions.Add(new RowDefinition());
- this.container.RowDefinitions.Add(new RowDefinition());
- this.container.RowDefinitions.Add(new RowDefinition());
- this.printVisual.Children.Add(this.container);
- this.header = new TextBlock();
- this.header.FontSize = 20;
- this.header.VerticalAlignment = System.Windows.VerticalAlignment.Top;
- this.header.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- this.container.Children.Add(this.header);
- this.footer = new TextBlock();
- this.footer.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
- this.footer.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- this.container.Children.Add(this.footer);
- this.footer.SetValue(Grid.RowProperty, 2);
- this.printPreview = new GcSpreadSheet();
- this.container.Children.Add(this.printPreview);
- this.printPreview.SetValue(Grid.RowProperty, 1);
- this.printPreview.VerticalAlignment = System.Windows.VerticalAlignment.Top;
- this.printPreview.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
- }
- public PrintDocument PrintDocument
- {
- get { return pd; }
- }
- public void PrintActiveSheet(string documentName)
- {
- MemoryStream ms = new MemoryStream();
- this.printSource.SaveXml(ms);
- ms.Seek(0, System.IO.SeekOrigin.Begin);
- this.printPreview.OpenXml(ms);
- ms.Close();
- // hide scrollbar tabstrip border
- this.printPreview.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
- this.printPreview.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
- this.printPreview.TabStripVisibility = System.Windows.Visibility.Collapsed;
- this.printPreview.BorderThickness = new Thickness(0);
- // hide selection
- var sheet = this.printPreview.ActiveSheet;
- sheet.RowCount++;
- sheet.ColumnCount++;
- sheet.FrozenTrailingColumnCount = 1;
- sheet.FrozenTrailingRowCount = 1;
- sheet.SetRowVisible(sheet.RowCount - 1, SheetArea.Cells, false);
- sheet.SetColumnVisible(sheet.ColumnCount - 1, SheetArea.Cells, false);
- sheet.SetActiveCell(sheet.RowCount - 1, sheet.ColumnCount - 1, true);
- vPageBreak = 0;
- vNextPageBreak = -1;
- hPageBreak = 0;
- hNextPageBreak = -1;
- pageWidth = 0;
- pageHeight = 0;
- this.documentName = documentName;
- pd.Print(documentName);
- }
- void pd_PrintPage(object sender, PrintPageEventArgs e)
- {
- var sheet = this.printPreview.ActiveSheet;
- var margin = sheet.PrintInfo.Margin;
- // set control position
- this.container.Margin = new Thickness(margin.Left, margin.Top, margin.Right, margin.Bottom);
- this.container.RowDefinitions[0].Height = new GridLength(margin.Header);
- this.container.RowDefinitions[2].Height = new GridLength(margin.Footer);
- this.header.Text = this.documentName;
- this.footer.Text = string.Format("page {0}", this.pd.PrintedPageCount + 1);
- // scroll to current page
- this.printPreview.ShowRow(0, vPageBreak, VerticalPosition.Top);
- this.printPreview.ShowColumn(0, hPageBreak, HorizontalPosition.Left);
- // calc current page size, and find next page break
- if (pageHeight == 0) // current page height is not calc yet
- {
- for (int i = vPageBreak; i < sheet.RowCount; i++)
- {
- double rowHeight = sheet.GetActualRowHeight(i, SheetArea.Cells) * sheet.ZoomFactor;
- if ((pageHeight + rowHeight) > (e.PrintableArea.Height - margin.Bottom - margin.Top - margin.Header - margin.Footer))
- {
- vNextPageBreak = i;
- break;
- }
- pageHeight += rowHeight;
- }
- if (this.printPreview.Height != pageHeight)
- {
- this.printPreview.Height = pageHeight;
- }
- }
- if (pageWidth == 0) // current page width is not calc yet
- {
- for (int i = hPageBreak; i < sheet.ColumnCount; i++)
- {
- double columnWidth = sheet.GetActualColumnWidth(i, SheetArea.Cells) * sheet.ZoomFactor;
- if ((pageWidth + columnWidth) > (e.PrintableArea.Width - margin.Left - margin.Right))
- {
- hNextPageBreak = i;
- break;
- }
- pageWidth += columnWidth;
- }
- if (this.printPreview.Width != pageWidth)
- {
- this.printPreview.Width = pageWidth;
- }
- }
- e.PageVisual = this.printVisual;
- // prepare for next page
- if (sheet.PrintInfo.PageOrder == PrintPageOrder.DownThenOver)
- {
- if (vNextPageBreak != -1) // has next page in vertical
- {
- vPageBreak = vNextPageBreak;
- vNextPageBreak = -1;
- pageHeight = 0;
- e.HasMorePages = true;
- }
- else if (hNextPageBreak != -1)
- {
- hPageBreak = hNextPageBreak;
- hNextPageBreak = -1;
- pageWidth = 0;
- pageHeight = 0;
- vPageBreak = 0;
- vNextPageBreak = -1;
- e.HasMorePages = true;
- }
- }
- else
- {
- if (hNextPageBreak != -1)
- {
- hPageBreak = hNextPageBreak;
- hNextPageBreak = -1;
- pageWidth = 0;
- e.HasMorePages = true;
- }
- else if (vNextPageBreak != -1)
- {
- vPageBreak = vNextPageBreak;
- vNextPageBreak = -1;
- pageWidth = 0;
- pageHeight = 0;
- hPageBreak = 0;
- hNextPageBreak = -1;
- e.HasMorePages = true;
- }
- }
- }
- }
复制代码
方法二:先打印成PDF,然后调用PDF的打印功能去打印
在进行 PDF 导出时,中文字体需要使用 SimSun 字体,不然就会出现乱码 |