C/S程序里打印预览是否能另存为Word文档 我用C#写了一个打印程序。利用PrintDocument这个类配合Graphics画出表格进行打印,现在客户要求在打印预览或者其他时候能把表格输出或者另存为WORD文档。请问哪位知道该怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //做一个打印的模板,用word里面的replace方法。 //将预览另存为word时,先将模板copy到另外的文件夹,然后查找替换。 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 = "需要替换的word文件.doc"; 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 = "要查找的文本1";//要查找的文本 ReplaceWith = "str1";//替换文本 oDoc.Content.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); FindText = "要查找的文本2";//要查找的文本 ReplaceWith = "str2";//替换文本 oDoc.Content.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(); if (oDoc != null) oDoc.Close(ref oMissing, ref oMissing, ref oMissing); if (oWord != null) oWord.Quit(ref oMissing, ref oMissing, ref oMissing); 谢谢楼上的不过我不是通过WORD模版来实现的因为我的打印表格是通过坐标配置后打印出来的,没有固定的格式和位置。所以希望能直接将打印的内容输出到一个WORD文件即可。 那就自己操作word自己画吧.或者用word的Microsoft Office Document Image Writer虚拟打印机,打印成.mdi文件,不知道可不可以? 自己操作word自己画这个说不清楚.需要根据你自己的需要做.Microsoft Office Document Image Writer虚拟打印机office自带的,安装之后,你在打印的时候,只需要选择Microsoft Office Document Image Writer进行打印,虚拟打印机就会打印成一个.mdi的文件,一样的可以对里面的信息copy等操作.可以先google一下Microsoft Office Document Image Writer,了解了解. 根据你的项目情况,目前,比较容易实现的方法,先存一个图,然后把图插入到WORD。 打印预览,截图存为imageimage放入word public Image CatchImage() { Bitmap bmpCatched = new Bitmap(预览控件.Width, 预览控件.Height); Graphics g = Graphics.FromImage(bmpCatched); Cursor.Hide(); g.CopyFromScreen(FlashBox.Location, new Point(0, 0), FlashBox.ClientRectangle.Size); return bmpCatched; } public static void ImageToWord(Image image) { string fileName = "tmp.jpg"; //重命名要要插入的图片 image.Save(fileName); FileInfo fi = new FileInfo(fileName); try { //ApplicationClass wordApp = new ApplicationClass(); Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Application oword = new Microsoft.Office.Interop.Word.Application(); Document doc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//word文档 oword.Selection.Font.Bold = 1; oword.Selection.Font.Size = 12; oword.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; oword.Visible = true; object LinkToFile = false; oword.Application.Selection.InlineShapes.AddPicture(fi.FullName, ref Nothing, ref Nothing, ref Nothing); //doc为活动文档的名称 } catch (Exception ex) { Log.WriteErLog(ex.Message); } finally { System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD"); for (int i = 0; i < CurrentProcess.Length; i++) { if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0) { try { CurrentProcess[i].Kill(); } catch { } } } } } 截图放入word,不能copy文字信息吧? yuxuanji的方案有个问题,就是我每次打印预览的时候,弹出的窗口并不是最大化的,而且有时候他默认的显示比例是50%的,所以预览的时候直接截屏取得的图片会有偏差。 如果可以用图片的话,那你不是都已经用Graphics画过吗?同样可以在画了保存图片插入到word. 可以用excel代替么?如果可以的话,你可以试着用reporting service这个服务可以导出excel,pdf等,还可以打印工具自带的 我这个是个小应用软件,如果应用reporting service 的话显得太庞大了 LZ,可以考虑Microsoft Office Document Image Writer虚拟打印机打印成其它文件保存.而且可以通过修改注册表的值,让虚拟打印机打印时不打开文件.HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\MODI\MDI writer DWORD OpenInMODI=0 c# 控件 如何将贴中一个.net3.5 部分代码转为.net 1.1 C# vs2010 做了程序该如何打包 ASCII转换问题? 求一动态的添加treenode的节点的方法,高手进. 关于对this关键字的理解 大家帮忙看看 遍历文件夹下的文件的排序问题? C#调用CMD运行命名的问题 如何进行事件更改 找出含有三个或以上if,else方法的名称和所在文件
//做一个打印的模板,用word里面的replace方法。
//将预览另存为word时,先将模板copy到另外的文件夹,然后查找替换。
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 = "需要替换的word文件.doc";
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 = "要查找的文本1";//要查找的文本
ReplaceWith = "str1";//替换文本
oDoc.Content.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); FindText = "要查找的文本2";//要查找的文本
ReplaceWith = "str2";//替换文本
oDoc.Content.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(); if (oDoc != null)
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
if (oWord != null)
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
不过我不是通过WORD模版来实现的
因为我的打印表格是通过坐标配置后打印出来的,没有固定的格式和位置。
所以希望能直接将打印的内容输出到一个WORD文件即可。
或者用word的Microsoft Office Document Image Writer虚拟打印机,打印成.mdi文件,不知道可不可以?
Microsoft Office Document Image Writer虚拟打印机office自带的,安装之后,你在打印的时候,只需要选择Microsoft Office Document Image Writer进行打印,虚拟打印机就会打印成一个.mdi的文件,一样的可以对里面的信息copy等操作.可以先google一下Microsoft Office Document Image Writer,了解了解.
截图存为image
image放入word
public Image CatchImage()
{
Bitmap bmpCatched = new Bitmap(预览控件.Width, 预览控件.Height);
Graphics g = Graphics.FromImage(bmpCatched);
Cursor.Hide();
g.CopyFromScreen(FlashBox.Location, new Point(0, 0), FlashBox.ClientRectangle.Size);
return bmpCatched; } public static void ImageToWord(Image image)
{
string fileName = "tmp.jpg"; //重命名要要插入的图片
image.Save(fileName);
FileInfo fi = new FileInfo(fileName);
try
{ //ApplicationClass wordApp = new ApplicationClass();
Object Nothing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oword = new Microsoft.Office.Interop.Word.Application(); Document doc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//word文档 oword.Selection.Font.Bold = 1;
oword.Selection.Font.Size = 12;
oword.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; oword.Visible = true; object LinkToFile = false; oword.Application.Selection.InlineShapes.AddPicture(fi.FullName, ref Nothing, ref Nothing, ref Nothing); //doc为活动文档的名称
}
catch (Exception ex) { Log.WriteErLog(ex.Message); }
finally
{
System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
for (int i = 0; i < CurrentProcess.Length; i++)
{
if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
{
try
{
CurrentProcess[i].Kill();
}
catch
{
}
}
}
} }
如果可以的话,你可以试着用reporting service
这个服务可以导出excel,pdf等,还可以打印
工具自带的
而且可以通过修改注册表的值,让虚拟打印机打印时不打开文件.
HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\MODI\MDI writer
DWORD OpenInMODI=0