C#操作office方面的总结 Excel和Word

本文介绍了使用C#进行Excel和Word文件的操作方法。对于Excel,包括从文件导入数据到DataSet,以及从DataSet导出数据到Excel文件。对于Word,则涉及创建、编辑文档,以及从Word文档生成HTML文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#操作Excel!
None.gif      public   class  ImportExportToExcel
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string strConn ;
InBlock.gif        
InBlock.gif        
private System.Windows.Forms.OpenFileDialog openFileDlg=new System.Windows.Forms.OpenFileDialog();
InBlock.gif        
private System.Windows.Forms.SaveFileDialog saveFileDlg=new System.Windows.Forms.SaveFileDialog();      
InBlock.gif        
InBlock.gif        
public ImportExportToExcel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            this.openFileDlg.DefaultExt = "xls";
InBlock.gif            
this.openFileDlg.Filter = "Excel文件 (*.xls)|*.xls";
InBlock.gif
InBlock.gif            
this.saveFileDlg.DefaultExt="xls";
InBlock.gif            
this.saveFileDlg.Filter= "Excel文件 (*.xls)|*.xls";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
从Excel文件导入到DataSet#region 从Excel文件导入到DataSet
InBlock.gif        
//        /// <summary>
InBlock.gif        
//        /// 从Excel导入文件
InBlock.gif        
//        /// </summary>
InBlock.gif        
//        /// <param name="strExcelFileName">Excel文件名</param>
InBlock.gif        
//        /// <returns>返回DataSet</returns>
InBlock.gif        
//        public DataSet ImportFromExcel(string strExcelFileName)
InBlock.gif        
//        {
InBlock.gif        
//            return doImport(strExcelFileName);
InBlock.gif        
//        }
ExpandedSubBlockStart.gifContractedSubBlock.gif
        /**//// <summary>
InBlock.gif        
/// 从选择的Excel文件导入
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet</returns>

InBlock.gif        public DataSet ImportFromExcel()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            
if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
InBlock.gif                ds
=doImport(openFileDlg.FileName);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从指定的Excel文件导入
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strFileName">Excel文件名</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataSet ImportFromExcel(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            ds
=doImport(strFileName);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行导入
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strFileName">文件名</param>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet</returns>

InBlock.gif        private DataSet doImport(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (strFileName==""return null;
InBlock.gif              
InBlock.gif            strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
InBlock.gif                
"Data Source=" +  strFileName + ";" +
InBlock.gif                
"Extended Properties=Excel 8.0;";
InBlock.gif            OleDbDataAdapter ExcelDA 
= new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
InBlock.gif
InBlock.gif            DataSet ExcelDs 
= new DataSet();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ExcelDA.Fill(ExcelDs, 
"ExcelInfo");
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Console.WriteLine( err.ToString() );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return ExcelDs;
InBlock.gif            
InBlock.gif            
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
从DataSet到出到Excel#region 从DataSet到出到Excel
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 导出指定的Excel文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ds">要导出的DataSet</param>
ExpandedSubBlockEnd.gif        
/// <param name="strExcelFileName">要导出的Excel文件名</param>

InBlock.gif        public void ExportToExcel(DataSet ds,string strExcelFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (ds.Tables.Count==0 || strExcelFileName==""return;
InBlock.gif            doExport(ds,strExcelFileName);
InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 导出用户选择的Excel文件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="ds">DataSet</param>

InBlock.gif        public void ExportToExcel(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
InBlock.gif                doExport(ds,saveFileDlg.FileName);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行导出
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ds">要导出的DataSet</param>
ExpandedSubBlockEnd.gif        
/// <param name="strExcelFileName">要导出的文件名</param>

InBlock.gif        private void doExport(DataSet ds,string strExcelFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            Excel.Application excel
= new Excel.Application();
InBlock.gif            
InBlock.gif            
//            Excel.Workbook obj=new Excel.WorkbookClass();
InBlock.gif            
//            obj.SaveAs("c:\zn.xls",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
InBlock.gif

InBlock.gif            
int rowIndex=1;
InBlock.gif            
int colIndex=0;
InBlock.gif
InBlock.gif            excel.Application.Workbooks.Add(
true);
InBlock.gif            
InBlock.gif    
InBlock.gif            System.Data.DataTable table
=ds.Tables[0] ;
InBlock.gif            
foreach(DataColumn col in table.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                colIndex
++;    
InBlock.gif                excel.Cells[
1,colIndex]=col.ColumnName;                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
foreach(DataRow row in table.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rowIndex
++;
InBlock.gif                colIndex
=0;
InBlock.gif                
foreach(DataColumn col in table.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    colIndex
++;
InBlock.gif                    excel.Cells[rowIndex,colIndex]
=row[col.ColumnName].ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            excel.Visible
=false;    
InBlock.gif            excel.Sheets[
0= "sss";
InBlock.gif            excel.ActiveWorkbook.SaveAs(strExcelFileName
+".XLS",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
InBlock.gif            
InBlock.gif            
InBlock.gif            
//wkbNew.SaveAs strBookName
InBlock.gif
InBlock.gif
InBlock.gif            
//excel.Save(strExcelFileName);
InBlock.gif
            excel.Quit();
InBlock.gif            excel
=null;
InBlock.gif            
InBlock.gif            GC.Collect();
//垃圾回收
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
从XML导入到Dataset#region 从XML导入到Dataset
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从选择的XML文件导入
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet</returns>

InBlock.gif        public DataSet ImportFromXML()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            System.Windows.Forms.OpenFileDialog openFileDlg
=new System.Windows.Forms.OpenFileDialog();
InBlock.gif            openFileDlg.DefaultExt
="xml";
InBlock.gif            openFileDlg.Filter
= "xml文件 (*.xml)|*.xml";
InBlock.gif            
if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
trydot.gif{ds.ReadXml(openFileDlg.FileName,System.Data.XmlReadMode.ReadSchema);}
ExpandedSubBlockStart.gifContractedSubBlock.gif                
catchdot.gif{}
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从指定的XML文件导入
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strFileName">XML文件名</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataSet ImportFromXML(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (strFileName=="")
InBlock.gif                
return null;
InBlock.gif            DataSet ds
=new DataSet();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
trydot.gif{ds.ReadXml(strFileName,System.Data.XmlReadMode.ReadSchema);}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catchdot.gif{}
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
从DataSet导出到XML#region 从DataSet导出到XML
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 导出指定的XML文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ds">要导出的DataSet</param>
ExpandedSubBlockEnd.gif        
/// <param name="strXMLFileName">要导出的XML文件名</param>

InBlock.gif        public void ExportToXML(DataSet ds,string strXMLFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (ds.Tables.Count==0 || strXMLFileName==""return;
InBlock.gif            doExportXML(ds,strXMLFileName);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 导出用户选择的XML文件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="ds">DataSet</param>

InBlock.gif        public void ExportToXML(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Windows.Forms.SaveFileDialog saveFileDlg
=new System.Windows.Forms.SaveFileDialog(); 
InBlock.gif            saveFileDlg.DefaultExt
="xml";
InBlock.gif            saveFileDlg.Filter
= "xml文件 (*.xml)|*.xml";
InBlock.gif            
if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
InBlock.gif                doExportXML(ds,saveFileDlg.FileName);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行导出
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ds">要导出的DataSet</param>
ExpandedSubBlockEnd.gif        
/// <param name="strExcelFileName">要导出的XML文件名</param>

InBlock.gif        private void doExportXML(DataSet ds,string strXMLFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{ds.WriteXml(strXMLFileName,System.Data.XmlWriteMode.WriteSchema );}
InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{System.Windows.Forms.MessageBox.Show(ex.Message,"Errol") ;}    
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif    
InBlock.gif    
ExpandedBlockEnd.gif    }









C#操作Word!
None.gif namespace  ImportExportToOffice
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif    
using System.ComponentModel;
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Windows.Forms;
InBlock.gif    
public class ImportExportToWord
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Word.ApplicationClass oWordApplic;    
InBlock.gif        
private Word.Document oDoc;        
InBlock.gif        
private const string strFileName    = @"F:\";
InBlock.gif        
private const string PostfixForWord    = @".doc";
InBlock.gif        
private const string PostfixForHtml    = @".Html";
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
InBlock.gif        
public ImportExportToWord()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic 
= new Word.ApplicationClass();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
public Word.Document Document
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.oDoc;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Word.ApplicationClass Application
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.oWordApplic;
ExpandedSubBlockEnd.gif            }
    
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有方法#region 私有方法
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置Word文档是否可视
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="InEnabled">boolean</param>

InBlock.gif        private void SetVisible( Boolean InEnabled )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Visible 
= InEnabled;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在垃圾回收时,在任务管理器中还存在当前操作的WORD的进程
InBlock.gif        
/// 查阅资料,必须在另一个方法中在调用GC才可以真正的清楚掉,当前的进程
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void GCForQuit()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing = System.Reflection.Missing.Value;
InBlock.gif            oWordApplic.Application.Quit( 
ref missing, ref missing, ref missing );    
InBlock.gif            
if ( oDoc != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
InBlock.gif                oDoc 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( oWordApplic != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic);
InBlock.gif                oWordApplic 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            GC.Collect();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回保存文件的FileName
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strDefaultExt">要保存文件的类型</param>
InBlock.gif        
/// <param name="strFilter">文件名筛选器字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>保存的路径</returns>

InBlock.gif        private string SaveFileName( string strDefaultExt, string strFilter )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string fileName = "";
InBlock.gif            System.Windows.Forms.SaveFileDialog saveFileDlg 
= new System.Windows.Forms.SaveFileDialog(); 
InBlock.gif            saveFileDlg.DefaultExt    
= strDefaultExt;
InBlock.gif            saveFileDlg.Filter        
= strFilter;
InBlock.gif            
if ( saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) 
InBlock.gif                fileName 
= saveFileDlg.FileName;
InBlock.gif            
return fileName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将被 SaveFileName 取代
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string SaveFileToHtmlForName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string fileName = "";
InBlock.gif            System.Windows.Forms.SaveFileDialog saveFileDlg 
= new System.Windows.Forms.SaveFileDialog(); 
InBlock.gif            saveFileDlg.DefaultExt    
= "Html";
InBlock.gif            saveFileDlg.Filter        
= "html文件 (*.html)|*.htm";
InBlock.gif            
if ( saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) 
InBlock.gif                fileName 
= saveFileDlg.FileName;
InBlock.gif            
return fileName;
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 保存
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void Save( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oDoc.Save();            
ExpandedSubBlockEnd.gif        }
        
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公有方法#region 公有方法
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 打开一个空的Word模板
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool Open( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = false;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object missing    = System.Reflection.Missing.Value;
InBlock.gif                oDoc            
= oWordApplic.Documents.Add( ref missing, ref missing, ref missing, ref missing );
InBlock.gif                oDoc.Activate(); 
InBlock.gif                result 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Quit();
InBlock.gif                
//throw ( new Exception() );
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 退出
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Quit( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            GCForQuit();
InBlock.gif            GC.Collect();
InBlock.gif            
foreach(System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(p.ProcessName.ToUpper() == "WINWORD")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    p.Kill();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 打开指定的Word文档
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strFileName">指定的Word文档</param>

InBlock.gif        public bool Open( string strFileName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.Open( strFileName, true );
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 打开指定的Word文档并判断是否显示
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strFileName">指定的Word文档</param>
ExpandedSubBlockEnd.gif        
/// <param name="isEnabled">显示与否</param>

InBlock.gif        public bool Open( string strFileName, bool isEnabled  )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = false;
InBlock.gif            
if ( strFileName == null || strFileName == "" ) return result;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object fileName        = strFileName;
InBlock.gif                
object readOnly        = false;
InBlock.gif                
object isVisible    = true;
InBlock.gif                
object missing        = System.Reflection.Missing.Value;
InBlock.gif            
#if OFFICEXP
InBlock.gif            oDoc                
= oWordApplic.Documents.Open(ref fileName, ref missing,ref readOnly, 
InBlock.gif                                    
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
InBlock.gif                                    
ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing);
InBlock.gif            
#else
InBlock.gif                oDoc                
= oWordApplic.Documents.Open(ref fileName,  ref missing,ref readOnly, 
InBlock.gif                    
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
InBlock.gif                    
ref missing, ref missing, ref isVisible);
InBlock.gif            
#endif
InBlock.gif                oDoc.Activate();        
InBlock.gif                oWordApplic.Visible 
= isEnabled;
InBlock.gif                result 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Quit();
InBlock.gif                
//throw ( new Exception() );
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 另存
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool SaveAs( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing            = System.Reflection.Missing.Value;
InBlock.gif            
object fileName            = SaveFileName( "doc""doc文件 (*.doc)|*.doc" );
InBlock.gif            
return this.SaveAs( Convert.ToString( fileName ) );
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 另存
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strFileName"></param>

InBlock.gif        public bool SaveAs( string strFileName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = false;
InBlock.gif            
if ( strFileName == null || strFileName == "" ) return result;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object missing    = System.Reflection.Missing.Value;
InBlock.gif                
object fileName = strFileName ;
InBlock.gif            
#if OFFICEXP
InBlock.gif                oDoc.SaveAs( 
ref fileName, ref missing,ref missing, ref missing,ref missing,ref missing,ref missing,
InBlock.gif                            
ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing );
InBlock.gif            
#else
InBlock.gif                oDoc.SaveAs( 
ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing );
InBlock.gif            
#endif
InBlock.gif                result 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//throw( new Exception() );
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Quit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 把Word文档装化为Html文件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strFileName">要转换的Word文档</param>

InBlock.gif        public bool WordToHtml( string strFileNameForWord )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string saveFileName = strFileName + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"+ PostfixForHtml;
InBlock.gif            
return this.WordToHtml( strFileNameForWord, saveFileName );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 把Word文档装化为Html文件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strFileName">要转换的Word文档</param>
ExpandedSubBlockEnd.gif        
/// <param name="strSaveFileName">要生成的具体的Html页面</param>

InBlock.gif        public bool WordToHtml( string strFileNameForWord, string strSaveFileName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = false;
InBlock.gif            
if ( strFileNameForWord == null || strFileNameForWord == "" ) return result;
InBlock.gif            
if ( this.Open( strFileNameForWord, false ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Type wordType 
= oWordApplic.GetType();
InBlock.gif                    
// 打开文件
InBlock.gif
                    Type docsType = oWordApplic.Documents.GetType();
InBlock.gif                    
// 转换格式,另存为
InBlock.gif
                    Type docType = oDoc.GetType();
InBlock.gif                    
object saveFileName = strSaveFileName;
ExpandedSubBlockStart.gifContractedSubBlock.gif                    docType.InvokeMember( 
"SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, oDoc, new object[]dot.gif{ saveFileName, Word.WdSaveFormat.wdFormatHTML } );
ContractedSubBlock.gifExpandedSubBlockStart.gif                    
其它格式:#region 其它格式:
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
/**////wdFormatHTML
InBlock.gif                    
///wdFormatDocument
InBlock.gif                    
///wdFormatDOSText
InBlock.gif                    
///wdFormatDOSTextLineBreaks
InBlock.gif                    
///wdFormatEncodedText
InBlock.gif                    
///wdFormatRTF
InBlock.gif                    
///wdFormatTemplate
InBlock.gif                    
///wdFormatText
InBlock.gif                    
///wdFormatTextLineBreaks
ExpandedSubBlockEnd.gif                    
///wdFormatUnicodeText

InBlock.gif                    //-----------------------------------------------------------------------------------
InBlock.gif                    
//            docType.InvokeMember( "SaveAs", System.Reflection.BindingFlags.InvokeMethod,
InBlock.gif                    
//                null, oDoc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML} );
InBlock.gif                    
// 退出 Word
InBlock.gif                    
//wordType.InvokeMember( "Quit", System.Reflection.BindingFlags.InvokeMethod,
InBlock.gif                    
//    null, oWordApplic, null );
ExpandedSubBlockEnd.gif
                    #endregion

InBlock.gif                    result 
= true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//throw ( new Exception() );
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Quit();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入文本操作,所有的打开与保存操作在外部执行
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strText"></param>

InBlock.gif        public void InsertText( string strText )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.TypeText( strText );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertText( string strText, int iNum )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for ( int i = 0; i < iNum; i++ )
InBlock.gif                
this.InsertText( strText );
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入文本操作
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strText">要保存的字符串</param>

InBlock.gif        public bool InsertTextBySelection( string strText )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strsavefilename = strFileName + DateTime.Now.ToString("yyyyMMddHHmmss"+ PostfixForWord;
InBlock.gif            
return this.InsertTextBySelection( strText, strsavefilename );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strText">要保存的字符串</param>
ExpandedSubBlockEnd.gif        
/// <param name="strSaveFileName">保存后的doc文件名</param>

InBlock.gif        public bool InsertTextBySelection( string strText, string strSaveFileName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.InsertTextBySelection( strText, strSaveFileName, 202000"华文仿宋" );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strText">要保存的字符串</param>
InBlock.gif        
/// <param name="strSaveFileName">保存后的doc文件名</param>
InBlock.gif        
/// <param name="leftindent">首行缩近多少</=param>
InBlock.gif        
/// <param name="size">字体大小</param>
InBlock.gif        
/// <param name="boldbi">是否粗体;1 yes, 0 no</param>
ExpandedSubBlockEnd.gif        
/// <param name="paragraphalignment">对齐方式</param>

InBlock.gif        public bool InsertTextBySelection( string strText, string strSaveFileName, float firstlineindent , int size, int boldbi,int paragraphalignment, string fontname )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = false;
InBlock.gif            
if ( strText == "" || strText == null ) return result;
InBlock.gif            
if ( this.Open() )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string[] strvalue = strText.Split( '$');
InBlock.gif                    
if ( strvalue.Length != 0  )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        oWordApplic.Selection.TypeText( 
"今天是:"+ DateTime.Now.ToString("yyyy-MM-dd") );
InBlock.gif                        Word.Paragraph para 
= this.GoToFirstParagraph();
InBlock.gif                        para.Range.Select();
InBlock.gif                        
this.SetFontName( fontname );
InBlock.gif                        
this.SetFirstLineIndent( firstlineindent );
InBlock.gif                        
this.SetFontSize( 20 );
InBlock.gif                        
this.setBoldBi( boldbi );
InBlock.gif                        
switch ( paragraphalignment )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
case 0 :
InBlock.gif                                
this.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphLeft);
InBlock.gif                                
break;
InBlock.gif                            
case 1 :
InBlock.gif                                
this.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphCenter);
InBlock.gif                                
break;
InBlock.gif                            
case 2:
InBlock.gif                                
this.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphRight);
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
int j = 0;
InBlock.gif                        
forint i= 0; i < strvalue.Length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
InBlock.gif                            
this.InsertParagraphAfterByParagraph(para,1);    //加1个空行
InBlock.gif
                            para = this.GoToEndParagraph();                 //定位到第1空行
InBlock.gif
                            this.InsertTextBeforeByParagraph( para, strvalue[i] );
InBlock.gif                            para 
= this.GoToEndParagraph();
InBlock.gif                            para.Range.Select();
InBlock.gif                            
this.setBoldBi( boldbi );                        //设置标题非粗体字
InBlock.gif
                            this.SetFontSize( size );                        //设置字体大小
InBlock.gif
                            this.SetFontName( fontname );                    //设置字体
InBlock.gif
                            j++;
InBlock.gif                            
if ( j == 9 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
this.InsertText( "━━━━"6 );
InBlock.gif                                j 
= 0;
ExpandedSubBlockEnd.gif                            }

InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
if ( this.SaveAs( strSaveFileName ) )
InBlock.gif                            result 
= true;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Quit();
InBlock.gif                    
//throw( new Exception() );
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入空行
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void InsertLineBreakBySelection( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.TypeParagraph();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 插入指定的空行
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="nline">行数</param>

InBlock.gif        public void InsertLineBreakBySelection( int nline )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for ( int i = 0; i < nline; i++ )
InBlock.gif                
this.InsertLineBreakBySelection();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 换页
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void InsertPagebreak()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object pBreak= (int)Word.WdBreakType.wdPageBreak;
InBlock.gif            oWordApplic.Selection.InsertBreak( 
ref pBreak );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertTextBeforeByParagraph( Word.Paragraph paragraph, string strText )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            paragraph.Range.InsertBefore( strText );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertTextAfterByParagraph( Word.Paragraph paragraph, string strText )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            paragraph.Range.InsertAfter( strText );
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertParagraphBeforeByParagraph( Word.Paragraph paragraph )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            paragraph.Range.InsertParagraphBefore();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertParagraphBeforeByParagraph( Word.Paragraph paragraph,int nLine )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for ( int i = 0; i < nLine; i++ )
InBlock.gif                paragraph.Range.InsertParagraphBefore();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertParagraphAfterByParagraph( Word.Paragraph paragraph )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            paragraph.Range.InsertParagraphAfter();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void InsertParagraphAfterByParagraph( Word.Paragraph paragraph, int nLine )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for ( int i = 0; i < nLine; i++ )
InBlock.gif                paragraph.Range.InsertParagraphAfter();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 数据集转换 即把DataSet转换为Word对象 
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="ds"></param>

InBlock.gif        public bool DataSetToWord( DataSet ds, string strFileName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool result = false;
InBlock.gif            
if ( ds == null ) return result;
InBlock.gif            
if ( strFileName == null || strFileName == "" ) return result;
InBlock.gif            
if ( this.Open() )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Word.Range para 
= oWordApplic.Application.Selection.Paragraphs.Item(1).Range;
InBlock.gif                    
object tableBehavior    =System.Reflection.Missing.Value;
InBlock.gif                    
object autoFitBehavior    =System.Reflection.Missing.Value;
InBlock.gif                    oDoc.Tables.Add( para,
InBlock.gif                        ds.Tables[
0].Rows.Count+1,    //多的一行用来表示表列
InBlock.gif
                        ds.Tables[0].Columns.Count,
InBlock.gif                        
ref tableBehavior,
InBlock.gif                        
ref autoFitBehavior );
InBlock.gif
InBlock.gif                    
//填充Word表格的列标
InBlock.gif
                    forint intCol = 0; intCol < ds.Tables[0].Columns.Count; intCol++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        oDoc.Tables.Item(
1).Cell( 1, intCol+1 ).Range.InsertBefore( ds.Tables[0].Columns[intCol].ColumnName.Trim() );
ExpandedSubBlockEnd.gif                    }

InBlock.gif                
InBlock.gif                    
//填充Word表格的内容
InBlock.gif
                    forint intRow = 0; intRow < ds.Tables[0].Rows.Count; intRow++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
forint intCol = 0; intCol < ds.Tables[0].Columns.Count; intCol++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            oDoc.Tables.Item(
1).Cell( intRow+2, intCol+1 ).Range.InsertBefore( ds.Tables[0].Rows[intRow][intCol].ToString().Trim() );
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
//保存
InBlock.gif
                    if ( this.SaveAs( strFileName ) )
InBlock.gif                        result 
= true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Quit();
InBlock.gif                    
//throw ( new Exception() );
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 采用默认地址保存
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="ds"></param>

InBlock.gif        public bool DataSetToWord( DataSet ds )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.DataSetToWord( ds, strFileName + ds.Tables[0].TableName.ToString() + PostfixForWord );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 段落的对齐方式
InBlock.gif        
/// 例如:word.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphCenter)
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="alignment"></param>

InBlock.gif        public void SetAlignment( Word.WdParagraphAlignment alignment )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.ParagraphFormat.Alignment 
= alignment;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 首行缩进
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="fltCount">float类型的数值</param>

InBlock.gif        public void SetFirstLineIndent( float fltCount )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.ParagraphFormat.FirstLineIndent 
= fltCount;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 左缩进
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="fltCount">float类型的数值</param>

InBlock.gif        public void SetLeftIndent( float fltCount )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.ParagraphFormat.LeftIndent 
= fltCount;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 右缩进
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="fltCount">float类型的数值</param>

InBlock.gif        public void SetRightIndent(float fltCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.ParagraphFormat.RightIndent 
= fltCount;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置字体类型
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strType"></param>

InBlock.gif        public void SetFont( string strType )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
switch (strType)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "Bold":
InBlock.gif                    oWordApplic.Selection.Font.Bold 
= 1;
InBlock.gif                    
break;
InBlock.gif                
case "Italic":
InBlock.gif                    oWordApplic.Selection.Font.Italic 
= 1;
InBlock.gif                    
break;
InBlock.gif                
case "Underlined":
InBlock.gif                    oWordApplic.Selection.Font.Subscript 
= 0;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置默认字体
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void SetFont( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.Bold            
= 0;
InBlock.gif            oWordApplic.Selection.Font.Italic        
= 0;
InBlock.gif            oWordApplic.Selection.Font.Subscript    
= 0;
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置字体名称
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strType"></param>

InBlock.gif        public void SetFontName( string strType )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.Name 
= strType;
InBlock.gif            
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置字体颜色
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="Color"></param>

InBlock.gif        public void SetFontColor( Word.WdColor Color)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.Color 
= Color;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置字体大小
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="nSize"></param>

InBlock.gif        public void SetFontSize( int nSize )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.Size 
= nSize;
InBlock.gif            
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置是否有粗体,0->否 ,1->是
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="intBoldBi"></param>

InBlock.gif        public void setBoldBi( int intBoldBi )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.BoldBi 
= intBoldBi;
InBlock.gif            
//oWordApplic.Selection.Font.Bold = intBoldBi;
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public void SetBoldSize( int intBold )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.Bold 
= intBold;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void SetUnderLine( Word.WdUnderline underLine )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.Underline 
= underLine; 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void SetUnderLineColor( Word.WdColor Color )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            oWordApplic.Selection.Font.UnderlineColor 
= Color;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定位到书签
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="strBookMarkName"></param>

InBlock.gif        public void GotoBookMark( string strBookMarkName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing        = System.Reflection.Missing.Value;
InBlock.gif            
object Bookmark        = (int)Word.WdGoToItem.wdGoToBookmark;
InBlock.gif            
object NameBookMark = strBookMarkName;
InBlock.gif            oWordApplic.Selection.GoTo( 
ref Bookmark, ref missing, ref missing,ref NameBookMark );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定位到文档开头
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void GoToTheBeginning( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing    = System.Reflection.Missing.Value;
InBlock.gif            
object unit;
InBlock.gif            unit            
= Word.WdUnits.wdStory ;
InBlock.gif            oWordApplic.Selection.HomeKey ( 
ref unit, ref missing );
InBlock.gif            
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定位到文档结尾
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void GoToTheEnd( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing    = System.Reflection.Missing.Value;
InBlock.gif            
object unit;
InBlock.gif            unit            
= Word.WdUnits.wdStory ;
InBlock.gif            oWordApplic.Selection.EndKey ( 
ref unit, ref missing );
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定位到首段
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public Word.Paragraph  GoToFirstParagraph()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.oWordApplic.Selection.Paragraphs.First;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 定位到尾段
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public Word.Paragraph GoToEndParagraph()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.oWordApplic.Selection.Paragraphs.Last ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 向后定位到指定段落
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="para"></param>
ExpandedSubBlockEnd.gif        
/// <param name="count"></param>

InBlock.gif        public void GoToNextParagraph(ref Word.Paragraph para,ref object count)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            para.Next(
ref count) ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 向前定位到指定段落
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="para"></param>
ExpandedSubBlockEnd.gif        
/// <param name="count"></param>

InBlock.gif        public void GoToPreviousParagraph( ref Word.Paragraph para, ref object count )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            para.Previous( 
ref count ) ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void GoToTheTable( int ntable )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing    = System.Reflection.Missing.Value;
InBlock.gif            
object what;
InBlock.gif            what            
= Word.WdUnits.wdTable ;
InBlock.gif            
object which;
InBlock.gif            which            
= Word.WdGoToDirection.wdGoToFirst;
InBlock.gif            
object count;
InBlock.gif            count            
= 1 ;
InBlock.gif            oWordApplic.Selection.GoTo( 
ref what, ref which, ref count, ref missing );
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
public void GoToRightCell( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing    = System.Reflection.Missing.Value;
InBlock.gif            
object direction;
InBlock.gif            direction        
= Word.WdUnits.wdCell;
InBlock.gif            oWordApplic.Selection.MoveRight( 
ref direction, ref missing, ref missing );
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
public void GoToLeftCell( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing    = System.Reflection.Missing.Value;
InBlock.gif            
object direction;
InBlock.gif            direction        
= Word.WdUnits.wdCell;
InBlock.gif            oWordApplic.Selection.MoveLeft( 
ref direction, ref missing, ref missing );
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
public void GoToDownCell( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing = System.Reflection.Missing.Value;
InBlock.gif            
object direction;
InBlock.gif            direction 
= Word.WdUnits.wdLine;
InBlock.gif            oWordApplic.Selection.MoveDown( 
ref direction, ref missing, ref missing );
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
public void GoToUpCell( )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object missing    = System.Reflection.Missing.Value;
InBlock.gif            
object direction;
InBlock.gif            direction        
= Word.WdUnits.wdLine;
InBlock.gif            oWordApplic.Selection.MoveUp( 
ref direction, ref missing, ref missing );
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
public Boolean ExecuteReplace( Word.Find find )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ExecuteReplace( find, Word.WdReplace.wdReplaceAll );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Boolean ExecuteReplace( Word.Find find, Object replaceOption )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Simple wrapper around Find.Execute:
InBlock.gif
            Object findText = Type.Missing;
InBlock.gif            Object matchCase 
= Type.Missing;
InBlock.gif            Object matchWholeWord 
= Type.Missing;
InBlock.gif            Object matchWildcards 
= Type.Missing;
InBlock.gif            Object matchSoundsLike 
= Type.Missing;
InBlock.gif            Object matchAllWordForms 
= Type.Missing;
InBlock.gif            Object forward 
= Type.Missing;
InBlock.gif            Object wrap 
= Type.Missing;
InBlock.gif            Object format 
= Type.Missing;
InBlock.gif            Object replaceWith 
= Type.Missing;
InBlock.gif            Object replace 
= replaceOption;
InBlock.gif            Object matchKashida 
= Type.Missing;
InBlock.gif            Object matchDiacritics 
= Type.Missing;
InBlock.gif            Object matchAlefHamza 
= Type.Missing;
InBlock.gif            Object matchControl 
= Type.Missing;
InBlock.gif      
InBlock.gif            
return find.Execute( ref findText, ref matchCase, ref matchWholeWord, 
InBlock.gif                
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms, 
InBlock.gif                
ref forward, ref wrap, ref format,    ref replaceWith, ref replace, 
InBlock.gif                
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, 
InBlock.gif                
ref matchControl );
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
public Boolean ExecuteFind( Word.Find find )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ExecuteFind( find, find.Text, Type.Missing, Type.Missing );
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public Boolean ExecuteFind( Word.Find find, string strFindText )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ExecuteFind( find, strFindText, Type.Missing, Type.Missing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Boolean ExecuteFind(
InBlock.gif            Word.Find find, 
string strFindText, Object wrapFind, Object forwardFind )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Simple wrapper around Find.Execute:
InBlock.gif
            
InBlock.gif            Object findText ;
InBlock.gif            Object matchCase 
= Type.Missing;
InBlock.gif            Object matchWholeWord 
= Type.Missing;
InBlock.gif            Object matchWildcards 
= Type.Missing;
InBlock.gif            Object matchSoundsLike 
= Type.Missing;
InBlock.gif            Object matchAllWordForms 
= Type.Missing;
InBlock.gif            Object forward 
= forwardFind;
InBlock.gif            Object wrap 
= wrapFind;
InBlock.gif            Object format 
= Type.Missing;
InBlock.gif            Object replaceWith 
= Type.Missing;
InBlock.gif            Object replace 
= Type.Missing;
InBlock.gif            Object matchKashida 
= Type.Missing;
InBlock.gif            Object matchDiacritics 
= Type.Missing;
InBlock.gif            Object matchAlefHamza 
= Type.Missing;
InBlock.gif            Object matchControl 
= Type.Missing;
InBlock.gif      
InBlock.gif            
if ( ( strFindText == "" )||( strFindText == string.Empty ) )
InBlock.gif                findText 
= find.Text;
InBlock.gif            
else
InBlock.gif                findText 
= strFindText;
InBlock.gif            find.ClearFormatting();
InBlock.gif            
return find.Execute( ref findText, ref matchCase, ref matchWholeWord, 
InBlock.gif                
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms, 
InBlock.gif                
ref forward, ref wrap, ref format, ref replaceWith, ref replace, 
InBlock.gif                
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, 
InBlock.gif                
ref matchControl );
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif    
InBlock.gif        
public Boolean FindInSelection( Word.Selection Selection, string strFindText ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                        
InBlock.gif            
return this.ExecuteFind( Selection.Find,strFindText,System.Type.Missing,System.Type.Missing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Boolean FindInSelection( Word.Selection Selection, string strFindText, Object wrapFind, Object forwardFind ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                        
InBlock.gif            
return this.ExecuteFind( Selection.Find, strFindText, wrapFind, forwardFind );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Boolean FindInRange( Word.Range range,string strFindText ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Boolean blnReturn  
=this.ExecuteFind( range.Find, strFindText, Type.Missing, Type.Missing );
InBlock.gif            range.Select();
InBlock.gif            
return blnReturn;
ExpandedSubBlockEnd.gif        }

InBlock.gif  
InBlock.gif        
public void FindInAllDocument( string strFindText ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int intFound        = 0;
InBlock.gif            Object start        
= 0;
InBlock.gif            Object end            
= this.oDoc.Characters.Count;
InBlock.gif            Word.Range rngDoc    
= oDoc.Range( ref start, ref end );
InBlock.gif            Word.Find fnd        
= rngDoc.Find;
InBlock.gif            fnd.ClearFormatting();
InBlock.gif            fnd.Forward        
= true;
InBlock.gif            fnd.Text        
= strFindText;
InBlock.gif            ExecuteFind( fnd );
InBlock.gif            
while ( fnd.Found )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                rngDoc.Font.Color    
= Word.WdColor.wdColorRed;
InBlock.gif                rngDoc.Font.Bold    
= 600;
InBlock.gif                intFound
++;
InBlock.gif                ExecuteFind( fnd );
ExpandedSubBlockEnd.gif            }

InBlock.gif            MessageBox.Show( String.Format( 
"lorem found {0} times.", intFound ), "FindInLoopAndFormat" );
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


转: http://www.cnblogs.com/zhanghl/archive/2005/09/19/239595.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值