我现在从Excel中读取所有客户数据,然后打印成信封,数据读出来了,可不知道怎么打印。
因为不知道这个打印模板做,可以随时更改打印模板!我想过用word来做打印模板,可不知道如何实现!
请高手指点,如还有其他方法,都提出来吧!
谢谢了!
因为不知道这个打印模板做,可以随时更改打印模板!我想过用word来做打印模板,可不知道如何实现!
请高手指点,如还有其他方法,都提出来吧!
谢谢了!
解决方案 »
- 急!WPF项目中使用另外一个程序集中的资源字典引发System.IO.FileNotFoundException
- datagridview的响应问题,在线等,解决就结贴
- 动态插入SQL数据。
- C#中怎么连接ACCESS数据库?
- connection与open()
- 【求助】!!datagridview中的dt 怎么把它变成一个数据集!
- 请教C#取硬盘编号
- 程式碼更改Button座標問題
- 如何写个类,谢谢!!!
- 关于处理空参数问题
- 我的VS2008,怎么项目模板里没有"ADO.NET Entity Data Model"?
- GridView.Rows[0].Cells[1].Text,取不到自动增长列的值
//用word替换的功能可以实现打印模板的功能
//先将模板复制到另外的文件夹,然后替换打印.
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
object fileName = "文件路径";
oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); object FindText, ReplaceWith, Replace;
Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; FindText = "old";//要查找的文本
ReplaceWith = "new";//替换文本
oWord.Selection.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDoc.Save();
oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); if (oDoc != null)
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
if (oWord != null)
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
以下是打印预览的方法,可是如何和word绑定啊? protected virtual void ShowPrintPreviewDialog(PrintDocument printDocument)
{
if (printDocument == null)
{
throw new Exception("关联的打印文档不能为空!");
} try
{
//申明并实例化PrintPreviewDialog
PrintPreviewDialog ppDlg = new PrintPreviewDialog(); //指定打印文档
ppDlg.Document = printDocument;
//显示对话框
DialogResult result = ppDlg.ShowDialog();
if (result == DialogResult.OK)
{
//...
} }
catch (System.Drawing.Printing.InvalidPrinterException e)
{
MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//word里面就有打印预览的功能
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;
object fileName = "文件路径";
oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.PrintPreview();
//注意这个
oWord.Visible = true; //不要下面的
if (oDoc != null)
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
if (oWord != null)
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);为什么会弹出保存为 tiff文件的对话框?
调用打印的方法了?
==============================
保存为 tiff文件的对话框:是你装的虚拟打印机打印文件
一个打印预览的,写10楼的回复.
============================================ //如果打印预览时,文档是没替换的,那就加上下面的代码
object FindText, ReplaceWith, Replace;
Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; FindText = "old";//要查找的文本
ReplaceWith = "new";//替换文本
oWord.Selection.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDoc.Save();
给个判断有没有打印机的代码
我的判断不对:string strdefaultprinter = printDocument1.PrinterSettings.PrinterName;//获取默认的打印机名
if (strdefaultprinter == null || strdefaultprinter == "")
{
MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
foreach (string strprinter in PrinterSettings.InstalledPrinters) //在列表框中列出所有的打印机,
{
if (strprinter == null||strprinter =="")//把默认打印机设为缺省值
{
MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
strdefaultprinter = strprinter;
break;
}