这么一个操作,是要点击按钮打开一个word文档,这个文档是一个模版形式,里面有表格,需要给表格中相应的位置填充数据
数据是从数据库读取的,相对来说属于FSO范畴吧,就是操作word中表格等元素的方法不知道有没有..
这个应该怎么操作呢?小弟初次接触,希望各位高手给指点一下,最好有参考资料,顶贴有分...

解决方案 »

  1.   

    学习~,这些功能一般都要通过添加COM组件引用到命名空间吧,如Microsoft word/Excel/Medial等
      

  2.   

    这种实现要通过VBA来做,在代码里面实现VBA的功能,给你个提示,打开WORD录制宏,这样能生成一段实现你所做的表格或者模板的代码,然后你在代码里面绘制,边绘制边放数据,每次都是重新绘制.
      

  3.   

    添加引用->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;
            }
      

  4.   

    我现在遇到的问题是,文件写入生成后打开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);
      

  5.   

    学习
    PS: 
    我的目标是 ----> ^_^
      

  6.   

    这种情况建议在Excel里面做,在Excel单元格里面插入一个标注,使用VBA处理,找到该标注向标注的单元格里面填充值
      

  7.   


    试试:
    StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
      

  8.   

    直接将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(); 
     
      

  9.   

    以前用C#生成过WORD,EXCEL等等报表.可以这么干:添加OFFICE的DLL引用,它提供的有操作各种OFFICE文档的方法,包括表格,图片等等.但是如果要填充模板,必须知道模板是什么样的,也就是从程序的角度得知道模板的结构.各种OFFICE都有不同的文档对象模型.有的是层次性的,大对象包含小对象,有的是数组性的.具体的操作函数有很多,长时间不用,已经忘记了.查MSDN,应该能够找得到.
      

  10.   

    http://support.microsoft.com/kb/316384/zh-cn