打开word文档,并写入数据,顶帖有分...

mm6268 2008-11-20 01:37:58
这么一个操作,是要点击按钮打开一个word文档,这个文档是一个模版形式,里面有表格,需要给表格中相应的位置填充数据
数据是从数据库读取的,相对来说属于FSO范畴吧,就是操作word中表格等元素的方法不知道有没有..
这个应该怎么操作呢?小弟初次接触,希望各位高手给指点一下,最好有参考资料,顶贴有分...
...全文
264 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
yilanwuyu123 2008-11-21
  • 打赏
  • 举报
回复
http://support.microsoft.com/kb/316384/zh-cn
huangpeng8612 2008-11-21
  • 打赏
  • 举报
回复
以前用C#生成过WORD,EXCEL等等报表.可以这么干:添加OFFICE的DLL引用,它提供的有操作各种OFFICE文档的方法,包括表格,图片等等.但是如果要填充模板,必须知道模板是什么样的,也就是从程序的角度得知道模板的结构.各种OFFICE都有不同的文档对象模型.有的是层次性的,大对象包含小对象,有的是数组性的.具体的操作函数有很多,长时间不用,已经忘记了.查MSDN,应该能够找得到.
Ki1381 2008-11-21
  • 打赏
  • 举报
回复
帮顶,学习下
wangping_li 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 mm6268 的回复:]
我现在遇到的问题是,文件写入生成后打开word全是乱码,怀疑是所取的编码格式不对,请高手指点...

C# code
string ph = "D:/Try/try.doc";
Stream fs = File.Open(ph, FileMode.Open);
StreamReader sr = new StreamReader(fs);

string st = sr.ReadToEnd();
string s1 = this.textBox1.Text.Trim();
string s2 = this.textBox2.Text.Trim();

[/Quote]
直接将work格式转为string会出现乱码的
你改用Byte方式读取,
如下:

System.IO.FileStream fs=File.OpenRead(MapPath("Cache/" + fileName + ".doc"));
byte[] fileB=new byte[fs.Length];
fs.Read(fileB,0,fileB.Length);

StreamWriter sw=new StreamWriter(Server.MapPath("Cache/"+ fileName+"新的" + "."+dbType),false,System.Text.Encoding.GetEncoding("gb2312"));
sw.Write(fileB);
sw.Flush();
sw.Close();
3tzjq 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 mm6268 的回复:]
我现在遇到的问题是,文件写入生成后打开word全是乱码,怀疑是所取的编码格式不对,请高手指点...
C# codestringph="D:/Try/try.doc";
Stream fs=File.Open(ph, FileMode.Open);
StreamReader sr=newStreamReader(fs);stringst=sr.ReadToEnd();strings1=this.textBox1.Text.Trim();strings2=this.textBox2.Text.Trim();strings3=this.textBox3.Text.Trim();strings4=this.textBox4.Text.Trim();strings5=t…
[/Quote]

试试:
StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
啊兵 2008-11-21
  • 打赏
  • 举报
回复
这种情况建议在Excel里面做,在Excel单元格里面插入一个标注,使用VBA处理,找到该标注向标注的单元格里面填充值
mm6268 2008-11-21
  • 打赏
  • 举报
回复
高手啊,进来看看吧.急死啦....
caorenlong 2008-11-21
  • 打赏
  • 举报
回复
cailinLucky 2008-11-21
  • 打赏
  • 举报
回复
顶......
derelictangel 2008-11-21
  • 打赏
  • 举报
回复
学习


PS:
我的目标是 ---->





















^_^
chenfan31 2008-11-21
  • 打赏
  • 举报
回复
顶!!!
Joschime 2008-11-21
  • 打赏
  • 举报
回复
up
netboygg 2008-11-21
  • 打赏
  • 举报
回复
标记一下 学习用
mm6268 2008-11-21
  • 打赏
  • 举报
回复
我现在遇到的问题是,文件写入生成后打开word全是乱码,怀疑是所取的编码格式不对,请高手指点...

string ph = "D:/Try/try.doc";
Stream fs = File.Open(ph, FileMode.Open);
StreamReader sr = new StreamReader(fs);

string st = sr.ReadToEnd();
string s1 = this.textBox1.Text.Trim();
string s2 = this.textBox2.Text.Trim();
string s3 = this.textBox3.Text.Trim();
string s4 = this.textBox4.Text.Trim();
string s5 = this.textBox5.Text.Trim();
string s6 = this.textBox6.Text.Trim();
st = st.Replace("M11", s1);
st = st.Replace("M21", s2);
st = st.Replace("M31", s3);
st = st.Replace("M12", s4);
st = st.Replace("M22", s5);
st = st.Replace("M32", s6);

String path = "D:/Try/try2.doc";
StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.GetEncoding(0));
sw.Write(st);
sxmonsy 2008-11-21
  • 打赏
  • 举报
回复
MARK
claymore1114 2008-11-20
  • 打赏
  • 举报
回复
up
wangping_li 2008-11-20
  • 打赏
  • 举报
回复
添加引用->COM->Microsoft Word 11.0 Object Library

引用using Word;
然后参考:

public string CreateWordFile(string CheckedInfo)
...{
string message = "";
try
...{
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录
string name = "CNSI_" + DateTime.Now.ToShortString()+".doc";
object filename = "C://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 = Picture;//图片所在路径
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+"文档生成成功,以保存到C:\CNSI\下";
}
catch
...{
message = "文件导出异常!";
}
return message;
}
gis_yxq 2008-11-20
  • 打赏
  • 举报
回复
接分
wesleyluo 2008-11-20
  • 打赏
  • 举报
回复
帮顶起。
haifeng39 2008-11-20
  • 打赏
  • 举报
回复
up
加载更多回复(12)

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧