怎么实现打印功能
解决方案 »
- C# TreeView中 Node可存储信息
- WPF/SL 中得依赖属性问题
- 各位大神我又来求助了,双缓冲的方面
- 求大神解答 MVC 向ORACLE数据库中插入数据 这句话return Entities.SaveChanges() >= 0;出错 为什么啊?
- 关于分类多选,如何查询某个分类的信息,求SQL句子
- C#能不能释放一个变量?
- System.Collections.Generic;应该引入哪个引用啊???缺少哪个...
- 关于反射获取带out参数的静态方法问题
- 请问利用SmtpMail.Send直接发送邮件需要什么配置?
- 读书中碰到的疑难请求指点——有关利用反射鉴别方法的调用方来改变其行为
- 请问如何打开默认的邮件程序创建新邮件,并添加附件?
- 怎样在单件模式的Form引用其他Form
{
/*
*
*/
}
<P class=msonormal style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 25pt; mso-line-height-rule: exactly"><SPAN style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: times new roman; mso-ascii-font-family: times new roman">区内各企业:</SPAN><SPAN lang=en-us style="FONT-SIZE: 14pt"><o:p></o:p></SPAN></P>
// 设置报告的设备信息
protected void SetDeviceInfo(LocalReport report)
{
Warning[] warnings;
m_streams = new List<Stream>();
m_currentPageIndex = 0; try
{
report.Render("Image", DeviceInfo, CreateStream, out warnings);
}
catch (Exception e)
{
ErrMessage = e.Message + e.InnerException.Message;
System.Windows.Forms.MessageBox.Show(ErrMessage);
} foreach (Stream stream in m_streams)
stream.Position = 0;
} // 构建流
protected Stream CreateStream(string name, string fileNameExtension, Encoding enfcoding, string mimeType, bool willSeek)
{
Stream stream;
m_currentPageIndex++;
stream = new FileStream(ReportNo + "-" + m_currentPageIndex.ToString() + ".emf", FileMode.OpenOrCreate);
m_streams.Add(stream);
return stream;
} // 打印报告
protected void PrintDialog()
{
if (m_streams == null || m_streams.Count == 0)
return; m_currentPageIndex = 0; // 从首页开始打印 PrintDocument printDoc = new PrintDocument();
printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();
printDoc.DocumentName = "报告-" + ReportNo + "-" + SpecimenXhSet; if (PrinterName != "") // 如指定过打印机,则用指定的,一般使用默认打印机
printDoc.PrinterSettings.PrinterName = PrinterName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("打印机不可用!! \"{0}\".", PrinterName);
Console.WriteLine(msg);
ErrMessage = msg;
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
try
{
printDoc.Print(); // 开始打印
Thread.Sleep(500);
foreach (Stream stream in m_streams) // 使用完后,一定要关闭文件流对象!!
{
stream.Close();
}
if (System.IO.File.Exists(ReportNo + ".emf")) // 删除报告图像文件名!
File.Delete(ReportNo + ".emf");
for (int j = 0; j <= m_currentPageIndex; j++)
{
if (System.IO.File.Exists(ReportNo + "-" + j.ToString() + ".emf")) // 删除报告图像文件名!
File.Delete(ReportNo + "-" + j.ToString() + ".emf");
}
}
catch (Exception e)
{
if (!e.Message.Contains("成功")) // 对于提示"操作成功完成"的提醒,不做处理!
MessageBox.Show("现在不能打印," + e.Message);
}
} // 页打印事件处理
protected void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); // 根据数据流,生成图形数据 ev.Graphics.DrawImage(pageImage, ev.PageBounds); m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
// 打印
protected void PrintDialog()
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();
printDoc.DocumentName = "报告-" + this.ReportNo; if (PrinterName != "") // 如指定过打印机,则用指定的,一般使用默认打印机
printDoc.PrinterSettings.PrinterName = PrinterName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("打印机不可用!! \"{0}\".", PrinterName);
Console.WriteLine(msg);
ErrMessage = msg;
return;
} printDoc.PrintPage += new PrintPageEventHandler(PrintPage); try
{
printDoc.Print(); // 开始打印
// ...
}
catch (Exception e)
{
if (!e.Message.Contains("成功")) // 对于提示"操作成功完成"的提醒,不做处理!
MessageBox.Show("现在不能打印," + e.Message);
}
} // 页打印事件处理
protected void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); // 根据数据流,生成图形数据
ev.Graphics.DrawImage(pageImage, ev.PageBounds); m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
<OBJECT id="WebBrowser" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"
VIEWASTEXT>
</OBJECT>
<input onclick="document.all.WebBrowser.ExecWB(6,1)" type="button" value="打印"> <input onclick="document.all.WebBrowser.ExecWB(6,6)" type="button" value="直接打印">
<input onclick="document.all.WebBrowser.ExecWB(8,1)" type="button" value="页面设置">
<input onclick="document.all.WebBrowser.ExecWB(7,1)" type="button" value="打印预览">
</div>
LocalReport应该是上面命名空间的吧?这就不行了啊
用Web打印也是可以的而且比较简单。 希望对你有帮助
<head>
//这个是用来隐藏不打印的部分 CSS
<style type="text/css" media=print>
.noprint{display : none }
</style>
</head>
<form>
<OBJECT id="WebBrowser" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" VIEWASTEXT>
</OBJECT><p class="noprint" align="center"> //来隐藏不打印的部分
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
<input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=关闭所有>
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印 id="Button2">
<input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=打印预览>
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置>
<input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=刷新>
</p>
</form>
private Excel.Application xApp = null;
private Excel.Workbook xBook = null;
private Excel.Worksheet xSheet = null;private void Print()
{
// check条件
CheckPrint();
// excel output method
// new Application
xApp = new Excel.ApplicationClass();
xApp.Visible = false;
xApp.DisplayAlerts = false;
//_excel.DisplayAlerts = false;
xApp.AlertBeforeOverwriting = false;
// get file path 模板
string path = Application.StartupPath.Trim() + @"\report\test.xls";
// open xml file
xBook = xApp.Workbooks.Open(path,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// open file's sheet
xSheet = (Excel.Worksheet)xBook.Sheets[1];
// Sheet名
xSheet.Name = ReplaceString(txtSozai.Text); object missing = System.Reflection.Missing.Value;
#region 插入数据
// 比如画面上有个textbox,放到模板中的A2
xSheet[2,1] = textbox.text;
#endregion #region 印刷
xApp.Visible = true;
//EXCEL印刷
xSheet.PrintOut(missing, missing, missing, true, cboPrint.Text, missing, missing, missing);
// 打印出来存放位置
string pathFile = Application.StartupPath.Trim()
+ @"\test";
if (Directory.Exists(pathFile) == false)
{
// 创建pathFile
Directory.CreateDirectory(pathFile);
}
// 文件名
String pathName = pathFile + ".xls";
if (File.Exists(pathName))
{
FileInfo fi1 = new FileInfo(pathName);
fi1.Attributes = FileAttributes.Normal;
File.Delete(pathName);
}
xApp.ActiveWorkbook.SaveAs(
pathName,
Excel.XlFileFormat.xlExcel9795,
null,
null,
false,
false,
Excel.XlSaveAsAccessMode.xlNoChange,
null,
null,
null,
null); FileInfo fi = new FileInfo(pathName); fi.Attributes = fi.Attributes | FileAttributes.ReadOnly;
// Close
xApp.Application.Workbooks.Close();
xApp.Quit();
#endregion
}