在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看。当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中,在显示的时,可以在线打开word,也可以把word转换成html标签显示。 这样使用word代替网页的原因有:
  第一:网页生成数学公式和特殊符号存储和显示比较麻烦(如何操作word生成数学公式,有待测试)
  第二:生成Word版的报告更容易存档和没有环境下的传阅及打印
  第三:客户直接操作Word感觉更亲切,而且非常熟悉 
  Msdn上的word操作api(不过只有英文版,英文差的先闪过) 
Word2007的API:http://msdn.microsoft.com/en-us/library/bb257531(v=office.12).aspx
Word2010的API:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word(v=office.14).aspx

  Word对象模型 (.Net Perspective)
  五大对象
Application :代表Microsoft Word应用程序本身
  是Document和Selection的基类。通过Application的属性和方法,我们可以控制Word的大环境。
Document :代表一个Word文档
  当你新建一个Word文档或者打开一个已有的Word文档,你将创建一个Document对象,该对象被加入到Words Documents Collection中。拥有焦点的Document称为ActiveDocument,可以通过Application对象的ActiveDocument属性获得当前文档对象
Selection :代表当前选中的区域(高亮),没有选中区域时代表光标点
  它通常是高亮显示的(例如,你要改变一段文字的字体,你首先得选中这段文字,那么选中的这块区域就是当前文档的Selection对象所包含的区域)
Bookmarks :书签
  1>书签一般有名字
  2>Saved with the document,且文档关闭了之后书签继续存在
  3>书签通常是隐藏的,但也可以通过代码设置其为可见

Range :代表一块区域,与Selection类似,不过一般不可见
  1>包含一个起始位置和一个结束位置
  2>它可以包含光标点,一段文本或者整个文档
  3>它包含空格,tab以及paragraph marks
  4>它可以是当前选中的区域,当然也可以不是当前选中区域
  5>它被动态创建
  6>当你在一个Range的末尾插入文本,这将扩展该Range

  word文档对象的结构图

关于对象的详细使用,可以参考msdn api

  实例使用

  创建Word 文档所使用的主要方法是通过微软公司提供的Microsoft Word X Objec
t Library,其中X 为版本号。Word2010对应14.0, Word 2007 对应12.0,Word 2003 对应11.0。通过在
项目中添加该组件,即可使用微软公司提供的方法创建相应版本的Word 文档。
在实例中我将所要生成word的格式设置为2003版本
如下: object format = MSWord.WdSaveFormat.wdFormatDocument;

  新建一个webForm项目文件, Com组件中添加 Microsoft Word 12.0 Object Library,引用面板中多出Microsoft.Office.Core、Microsoft.Office.Interop.Word两个引用。
  在类文件中添加应用如下:
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Word;
  下面从word创建、格式设置、文本添加、图片添加、表格添加展示部分代码:


void CreateWord()
{
object path;//文件路径
string strContent;//文件内容
MSWord.Application wordApp;//Word应用程序变量
MSWord.Document wordDoc;//Word文档变量
path = "d:\\myWord.doc";//保存为Word2003文档
// path = "d:\\myWord.doc";//保存为Word2007文档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM 库,因此有许多变量需要用Missing.Value 代替
Object Nothing = Missing.Value;
//新建一个word对象
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//WdSaveDocument为Word2003文档的保存格式(文档后缀.doc)\wdFormatDocumentDefault为Word2007的保存格式(文档后缀.docx)
object format = MSWord.WdSaveFormat.wdFormatDocument;
//将wordDoc 文档对象的内容保存为DOC 文档,并保存到path指定的路径
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script>alert('" + path + ": Word文档创建完毕!');</script>");
}
创建Word文档

private void SetWordStyle()
{
object path;//文件路径
string strContent;//文件内容
MSWord.Application wordApp;//Word应用程序变量
MSWord.Document wordDoc;//Word文档变量
path = "d:\\myWord.doc";//保存为Word2003文档
// path = "d:\\myWord.doc";//保存为Word2007文档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//页面设置
wordDoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式
wordDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
wordDoc.PageSetup.TopMargin = 57.0f;
wordDoc.PageSetup.BottomMargin = 57.0f;
wordDoc.PageSetup.LeftMargin = 57.0f;
wordDoc.PageSetup.RightMargin = 57.0f;
wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置
//设置页眉
wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;//视图样式
wordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;//进入页眉设置,其中页眉边距在页面设置中已完成
wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//插入页眉图片(测试结果图片未插入成功)
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
string headerfile = "d:\\header.jpg";
Microsoft.Office.Interop.Word.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing);
shape1.Height = 20;
shape1.Width = 80;
wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" 文档页眉");
//去掉页眉的横线
wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
wordApp.ActiveWindow.ActivePane.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].Visible = false;
wordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;//退出页眉设置
//为当前页添加页码
Microsoft.Office.Interop.Word.PageNumbers pns = wordApp.Selection.Sections[1].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码
pns.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash;
pns.HeadingLevelForChapter = 0;
pns.IncludeChapterNumber = false;
pns.RestartNumberingAtSection = false;
pns.StartingNumber = 0;
object pagenmbetal = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间
object first = true;
wordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first); object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script>alert('" + path + ": Word文档格式设置完毕!');</script>");
}
设置Word文档格式

效果图:


private void AddWordText()
{
object path;//文件路径
string strContent;//文件内容
MSWord.Application wordApp;//Word应用程序变量
MSWord.Document wordDoc;//Word文档变量
path = "d:\\myWord.doc";//保存为Word2003文档
// path = "d:\\myWord.doc";//保存为Word2007文档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Selection.ParagraphFormat.LineSpacing = 35f;//设置文档的行间距
//写入普通文本
wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度
strContent = "c#向Word写入文本 普通文本:\n"; 
wordDoc.Paragraphs.Last.Range.Text = strContent;
//将文档的前三个字替换成"asdfasdf",并将其颜色设为蓝色
object start = 0;
object end = 3;
Microsoft.Office.Interop.Word.Range rang = wordDoc.Range(ref start, ref end);
rang.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBrightGreen;
rang.Text = "我是替换文字";
wordDoc.Range(ref start, ref end);
 
//写入黑体文本
object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.FirstLineIndent = 0;//取消首行缩进的长度
strContent = "黑体文本\n ";//在文本中使用'\n'换行
wordDoc.Paragraphs.Last.Range.Font.Name = "黑体";
wordDoc.Paragraphs.Last.Range.Text = strContent;
// wordApp.Selection.Text = strContent;
//写入加粗文本
strContent = "加粗文本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;//Bold=0为不加粗
wordDoc.Paragraphs.Last.Range.Text = strContent;
// wordApp.Selection.Text = strContent;
//写入15号字体文本
strContent = "15号字体文本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing); wordDoc.Paragraphs.Last.Range.Font.Size = 15;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入斜体文本
strContent = "斜体文本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入蓝色文本
strContent = "蓝色文本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue;
wordDoc.Paragraphs.Last.Range.Text = strContent;
//写入下划线文本
strContent = "下划线文本\n ";
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Text = strContent;
object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script> alert('" + path + ": Word文档写入文本完毕!');</script>");
}
添加文本

效果图:


private void AddWordPic()
{
object path;//文件路径
string strContent;//文件内容
MSWord.Application wordApp;//Word应用程序变量
MSWord.Document wordDoc;//Word文档变量
path = "d:\\myWord.doc";//保存为Word2003文档
// path = "d:\\myWord.doc";//保存为Word2007文档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
string filename = "d:\\kk.jpg";
//定义要向文档中插入图片的位置
object range = wordDoc.Paragraphs.Last.Range;
//定义该图片是否为外部链接
object linkToFile = false;//默认
//定义插入的图片是否随word一起保存
object saveWithDocument = true;
//向word中写入图片
wordDoc.InlineShapes.AddPicture(filename, ref Nothing, ref Nothing, ref Nothing);
object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;//居中显示图片
wordDoc.InlineShapes[1].Height = 130;
wordDoc.InlineShapes[1].Width = 200;
wordDoc.Content.InsertAfter("\n");
wordApp.Selection.EndKey(ref unite, ref Nothing);
wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordApp.Selection.Font.Size = 10;//字体大小
wordApp.Selection.TypeText("图1 测试图片\n"); object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script>alert('" + path + ": Word文档创建图片完毕!');</script>");
}
添加图片

效果图:


private void AddWordTable()
{
object path;//文件路径
string strContent;//文件内容
MSWord.Application wordApp;//Word应用程序变量
MSWord.Document wordDoc;//Word文档变量
path = "d:\\myWord.doc";//保存为Word2003文档
// path = "d:\\myWord.doc";//保存为Word2007文档
wordApp = new MSWord.ApplicationClass();//初始化
if (File.Exists((string)path))
{
File.Delete((string)path);
}
Object Nothing = Missing.Value;
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
int tableRow = 6;
int tableColumn = 6;
//定义一个word中的表格对象
MSWord.Table table = wordDoc.Tables.Add(wordApp.Selection.Range, tableRow, tableColumn, ref Nothing, ref Nothing); wordDoc.Tables[1].Cell(1, 1).Range.Text = "列\n行"; 
for (int i = 1; i < tableRow; i++)
{
for (int j = 1; j < tableColumn; j++)
{
if (i == 1)
{
table.Cell(i, j+1).Range.Text = "Column " + j;
}
if (j == 1)
{
table.Cell(i+1, j).Range.Text = "Row " + i;
}
table.Cell(i+1, j+1).Range.Text = i + "行 " + j + "列";
}
} //添加行
table.Rows.Add(ref Nothing);
table.Rows[tableRow + 1].Height = 45;
//向新添加的行的单元格中添加图片
string FileName = "d:\\kk.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = table.Cell(tableRow+1, tableColumn).Range;//选中要添加图片的单元格
wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); wordDoc.Application.ActiveDocument.InlineShapes[1].Width = 75;//图片宽度
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = 45;//图片高度
// 将图片设置为四周环绕型
MSWord.Shape s = wordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = MSWord.WdWrapType.wdWrapSquare; //设置table样式
table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAtLeast;
table.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
table.Range.Font.Size = 10.5F;
table.Range.Font.Bold = 0;
table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
table.Range.Cells.VerticalAlignment = MSWord.WdCellVerticalAlignment.wdCellAlignVerticalBottom;
//设置table边框样式
table.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleDouble;
table.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
table.Rows[1].Range.Font.Bold = 1;
table.Rows[1].Range.Font.Size = 12F;
table.Cell(1, 1).Range.Font.Size = 10.5F;
wordApp.Selection.Cells.Height = 40;//所有单元格的高度
for (int i = 2; i <= tableRow; i++)
{
table.Rows[i].Height = 20;
}
table.Cell(1, 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(1, 1).Range.Paragraphs[2].Format.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; table.Columns[1].Width = 50;
for (int i = 2; i <=tableColumn; i++)
{
table.Columns[i].Width = 75;
} //添加表头斜线,并设置表头的样式
table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Visible = true;
table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].Color = Microsoft.Office.Interop.Word.WdColor.wdColorGray60;
table.Cell(1, 1).Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
//表格边框
//表格内容行边框
SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt);
//表格内容列边框
SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical, Microsoft.Office.Interop.Word.WdColor.wdColorGray20, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt);
SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);
SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);
SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);
SetTableBorderStyle(table, Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom, Microsoft.Office.Interop.Word.WdColor.wdColorGray50, Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt);
//合并单元格
table.Cell(4, 4).Merge(table.Cell(4, 5));//横向合并
table.Cell(2, 3).Merge(table.Cell(4, 3));//纵向合并 object format = MSWord.WdSaveFormat.wdFormatDocument;
wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
Response.Write("<script>alert('" + path + ": Word文档创建表格完毕!');</script>");
}
添加表格

附:SetTableBorderStyle函数内容

table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Visible = true;             
table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].Color = 
Word.WdColor.wdColorGreen; 
            
table1.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth = 
Word.WdLineWidth.wdLineWidth050pt

效果图:

书签使用:

使用步骤:1:建立word模板,并且在word中插入要用到的书签

     2:c#方法中新建word操作类,并且打开硬盘中建立好的word模板

     3:找到word模板中的书签,并在书签处写入要插入的数据

public void AddDocModel()
{

killWinWordProcess();
wordApp = new ApplicationClass();
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;
object missing = System.Reflection.Missing.Value;
object templateName = Application.StartupPath + @"\Report";//最终的word文档需要写入的位置
object ModelName = Application.StartupPath + @"\Report\ReportModel_Stand.doc";//word模板的位置
object count = 1;
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
wordDoc = wordApp.Documents.Open(ref ModelName, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing);//打开word模板

//在书签处插入文字
object oStart = "PatName";//word中的书签名 
Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
range.Text = "这里是您要输入的内容";//在书签处插入文字内容

//在书签处插入表格
oStart = "PatInfo";//word中的书签名 
range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 
MSWord.Table tab_Pat = wordDoc.Tables.Add(range, 2, 4, ref missing, ref missing);//开辟一个2行4列的表格
tab_Pat.Range.Font.Size = 10.5F;
tab_Pat.Range.Font.Bold = 0;

tab_Pat.Columns[1].Width = 50;
tab_Pat.Columns[2].Width = 65;
tab_Pat.Columns[3].Width = 40;
tab_Pat.Columns[4].Width = 40;

tab_Pat.Cell(1, 1).Range.Text = "病历号";
tab_Pat.Cell(1, 2).Range.Text = "PatientNO";
tab_Pat.Cell(1, 3).Range.Text = "身高";
tab_Pat.Cell(1, 4).Range.Text = "Height";

tab_Pat.Cell(2, 1).Range.Text = "姓名";
tab_Pat.Cell(2, 2).Range.Text ="PatientName";
tab_Pat.Cell(2, 3).Range.Text = "体重";
tab_Pat.Cell(2, 4).Range.Text = "Weight";

//保存word
object format = WdSaveFormat.wdFormatDocument;//保存格式 
wordDoc.SaveAs(ref templateName, ref format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
//关闭wordDoc,wordApp对象 
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}

// 杀掉winword.exe进程 
public void killWinWordProcess()
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); 
foreach (System.Diagnostics.Process process in processes) 
{
bool b = process.MainWindowTitle == ""; 
if (process.MainWindowTitle == "") 
{
process.Kill(); 

}
}

word书签使用

附: c# 将word文档显示在网页上的方式:

public void WordToHtml(string wordFileName)
{
//在此处放置用户代码以初始化页面
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;

//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });

//转换格式,另存为
Type docType = doc.GetType();

string wordSaveFileName = wordFileName.ToString();
//配置保存htm文件的地址
string strPath = Server.MapPath("~/Model/Word/Files/");
string strSaveFileName = strPath + "a.html"; //wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;

docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML });

docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
null, word, null);

Response.Write("<script language='javascript'>window.open ('Files/a.html', 'newwindow', 'height=600, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')</script>");

}

Word转换Html

转换思路:

  >取得Word文档的本地路径

  >将Word文档转换为html文件

  >将html保存到项目中

  >在当前项目中打开此html文件

局限:

  目前只在IE10测试中可以很好使用,在firefox和chrome测试用均有中文乱码的问题,有待解决。

引用:

http://blog.csdn.net/ruby97/article/details/7406806

http://wenku.baidu.com/link?url=osPLfzoQc1Tl0mi7MAT5srs7KUmPZ3WLf3Pjs_I9Ahu87UNbJceGsogT5ONBsI87DnndqJX7HI6-mRcWehoGcF2P-gLKkvCtiH5KA3UI13S

c# 操作Word总结(车)的更多相关文章

  1. python操作word入门

    1.安装pywin32 http://sourceforge.net/projects/pywin32 在files里去找适合你的python版本.截止此文,最新版本是pywin32-219快捷路径: ...

  2. C#中操作Word(1)—— word对象模型介绍

    一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...

  3. C#操作Word的超详细总结

    本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...

  4. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  5. C#操作Word的辅助类(word2003) 修改完善版

    转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...

  6. 黄聪:C#操作Word表格的常见操作(转)

    几种常见C#操作Word表格操作有哪些呢?让我们来看看具体的实例演示: bool saveChange = false; //C#操作Word表格操作 object missing = System. ...

  7. c#操作word表格

    http://www.webshu.net/jiaocheng/programme/ASPNET/200804/6499.html <% if request("infoid" ...

  8. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  9. OpenXML操作word

    OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错.现在开始采用OpenXML.OpenXML(OOXML)是微软在Office 2007中提出的一 ...

随机推荐

  1. 【noip 2011】提高组Day1T3.Mayan游戏

    Description Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是 ...

  2. Centos 04 基础系统优化命令

    在Linux这个系统当中,几乎所有的硬件设备文件都在/dev这个目录内.举例来说,IDE介面的硬盘的文件名称即为/dev/hd[a-d],其中, 括号内的字母为a-d当中的任意一个,亦即有/dev/h ...

  3. Git更新远程仓库代码到本地(转)

    参考链接:https://blog.csdn.net/chailyuan/article/details/53292031 在下载一个较大的github项目以后,当该项目代码更新以后,我们想将更新的内 ...

  4. python - 编程规范问题

    软件目录结构规范alex_老男孩:为什么要设计好目录结构?“设计项目目录结构”,就和“胆码编码风格”一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度:    1.一类同学认为,这种个人 ...

  5. Nginx系列2:用Nginx搭建一个可用的静态资源Web服务器

    上一节中编译好自己的nginx服务器后, 现在要对nginx.conf文件进行配置,搭建一个可用的静态资源Web服务器 1.放入可访问的html文件到nginx文件夹下,如图1所示: 这里我放入的是一 ...

  6. 接口转换 数据库列表的内容 显示在datagrid

    public class AddressConverter : IValueConverter { public object Convert(object value, Type targetTyp ...

  7. graph slam BACK END 相关技术资料收集

    学习SLAM首推2个网站: 1. WIKI上的SLAM介绍与资源总结:http://en.wikipedia.org/wiki/Simultaneous_localization_and_mappin ...

  8. 【转】python模块分析之unittest测试(五)

    [转]python模块分析之unittest测试(五) 系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python模块分析之typing(三) p ...

  9. 【转】python操作excel表格(xlrd/xlwt)

    [转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...

  10. 解决MySQL报错ERROR 2002 (HY000)【转】

    今天在为新的业务线搭架数据库后,在启动的时候报错 root@qsbilldatahis-db01:/usr/local/mysql/bin# ./mysql ERROR 2002 (HY000): C ...