如题所述
ä»å¤©ï¼ç¨å®ä¾ä»£ç ï¼è¯´æ.NETæ¯å¦ä½æå°WORDãEXCELçOFFICEæ件ï¼ä»¥åPDFæ件çã
éç¨æå®çæå°æºæå°OFFICEæ件
æ¤æ¹æ³åå为 âæ¾ç¤ºç¸åºçç¨åºçªå£â å âä¸æ¾ç¤ºç¸åºçç¨åºçªå£â两ç§æ¹å¼ã
(1) æ¾ç¤ºWORDãEXCELçç¨åºçªå£
éç¨æä½ç³»ç»èªèº«çèªå¨è¯å«æ¨¡å¼çæå°ï¼æ¤æ¹æ³å®é éç¨äºNå¤ç§æ件ï¼å¹¶ä¸éäºWORD,EXCEL,PDFä¹ç±»çï¼ä½æ¯è¿ç§æ¹æ³ï¼æä¸ä¸ªç¼ºé·ï¼å°±æ¯ï¼å¯¹äºæäºç±»åçææ¡£ï¼å¦WORD,EXCEL,PDFçï¼æå°æ¶ï¼ä¼æç¸åºçç¨åºçªå£ä¸éªèè¿ã
å®ç°ä»£ç å¦ä¸ï¼
System.Diagnostics.Process p = new System.Diagnostics.Process();
//ä¸ç°å®è°ç¨ç¨åºçªå£,ä½æ¯å¯¹äºæäºåºç¨æ æ
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//éç¨æä½ç³»ç»èªå¨è¯å«ç模å¼
p.StartInfo.UseShellExecute = true;
//è¦æå°çæ件路å¾ï¼å¯ä»¥æ¯WORD,EXCEL,PDF,TXTçç
p.StartInfo.FileName = @"d:\a.doc";
//æå®æ§è¡çå¨ä½ï¼æ¯æå°ï¼å³printï¼æå¼æ¯ open
p.StartInfo.Verb = "print";
//å¼å§
p.Start();
æ¤ç§æ¹æ³ï¼ä»£ç ç®åï¼æ§è½å¥½ï¼å¯é 稳å®ãæ¤ç§æ¹å¼ï¼å¦æè¦æå®æå°æºï¼ååªè½å©ç¨è®¾ç½®é»è®¤æå°æºçæ¹å¼æ¥å®ç°ã
C#设置系ç»é»è®¤æå°æºçå®ç°æ¹æ³ï¼è§ âC#è·åå设置系ç»çé»è®¤æå°æºâ ä¸æ
æ·»å äºæå®æå°æºåè½ç代ç å¦ä¸ï¼
System.Diagnostics.Process p = new System.Diagnostics.Process();
//ä¸ç°å®è°ç¨ç¨åºçªå£,ä½æ¯å¯¹äºæäºåºç¨æ æ
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//éç¨æä½ç³»ç»èªå¨è¯å«ç模å¼
p.StartInfo.UseShellExecute = true;
//è¦æå°çæ件路å¾
p.StartInfo.FileName = @"d:\a.doc";
//æå®æ§è¡çå¨ä½ï¼æ¯æå°ï¼å³printï¼æå¼æ¯ open
p.StartInfo.Verb = "print";
//è·åå½åé»è®¤æå°æº
//string defaultPrinter = GetDefaultPrinter();
//å°æå®çæå°æºè®¾ä¸ºé»è®¤æå°æº
SetDefaultPrinter("æå®çæå°æº");
//å¼å§æå°
p.Start();
//çå¾ åç§
p.WaitForExit(10000);
//å°é»è®¤æå°æºè¿å
SetDefaultPrinter(defaultPrinter);
(2) ä¸æ¾ç¤ºWORDãEXCELçç¨åºçªå£
æ¤ç§æ¹å¼ï¼ä½¿ç¨.NETè°ç¨COMçæ¹å¼æ¥å®ç°ï¼å©ç¨COM对象æ¬èº«çç¹æ§æ¥è®¾ç½®å¯è§æ§åæå°æº
使ç¨æ¤æ¹æ³åï¼éè¦å æ·»å OfficeçCOMå¼ç¨ï¼è¿éç¥è¿ã
//è¦æå°çæ件路å¾
object wordFile = @"d:\a.doc";
object oMissing = Missing.Value;
//èªå®ä¹objectç±»åçå¸å°å¼
object oTrue = true;
object oFalse = false;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//å®ä¹WORD Applicationç¸å ³
Word.Application appWord = new Word.Application();
//WORDç¨åºä¸å¯è§
appWord.Visible = false;
//ä¸å¼¹åºè¦åæ¡
appWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
//å ä¿åé»è®¤çæå°æº
string defaultPrinter = appWord.ActivePrinter;
//æå¼è¦æå°çæ件
Word.Document doc = appWord.Documents.Open(
ref wordFile,
ref oMissing,
ref oTrue,
ref oFalse,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing);
//设置æå®çæå°æº
appWord.ActivePrinter = "æå®æå°æºçåå";
//æå°
doc.PrintOut(
ref oTrue, //æ¤å¤ä¸ºtrue,表示åå°æå°
ref oFalse,
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
);
//æå°å®å ³éWORDæ件
doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
//è¿ååæ¥çé»è®¤æå°æº
appWord.ActivePrinter = defaultPrinter;
//éåºWORDç¨åº
appWord.Quit(ref oMissing, ref oMissing, ref oMissing);
doc = null;
appWord = null;
æ¤æ¹æ³ï¼äºCOM交äºï¼æ§è½æäºæ失ï¼è¦æ³¨æCOM对象çéæ¾ï¼ä»¥åå¼å¸¸æ§å¶ï¼æ¤å¤å¿½ç¥ã
éç¨æå®çæå°æºæå°OFFICEæ件
æ¤æ¹æ³åå为 âæ¾ç¤ºç¸åºçç¨åºçªå£â å âä¸æ¾ç¤ºç¸åºçç¨åºçªå£â两ç§æ¹å¼ã
(1) æ¾ç¤ºWORDãEXCELçç¨åºçªå£
éç¨æä½ç³»ç»èªèº«çèªå¨è¯å«æ¨¡å¼çæå°ï¼æ¤æ¹æ³å®é éç¨äºNå¤ç§æ件ï¼å¹¶ä¸éäºWORD,EXCEL,PDFä¹ç±»çï¼ä½æ¯è¿ç§æ¹æ³ï¼æä¸ä¸ªç¼ºé·ï¼å°±æ¯ï¼å¯¹äºæäºç±»åçææ¡£ï¼å¦WORD,EXCEL,PDFçï¼æå°æ¶ï¼ä¼æç¸åºçç¨åºçªå£ä¸éªèè¿ã
å®ç°ä»£ç å¦ä¸ï¼
System.Diagnostics.Process p = new System.Diagnostics.Process();
//ä¸ç°å®è°ç¨ç¨åºçªå£,ä½æ¯å¯¹äºæäºåºç¨æ æ
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//éç¨æä½ç³»ç»èªå¨è¯å«ç模å¼
p.StartInfo.UseShellExecute = true;
//è¦æå°çæ件路å¾ï¼å¯ä»¥æ¯WORD,EXCEL,PDF,TXTçç
p.StartInfo.FileName = @"d:\a.doc";
//æå®æ§è¡çå¨ä½ï¼æ¯æå°ï¼å³printï¼æå¼æ¯ open
p.StartInfo.Verb = "print";
//å¼å§
p.Start();
æ¤ç§æ¹æ³ï¼ä»£ç ç®åï¼æ§è½å¥½ï¼å¯é 稳å®ãæ¤ç§æ¹å¼ï¼å¦æè¦æå®æå°æºï¼ååªè½å©ç¨è®¾ç½®é»è®¤æå°æºçæ¹å¼æ¥å®ç°ã
C#设置系ç»é»è®¤æå°æºçå®ç°æ¹æ³ï¼è§ âC#è·åå设置系ç»çé»è®¤æå°æºâ ä¸æ
æ·»å äºæå®æå°æºåè½ç代ç å¦ä¸ï¼
System.Diagnostics.Process p = new System.Diagnostics.Process();
//ä¸ç°å®è°ç¨ç¨åºçªå£,ä½æ¯å¯¹äºæäºåºç¨æ æ
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//éç¨æä½ç³»ç»èªå¨è¯å«ç模å¼
p.StartInfo.UseShellExecute = true;
//è¦æå°çæ件路å¾
p.StartInfo.FileName = @"d:\a.doc";
//æå®æ§è¡çå¨ä½ï¼æ¯æå°ï¼å³printï¼æå¼æ¯ open
p.StartInfo.Verb = "print";
//è·åå½åé»è®¤æå°æº
//string defaultPrinter = GetDefaultPrinter();
//å°æå®çæå°æºè®¾ä¸ºé»è®¤æå°æº
SetDefaultPrinter("æå®çæå°æº");
//å¼å§æå°
p.Start();
//çå¾ åç§
p.WaitForExit(10000);
//å°é»è®¤æå°æºè¿å
SetDefaultPrinter(defaultPrinter);
(2) ä¸æ¾ç¤ºWORDãEXCELçç¨åºçªå£
æ¤ç§æ¹å¼ï¼ä½¿ç¨.NETè°ç¨COMçæ¹å¼æ¥å®ç°ï¼å©ç¨COM对象æ¬èº«çç¹æ§æ¥è®¾ç½®å¯è§æ§åæå°æº
使ç¨æ¤æ¹æ³åï¼éè¦å æ·»å OfficeçCOMå¼ç¨ï¼è¿éç¥è¿ã
//è¦æå°çæ件路å¾
object wordFile = @"d:\a.doc";
object oMissing = Missing.Value;
//èªå®ä¹objectç±»åçå¸å°å¼
object oTrue = true;
object oFalse = false;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//å®ä¹WORD Applicationç¸å ³
Word.Application appWord = new Word.Application();
//WORDç¨åºä¸å¯è§
appWord.Visible = false;
//ä¸å¼¹åºè¦åæ¡
appWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
//å ä¿åé»è®¤çæå°æº
string defaultPrinter = appWord.ActivePrinter;
//æå¼è¦æå°çæ件
Word.Document doc = appWord.Documents.Open(
ref wordFile,
ref oMissing,
ref oTrue,
ref oFalse,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing);
//设置æå®çæå°æº
appWord.ActivePrinter = "æå®æå°æºçåå";
//æå°
doc.PrintOut(
ref oTrue, //æ¤å¤ä¸ºtrue,表示åå°æå°
ref oFalse,
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
);
//æå°å®å ³éWORDæ件
doc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
//è¿ååæ¥çé»è®¤æå°æº
appWord.ActivePrinter = defaultPrinter;
//éåºWORDç¨åº
appWord.Quit(ref oMissing, ref oMissing, ref oMissing);
doc = null;
appWord = null;
æ¤æ¹æ³ï¼äºCOM交äºï¼æ§è½æäºæ失ï¼è¦æ³¨æCOM对象çéæ¾ï¼ä»¥åå¼å¸¸æ§å¶ï¼æ¤å¤å¿½ç¥ã
温馨提示:答案为网友推荐,仅供参考