Spread WPF-Silverlight打印功能例程
版主您好!我们用silverlight开发的系统用到了咱们spread组件,关于咱们spread组件的打印功能能否提供一个例程?谢谢! 回复 1楼zysfaithboy的帖子
目前SpreadSilverlight/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.Height = new GridLength(margin.Header);
this.container.RowDefinitions.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 字体,不然就会出现乱码 非常感谢版主回复,您能不能把方法一所在的工程压一个包,发给我们学习一下。谢谢! 回复 3楼zysfaithboy的帖子
帮您整理了一个包,供您参考:
收到,非常感谢版主! 回复 5楼zysfaithboy的帖子
:mj72:客气了 回复 5楼zysfaithboy的帖子
您好,
目前 Spread 8.0 在XAML和WinRT平台产品中提供了打印和打印预览功能,公式支持结构化引用,新的控件外观,更重要的是在Excel导入速度方面也有明显的提升。
下载链接:
http://www.gcpowertools.com.cn/downloads/trial/SpreadStudio/8/SpreadforWPF-Silverlightv8.zip
谢谢
页:
[1]