如何向Word中写入数据,C#.net

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何向Word中写入数据,C#.net相关的知识,希望对你有一定的参考价值。

利用如下代码即可:
C# code
public bool ExportWord(DataSet ds, string saveFileName)

bool fileSaved = false;
object filename = saveFileName;
try

Object Nothing = System.Reflection.Missing.Value;
//创建Word文档
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//文档中创建表格
WordApp.Selection.TypeParagraph();
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 13, 5, ref Nothing, ref Nothing);
//设置表格样式
//newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
//newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 100f;
newTable.Columns[3].Width = 100f;
newTable.Columns[4].Width = 50f;
newTable.Columns[5].Width = 50f;
//填充表格内容
newTable.Cell(1, 1).Range.Text = "登记表";
newTable.Cell(1, 1).Range.Bold = 3;//设置单元格中字体为粗体
//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 5));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
object missing = System.Reflection.Missing.Value;
object unit;
unit = Word.WdUnits.wdStory;
WordApp.Selection.EndKey(ref unit, ref missing);
WordApp.Selection.TypeParagraph();
//文件保存
WordDoc.SaveAs(ref filename, 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, ref Nothing);
fileSaved = true;
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
if (WordApp != null)

System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
WordApp = null;

GC.Collect()

catch

fileSaved = false;

return fileSaved;
参考技术A private void but_Table_Click(object sender, EventArgs e)


object Nothing = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
//创建Word文档
Word.Application wordApp = new Word.ApplicationClass();
Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
wordApp.Visible = true;

//设置文档宽度
wordApp.Selection.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse("2"));
wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11;
wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2"));

Object start = Type.Missing;
Object end = Type.Missing;

PictureBox pp = new PictureBox(); //新建一个PictureBox控件
int p1 = 0;
for (int i = 0; i < MyDS_Grid.Tables[0].Rows.Count; i++)

try

byte[] pic = (byte[])(MyDS_Grid.Tables[0].Rows[i][23]); //将数据库中的图片转换成二进制流
MemoryStream ms = new MemoryStream(pic); //将字节数组存入到二进制流中
pp.Image = Image.FromStream(ms); //二进制流Image控件中显示
pp.Image.Save(@"C:\22.bmp"); //将图片存入到指定的路径

catch

p1 = 1;

object rng = Type.Missing;
string strInfo = "职工基本信息表" + "(" + MyDS_Grid.Tables[0].Rows[i][1].ToString() + ")";
start = 0;
end = 0;
wordDoc.Range(ref start, ref end).InsertBefore(strInfo); //插入文本
wordDoc.Range(ref start, ref end).Font.Name = "Verdana"; //设置字体
wordDoc.Range(ref start, ref end).Font.Size = 20; //设置字体大小
wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //设置字体局中

start = strInfo.Length;
end = strInfo.Length;
wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回车

object missingValue = Type.Missing;
object location = strInfo.Length; //如果location超过已有字符的长度将会出错。一定要比"明细表"串多一个字符
Word.Range rng2 = wordDoc.Range(ref location, ref location);

wordDoc.Tables.Add(rng2, 14, 6, ref missingValue, ref missingValue);
wordDoc.Tables.Item(1).Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;
wordDoc.Tables.Item(1).Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
wordDoc.Tables.Item(1).Range.Font.Size = 10;
wordDoc.Tables.Item(1).Range.Font.Name = "宋体";

//设置表格样式
wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).Color = Word.WdColor.wdColorAutomatic;
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐

//第5行显示
wordDoc.Tables.Item(1).Cell(1, 5).Merge(wordDoc.Tables.Item(1).Cell(5, 6));
//第6行显示
wordDoc.Tables.Item(1).Cell(6, 5).Merge(wordDoc.Tables.Item(1).Cell(6, 6));
//第9行显示
wordDoc.Tables.Item(1).Cell(9, 4).Merge(wordDoc.Tables.Item(1).Cell(9, 6));
//第12行显示
wordDoc.Tables.Item(1).Cell(12, 2).Merge(wordDoc.Tables.Item(1).Cell(12, 6));
//第13行显示
wordDoc.Tables.Item(1).Cell(13, 2).Merge(wordDoc.Tables.Item(1).Cell(13, 6));
//第14行显示
wordDoc.Tables.Item(1).Cell(14, 2).Merge(wordDoc.Tables.Item(1).Cell(14, 6));

//第1行赋值
wordDoc.Tables.Item(1).Cell(1, 1).Range.Text = "职工编号:";
wordDoc.Tables.Item(1).Cell(1, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][0].ToString();
wordDoc.Tables.Item(1).Cell(1, 3).Range.Text = "职工姓名:";
wordDoc.Tables.Item(1).Cell(1, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][1].ToString();

//插入图片

if (p1 == 0)

string FileName = @"C:\22.bmp";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = wordDoc.Tables.Item(1).Cell(1, 5).Range; //指定图片插入的区域
//将图片插入到单元格中
wordDoc.Tables.Item(1).Cell(1, 5).Range.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);

p1 = 0;

//第2行赋值
wordDoc.Tables.Item(1).Cell(2, 1).Range.Text = "民族类别:";
wordDoc.Tables.Item(1).Cell(2, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][2].ToString();
wordDoc.Tables.Item(1).Cell(2, 3).Range.Text = "出生日期:";
try

wordDoc.Tables.Item(1).Cell(2, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][3]).ToShortDateString());

catch wordDoc.Tables.Item(1).Cell(2, 4).Range.Text = "";
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][3]);
//第3行赋值
wordDoc.Tables.Item(1).Cell(3, 1).Range.Text = "年龄:";
wordDoc.Tables.Item(1).Cell(3, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][4]);
wordDoc.Tables.Item(1).Cell(3, 3).Range.Text = "文化程序:";
wordDoc.Tables.Item(1).Cell(3, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][5].ToString();
//第4行赋值
wordDoc.Tables.Item(1).Cell(4, 1).Range.Text = "婚姻:";
wordDoc.Tables.Item(1).Cell(4, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][6].ToString();
wordDoc.Tables.Item(1).Cell(4, 3).Range.Text = "性别:";
wordDoc.Tables.Item(1).Cell(4, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][7].ToString();
//第5行赋值
wordDoc.Tables.Item(1).Cell(5, 1).Range.Text = "政治面貌:";
wordDoc.Tables.Item(1).Cell(5, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][8].ToString();
wordDoc.Tables.Item(1).Cell(5, 3).Range.Text = "单位工作时间:";
try

wordDoc.Tables.Item(1).Cell(5, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[0][10]).ToShortDateString());

catch wordDoc.Tables.Item(1).Cell(5, 4).Range.Text = "";
//第6行赋值
wordDoc.Tables.Item(1).Cell(6, 1).Range.Text = "籍贯:";
wordDoc.Tables.Item(1).Cell(6, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][24].ToString();
wordDoc.Tables.Item(1).Cell(6, 3).Range.Text = MyDS_Grid.Tables[0].Rows[i][25].ToString();
wordDoc.Tables.Item(1).Cell(6, 4).Range.Text = "身份证:";
wordDoc.Tables.Item(1).Cell(6, 5).Range.Text = MyDS_Grid.Tables[0].Rows[i][9].ToString();
//第7行赋值
wordDoc.Tables.Item(1).Cell(7, 1).Range.Text = "工龄:";
wordDoc.Tables.Item(1).Cell(7, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][11]);
wordDoc.Tables.Item(1).Cell(7, 3).Range.Text = "职工类别:";
wordDoc.Tables.Item(1).Cell(7, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][12].ToString();
wordDoc.Tables.Item(1).Cell(7, 5).Range.Text = "职务类别:";
wordDoc.Tables.Item(1).Cell(7, 6).Range.Text = MyDS_Grid.Tables[0].Rows[i][13].ToString();
//第8行赋值
wordDoc.Tables.Item(1).Cell(8, 1).Range.Text = "工资类别:";
wordDoc.Tables.Item(1).Cell(8, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][14].ToString();
wordDoc.Tables.Item(1).Cell(8, 3).Range.Text = "部门类别:";
wordDoc.Tables.Item(1).Cell(8, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][15].ToString();
wordDoc.Tables.Item(1).Cell(8, 5).Range.Text = "职称类别:";
wordDoc.Tables.Item(1).Cell(8, 6).Range.Text = MyDS_Grid.Tables[0].Rows[i][16].ToString();
//第9行赋值
wordDoc.Tables.Item(1).Cell(9, 1).Range.Text = "月工资:";
wordDoc.Tables.Item(1).Cell(9, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][26]);
wordDoc.Tables.Item(1).Cell(9, 3).Range.Text = "银行帐号:";
wordDoc.Tables.Item(1).Cell(9, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][27].ToString();
//第10行赋值
wordDoc.Tables.Item(1).Cell(10, 1).Range.Text = "合同起始日期:";
try

wordDoc.Tables.Item(1).Cell(10, 2).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][28]).ToShortDateString());

catch wordDoc.Tables.Item(1).Cell(10, 2).Range.Text = "";
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][28]);
wordDoc.Tables.Item(1).Cell(10, 3).Range.Text = "合同结束日期:";
try

wordDoc.Tables.Item(1).Cell(10, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][29]).ToShortDateString());

catch wordDoc.Tables.Item(1).Cell(10, 4).Range.Text = "";
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][29]);
wordDoc.Tables.Item(1).Cell(10, 5).Range.Text = "合同年限:";
wordDoc.Tables.Item(1).Cell(10, 6).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][30]);
//第11行赋值
wordDoc.Tables.Item(1).Cell(11, 1).Range.Text = "电话:";
wordDoc.Tables.Item(1).Cell(11, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][17].ToString();
wordDoc.Tables.Item(1).Cell(11, 3).Range.Text = "手机:";
wordDoc.Tables.Item(1).Cell(11, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][18].ToString();
wordDoc.Tables.Item(1).Cell(11, 5).Range.Text = "毕业时间:";
try

wordDoc.Tables.Item(1).Cell(11, 6).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][21]).ToShortDateString());

catch wordDoc.Tables.Item(1).Cell(11, 6).Range.Text = "";
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][21]);
//第12行赋值
wordDoc.Tables.Item(1).Cell(12, 1).Range.Text = "毕业学校:";
wordDoc.Tables.Item(1).Cell(12, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][19].ToString();
//第13行赋值
wordDoc.Tables.Item(1).Cell(13, 1).Range.Text = "主修专业:";
wordDoc.Tables.Item(1).Cell(13, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][20].ToString();
//第14行赋值
wordDoc.Tables.Item(1).Cell(14, 1).Range.Text = "家庭地址:";
wordDoc.Tables.Item(1).Cell(14, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][22].ToString();

wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回车
wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //设置字体局中

追问

如何读取Word中信息?

参考技术B 1. 安装Office,添加引用COM里面的 Microsoft Word 14.0 Object. Library
2. 导命名空间 using MsWord =Microsoft.Office.Interop.Word;
3. 创建Word应用程序
把引用中的Microsoft.Office.Interop.Word属性中的嵌入互操作设为False
创建Word应用程序
MsWord .Application wordApp = new MsWord.ApplicationClass();
使其可见:
wordApp.Visible = true;
4. 创建Word文档对象:
MsWord.Document wordDoc = wordApp.Documents.Add();创建空文档
根据aaa这个模板来创建新的文档
MsWord.Document wordDoc = wordApp.Documents.Add(@"E:\aaa.doc");
5. 打开Word文档
MsWord.Document wordDoc = wordApp.Documents.Open(@"E:\aaa.doc");
6. 向word中添加内容

//加内容
MsWord.Application wordApp = new MsWord.ApplicationClass();
MsWord.Document wordDoc = wordApp.Documents.Add();
//Paragraphs是段落,可直接add,这里是最后一段范围
wordDoc.Paragraphs.Last.Range.Text = "这是第一行\n";
wordDoc.Paragraphs.Last.Range.Text = "第二行开始了";//不加\n会被覆盖
wordDoc.Paragraphs.Last.Range.Text = "会覆盖的";
//直接添加段,不是覆盖
wordDoc.Paragraphs.Last.Range.Text += "不会覆盖的";
//添加在此段的文字后面,不是新段落
wordDoc.Paragraphs.Last.Range.InsertAfter("这是后面的内容");
wordApp.Visible = true;

7. 保存
wordApp.Visible = false; //不可见直接保存
wordDoc.SaveAs2(@"E:\bbb.docx");//保存
wordDoc.Close(true);//关闭
wordApp.Quit();//释放Word进程
8. 操作字体

//字体
wordDoc.Paragraphs.Last.Range.Font.Bold = 1;
wordDoc.Paragraphs.Last.Range.Font.Italic = 1;
wordDoc.Paragraphs.Last.Range.Font.Size = 32;
wordDoc.Paragraphs.Last.Range.Font.Name = "楷体";
wordDoc.Paragraphs.Last.Range.Font.Underline = MsWord.WdUnderline.wdUnderlineDash;
wordDoc.Paragraphs.Last.Range.Font.ColorIndex = MsWord.WdColorIndex.wdPink;

9. 创建表格

//表格,5行5列
MsWord.Table table = wordDoc.Paragraphs.Last.Range.Tables.Add(wordDoc.Paragraphs.Last.Range,5,5);
//设置边线可见
table.Borders.Enable = 1;
table.Borders.InsideColor = MsWord.WdColor.wdColorBlue;
table.Borders.OutsideColor = MsWord.WdColor.wdColorBlack;
table.Borders.OutsideLineWidth = MsWord.WdLineWidth.wdLineWidth025pt;
//表格加内容
for(int i =1 ;i<=5;i++)

for (int j = 1; j <= 5; j++)

table.Rows[i].Cells[j].Range.Text="第i行,第j列";

//下面这行代码设置表格一行的背景色
table.Rows[i].Shading.BackgroundPatternColor = MSWord.WdColor.wdColorDarkYellow;

10.如何插入图片?

wordDoc.InlineShapes.AddPicture(@"E:\cyl.jpg",false,true);
//设置图片宽高的绝对大小
//wordDoc.InlineShapes[1].Width = 200;
//wordDoc.InlineShapes[1].Height = 150;
//按比例缩放大小
wordDoc.InlineShapes[1].ScaleWidth = 30;
wordDoc.InlineShapes[1].ScaleHeight = 30;

11. 如何操作书签,在书签处添加内容

wordDoc.Bookmarks["xingming"].Range.Text = "马晨";
wordDoc.Bookmarks["xingbie"].Range.Text = "男";
wordDoc.Bookmarks["minzu"].Range.Text = "汉族";
wordDoc.Bookmarks["shengri"].Range.Text = "1991-6-1";
wordDoc.Bookmarks["zhengzhimianmao"].Range.Text = "共青团员";
wordDoc.Bookmarks["zhaopian"].Range.InlineShapes.AddPicture(@"e:\cyl.jpg");
wordDoc.Bookmarks["zhaopian"].Range.InlineShapes[1].Width = 80;
wordDoc.Bookmarks["zhaopian"].Range.InlineShapes[1].Height = 100;

12. 打印预览及打印
打印预览:
wordDoc.PrintPreview();
打印
wordDoc.PrintOut();
13. 注意:在隐藏的时候一定要注意代码最后写上关闭文档和程序
wordApp.Visible = false;

wordDoc.Close(false);
wordApp.Quit();
参考技术C 近做项目要涉及生成WORD,找了资料,感觉这个CreateWordFile()方法比较详细,借鉴!
1、添加引用->COM->Microsoft Word 11.0 Object Library
2、在.cs文件中添加CreateWordFile()方法
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
//添加二个命名控件
using Word;
using System.IO;
namespace WordFile

public partial class _Default : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)


protected void Button1_Click(object sender, EventArgs e)

CreateWordFile("D:\\dd.doc");//调用方法

public string CreateWordFile(string CheckedInfo)

string message = "";
try

Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("D:/CNSI"); //创建文件所在目录

string name = "CNSI_" + DateTime.Now.ToLongDateString() + ".doc";//文件名
object filename = "D://CNSI//" + name; //文件保存路径
//创建Word文档
Word.Application WordApp = new Word.ApplicationClass();
Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//添加页眉
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
//移动焦点并换行
object count = 14;
object WdLine = Word.WdUnits.wdLine;//换一行;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
WordApp.Selection.TypeParagraph();//插入段落
//文档中创建表格
Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle =Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;
//填充表格内容
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
//填充表格内容
newTable.Cell(2, 1).Range.Text = "产品基本信息";
newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
//合并单元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//填充表格内容
newTable.Cell(3, 1).Range.Text = "品牌名称:";
newTable.Cell(3, 2).Range.Text = "BrandName";
//纵向合并单元格
newTable.Cell(3, 3).Select();//选中一行
object moveUnit = Word.WdUnits.wdLine;
object moveCount = 5;
object moveExtend = Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();
//插入图片
string FileName = @"d:\8.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
//将图片设置为四周环绕型
Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type =Word.WdWrapType.wdWrapSquare;
newTable.Cell(12, 1).Range.Text = "产品特殊属性";
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
//文件保存
WordDoc.SaveAs(ref filename, 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, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
message = name + "文档生成成功,以保存到D:CNSI下";
Response.Write(message);

catch

message = "文件导出异常!";

return message;


参考技术D 您好。这个问题我以前曾经研究过,写一篇博文来讲述这个问题。
请参考我的博客,相信里面的内容应该是你要的。
http://blog.csdn.net/fujie724/archive/2010/04/02/5443322.aspx
《C#:简单实现动态数据生成Word文档并保存》

谢谢采纳。追问

如何读取Word中信息?

追答

http://hi.baidu.com/hclred/blog/item/dacba61c420a538887d6b64b.html

请参考这里的作法。

本回答被提问者采纳

向XML中写入数据

用C#操作XML:(写入)
在textarea中写入文本,带换行符的
在写入XML中时,会被直接写入,就是在XML中的数据还是换行的
用正则把文本中"\n"换成"<br>",在XML中还是换了行
方法是可以用<![CDATA["asfdasfd"]]>来解决。
但是要怎么在写入数据的时候加上<![CDATA[]>?!
还要保证<![CDATA[]>里面不能有换行
谢谢

参考技术A 向xml文件中动态的添加节点(数据)是一件很爽的事,可以给你的程序带来很多的方便,比如在web中,如果你的flash用到了xml文件,这个方法可以让你在后台就轻轻松松的更新你的flash内容哦 参考技术B 1, 把数据读入到dataset .

2 ,利用dataset 的write() 方法写入。
参考技术C XmlDocument doc = new XmlDocument();
XmlElement xe = doc.CreateElement("root");
XmlCDataSection xcs = doc.CreateCDataSection("test");

xcs.InnerText = "<aaaaa><br/>";
xe.AppendChild(xcs);
doc.AppendChild(xe);
doc.Save("c:\\aa.xml");本回答被提问者采纳
参考技术D XmlDocument
doc
=
new
XmlDocument();
XmlElement
xe
=
doc.CreateElement("root");
XmlCDataSection
xcs
=
doc.CreateCDataSection("test");
xcs.InnerText
=
"<aaaaa><br/>";
xe.
AppendChild
(xcs);
doc.AppendChild(xe);
doc.Save("c:\\aa.xml");

以上是关于如何向Word中写入数据,C#.net的主要内容,如果未能解决你的问题,请参考以下文章

向XML中写入数据

keil中 如何向指定地址写入数据

向Word模板中填充数据

如何将matlab 中的数据写入现有的word 表格中?

c#/.net 如何把word文件储存到服务器

C#/VB.NET Word转Text