首页 新闻 会员 周边

asp.net 生成word的问题

0
悬赏园豆:20 [已解决问题] 解决于 2010-03-18 14:58

客户给了一下word表格

需求是要把从数据库查询出的数据填充到这些word表格对应的栏目里

应该怎么实现

高手们提供下思路

程序小强的主页 程序小强 | 初学一级 | 园豆:0
提问于:2010-03-12 09:00
< >
分享
最佳答案
0

其实Word模板比较好容易实现,其原理就是占位符+替代字符串,你可以用占位符(如"<%字段1%>")表示字段名,建立一个Word文件,然后用程序操作这个Word,用数据替换占位符,然后另存为新的文件即可。

这里的难点是在如何处理表格,需要你了解Word的相关API,以下代码仅供参考:

  1. bool saveChange = false;  
  2.  //C#操作Word表格操作
  3. object missing = System.Reflection.Missing.Value;  
  4.  
  5. object template = (object)templateFilePath;  
  6.  
  7. object filename = (object)saveFilePath;    
  8. object isVisible = missing;  
  9. object readOnly = missing;  
  10. object breakType = Word.WdBreakType.wdSectionBreakNextPage;  
  11. object isSaveChange = (object)saveChange;  
  12. Word.Document doc = null;  
  13.  
  14. //定义一个Word.Application 对象  
  15.  
  16. Word.Application WordApp = new Word.ApplicationClass();  
  17.  
  18. //C#操作Word表格操作之打开文档  
  19. doc = WordApp.Documents.Open(ref template,   
  20. ref missing,ref readOnly,ref missing,   
  21. ref missing, ref missing, ref missing,   
  22. ref missing, ref missing,ref missing,   
  23. ref missing, ref isVisible, ref missing,   
  24. ref missing, ref missing, ref missing);  
  25.  
  26. //C#操作Word表格操作之设置页眉文本  
  27.  
  28. WordApp.ActiveWindow.ActivePane.View.SeekView =  
  29.  Word.WdSeekView.wdSeekCurrentPageHeader;  
  30. WordApp.Selection.WholeStory();  
  31. WordApp.Selection.TypeText( this.m_titleText );  
  32. WordApp.ActiveWindow.ActivePane.View.SeekView =   
  33. Word.WdSeekView.wdSeekMainDocument;  
  34.  
  35. //页面设置,设置页面为纵向布局,设置纸张类型为A4纸  
  36.  
  37. doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;  
  38. doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);  
  39. doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);  
  40.  
  41. //C#操作Word表格操作之创建表格及设置表格和单元格属性  
  42.  
  43. object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;  
  44. doc.Content.Tables.Add(  
  45. WordApp.Selection.Range, totalCount + 1,   
  46. totalField - keyCount_1, ref missing,   
  47. ref autoFitBehavior);  
  48.  
  49. //C#操作Word表格操作之合并单元隔  
  50.  
  51. doc.Content.Tables[1].Cell(i+1,j).Select();  
  52. object moveUnit = Word.WdUnits.wdLine;  
  53. object moveCount = 1;  
  54. object moveExtend = Word.WdMovementType.wdExtend;  
  55. WordApp.Selection.MoveUp(ref moveUnit,  
  56.  ref moveCount, ref moveExtend);  
  57. WordApp.Selection.Cells.Merge();  
  58. WordApp.Selection.Cells.VerticalAlignment =   
  59. Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
  60.  
  61. doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”  
  62.  
  63. //添加表格行  
  64.  
  65. doc.Content.Tables[0].Rows.Add(ref beforeRow);  
  66.  
  67. //C#操作Word表格操作之添加表格列  
  68.  
  69. doc.Content.Tables[0].Columns.Add(ref beforeColumn);  
  70.  
  71. //文本居中  
  72. WordApp.Selection.ParagraphFormat.Alignment =  
  73. Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  74. WordApp.Selection.Cells.VerticalAlignment =   
  75. Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;  
  76.  
  77. //C#操作Word表格操作之选中单元格文字垂直居中  
  78.  
  79. oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);  
  80.  
  81. Word.Table newTable = oDoc.Tables[1];  
  82. object beforeRow = newTable.Rows[1];  
  83. newTable.Rows.Add(ref beforeRow);  
  84.  
  85. Word.Cell cell = newTable.Cell(1, 1);  
  86. cell.Merge(newTable.Cell(1, 2)); 
收获园豆:20
查尔斯 | 老鸟四级 |园豆:3832 | 2010-03-12 10:30
其他回答(1)
0

我记得上家公司原来搞过Excel的生成。按照当时做的思路来说,你首先需要按照客户的要求用word制作一个模板,然后这个模板可以是doc(2003)格式或者是dot格式(2003的模板格式)的,然后保存到你程序可以访问的目录,然后程序从数据库查询出数据,数据读取完成后按照模板来填充内容即可。

西越泽 | 园豆:10775 (专家六级) | 2010-03-12 09:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册