Нет описания

ExportWord.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.IO;
  7. using MSWord = Microsoft.Office.Interop.Word;
  8. /// <summary>
  9. ///DaoChuWord 的摘要说明
  10. /// </summary>
  11. public class ExportWord
  12. {
  13. public string CreateWord(DataTable dt, DataTable jbdata,string usercode)
  14. {
  15. string message = "";
  16. Object Nothing = System.Reflection.Missing.Value;
  17. Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
  18. Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
  19. //设置纸张为A4纸大小
  20. WordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;
  21. WordDoc.ActiveWindow.Selection.Font.Bold = 2;
  22. WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距
  23. WordDoc.PageSetup.LeftMargin = 45f;
  24. WordDoc.PageSetup.RightMargin = 45f;
  25. //移动焦点并换行
  26. object count = 14;
  27. object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
  28. WordApp.Selection.ParagraphFormat.LineSpacing = 18f;
  29. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  30. WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
  31. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  32. WordApp.Selection.TypeParagraph();//插入段落
  33. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  34. WordApp.Selection.Font.Size = 18f;
  35. object missing = System.Reflection.Missing.Value;
  36. object WdLine2 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
  37. WordApp.Selection.Text = "安 阳 市 12345 政 务 服 务 热 线 交 办 单";
  38. WordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距
  39. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  40. WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
  41. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  42. WordApp.Selection.TypeParagraph();//插入段落
  43. WordApp.Selection.Font.Size = 11f;
  44. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
  45. WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
  46. //文档中创建表格
  47. Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 18, 8, ref Nothing, ref Nothing);
  48. //设置表格样式
  49. for (int i = 1; i < 9; i ++)
  50. {
  51. if (i ==2)
  52. {
  53. newTable.Columns[i].Width = 75f;
  54. }
  55. else if (i ==8)
  56. {
  57. newTable.Columns[i].Width = 80f;
  58. }
  59. else if (i ==4)
  60. {
  61. newTable.Columns[i].Width = 65f;
  62. }
  63. else if (i == 6)
  64. {
  65. newTable.Columns[i].Width = 65f;
  66. }
  67. else
  68. newTable.Columns[i].Width = 60f;
  69. }
  70. for (int i=1;i<19;i++)
  71. {
  72. newTable.Rows[i].Height = 32f;
  73. }
  74. string user = "";
  75. if (jbdata.Rows.Count > 0)
  76. user = jbdata.Rows[0]["F_CreateUser"].ToString();
  77. //表格外线
  78. newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
  79. //表格内线
  80. newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
  81. //垂直居中
  82. object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
  83. object countjz = 1;
  84. WordApp.Selection.MoveEnd(ref unit, ref countjz);
  85. WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
  86. newTable.Cell(1, 1).Range.Text = "事项编号";
  87. newTable.Cell(1, 1).Merge(newTable.Cell(2, 1));
  88. newTable.Cell(1, 2).Range.Text = dt.Rows[0]["F_WorkOrderId"].ToString();
  89. newTable.Cell(1, 2).Range.Font.Bold = 0;
  90. newTable.Cell(1, 2).Merge(newTable.Cell(2, 2));
  91. newTable.Cell(1, 3).Range.Text = "工单来源";
  92. newTable.Cell(1, 4).Range.Text = dt.Rows[0]["SourceName"].ToString();
  93. newTable.Cell(1, 4).Range.Font.Bold = 0;
  94. newTable.Cell(1, 5).Range.Text = "办理时限";
  95. if (jbdata.Rows.Count > 0)
  96. {
  97. if (jbdata.Rows[0]["F_LimitTime"]!=null && jbdata.Rows[0]["F_LimitTime"].ToString()!="")
  98. newTable.Cell(1, 6).Range.Text =DateTime .Parse ( jbdata.Rows[0]["F_LimitTime"].ToString()).ToString ("yyyy-MM-dd");
  99. else
  100. newTable.Cell(1, 6).Range.Text = "";
  101. }
  102. else
  103. newTable.Cell(1, 6).Range.Text = "";
  104. newTable.Cell(1, 6).Range.Font.Bold = 0;
  105. newTable.Cell(1, 7).Range.Text = "联系电话";
  106. newTable.Cell(1, 8).Range.Text = dt.Rows[0]["F_CusPhone"].ToString();
  107. newTable.Cell(1, 8).Range.Font.Bold = 0;
  108. newTable.Cell(2, 3).Range.Text = "签收时间";
  109. if (jbdata.Rows.Count > 0)
  110. {
  111. if (jbdata.Rows[0]["F_SureTime"] != null && jbdata.Rows[0]["F_SureTime"].ToString() != "")
  112. newTable.Cell(2, 4).Range.Text = DateTime.Parse(jbdata.Rows[0]["F_SureTime"].ToString()).ToString("yyyy-MM-dd");
  113. else
  114. newTable.Cell(2, 4).Range.Text = "";
  115. }
  116. else
  117. newTable.Cell(2, 4).Range.Text = "";
  118. newTable.Cell(2, 4).Range.Font.Bold = 0;
  119. newTable.Cell(2, 5).Range.Text = "办理时间";
  120. if (dt.Rows[0]["F_CreateTime"] != null && dt.Rows[0]["F_CreateTime"].ToString() != "")
  121. newTable.Cell(2, 6).Range.Text = DateTime.Parse(dt.Rows[0]["F_CreateTime"].ToString()).ToString("yyyy-MM-dd");
  122. else
  123. newTable.Cell(2, 6).Range.Text = dt.Rows[0]["F_CreateTime"].ToString();
  124. newTable.Cell(2, 6).Range.Font.Bold = 0;
  125. newTable.Cell(2, 7).Range.Text = "来电人";
  126. newTable.Cell(2, 8).Range.Text = dt.Rows[0]["F_CusName"].ToString();
  127. newTable.Cell(2,8).Range.Font.Bold = 0;
  128. newTable.Cell(3, 1).Range.Text = "事项区域";
  129. newTable.Cell(3, 2).Range.Text = dt.Rows[0]["F_SourceAddress"].ToString();
  130. newTable.Cell(3, 2).Merge(newTable.Cell(3, 8));
  131. newTable.Cell(3, 2).Range.Font.Bold = 0;
  132. newTable.Cell(4, 1).Range.Text = "内容摘要";
  133. newTable.Cell(4, 1).Merge(newTable.Cell(7, 1));
  134. if (dt.Rows[0]["F_Content"] != null && dt.Rows[0]["F_Content"].ToString() != "")
  135. {
  136. newTable.Cell(4, 2).Range.Text = dt.Rows[0]["F_Content"].ToString();
  137. }
  138. else
  139. {
  140. newTable.Cell(4, 2).Range.Text = dt.Rows[0]["F_ComContent"].ToString();
  141. }
  142. newTable.Cell(4, 2).Merge(newTable.Cell(7, 8));
  143. newTable.Cell(4, 2).Range.Font.Bold = 0;
  144. newTable.Cell(4, 1).Height = 130f;
  145. newTable.Cell(4, 2).Height = 130f;
  146. newTable.Cell(5, 1).Range.Text = "承办单位";
  147. if (jbdata.Rows.Count > 0)
  148. newTable.Cell(5, 2).Range.Text = jbdata.Rows[0]["DeptName"].ToString();
  149. newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));
  150. newTable.Cell(5, 2).Range.Font.Bold = 0;
  151. newTable.Cell(5, 3).Range.Text = "协办单位";
  152. if (jbdata.Rows.Count > 0)
  153. newTable.Cell(5, 4).Range.Text = jbdata.Rows[0]["OtherDeptName"].ToString();
  154. newTable.Cell(5, 4).Merge(newTable.Cell(5, 6));
  155. newTable.Cell(5, 4).Range.Font.Bold = 0;
  156. newTable.Cell(6, 1).Range.Text = "调度意见";
  157. newTable.Cell(6, 1).Merge(newTable.Cell(8, 1));
  158. if (jbdata.Rows.Count > 0)
  159. newTable.Cell(6, 2).Range.Text = jbdata.Rows[0]["F_AssignedOpinion"].ToString();
  160. newTable.Cell(6, 2).Merge(newTable.Cell(8, 8));
  161. newTable.Cell(6, 2).Range.Font.Bold = 0;
  162. newTable.Cell(6, 1).Height = 90f;
  163. newTable.Cell(6, 2).Height = 90f;
  164. newTable.Cell(7, 1).Range.Text = "承办意见";
  165. newTable.Cell(7, 1).Merge(newTable.Cell(8, 1));
  166. newTable.Cell(7, 2).Range.Text = " ";
  167. newTable.Cell(7, 2).Merge(newTable.Cell(8, 8));
  168. newTable.Cell(7, 1).Height = 50f;
  169. newTable.Cell(7, 2).Height = 50f;
  170. newTable.Cell(8, 1).Range.Text = "领导批示";
  171. newTable.Cell(8, 1).Merge(newTable.Cell(9, 1));
  172. newTable.Cell(8, 2).Range.Text = "";
  173. newTable.Cell(8, 2).Merge(newTable.Cell(9, 8));
  174. newTable.Cell(8, 1).Height = 50f;
  175. newTable.Cell(8, 2).Height = 50f;
  176. newTable.Cell(9, 1).Range.Text = "处理结果";
  177. newTable.Cell(9, 1).Merge(newTable.Cell(12, 1));
  178. newTable.Cell(9, 2).Range.Text = dt.Rows[0]["F_Result"].ToString();
  179. newTable.Cell(9, 2).Merge(newTable.Cell(12, 8));
  180. newTable.Cell(9, 2).Range.Font.Bold = 0;
  181. newTable.Cell(9, 1).Height = 140f;
  182. newTable.Cell(9, 2).Height = 140f;
  183. WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
  184. object unite = MSWord.WdUnits.wdStory;
  185. WordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
  186. WordDoc.Content.InsertAfter("联系电话:" + "(0372)12345"+ " " + "审核员:" + usercode + " " + " 调度员:" + user);
  187. string FileName = "/Upload/Word/" + "/word.png";
  188. FileName = HttpContext.Current.Server.MapPath("..") + FileName;
  189. newTable.Cell(6, 2).Select();//选中一行
  190. object LinkToFile = false;
  191. object SaveWithDocument = true;
  192. object Anchor = WordDoc.Application.Selection.Range;
  193. WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
  194. WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 120f;//图片宽度
  195. WordDoc.Application.ActiveDocument.InlineShapes[1].Height =120f;//图片高
  196. WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape().IncrementLeft(300.0f);//图片位置
  197. // WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
  198. //将图片设置浮动在文字上方
  199. //WdSaveFormat为Word 2003文档的保存格式
  200. object format = MSWord.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
  201. object path;
  202. path = "/Upload/Word/"+ "/工单"+ dt.Rows[0]["F_WorkOrderId"].ToString() + ".doc";
  203. path = HttpContext.Current.Server.MapPath("..") + path;
  204. if (File.Exists((string)path))
  205. {
  206. File.Delete((string)path);
  207. }
  208. //将wordDoc文档对象的内容保存为doc文档
  209. 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);
  210. WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
  211. WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  212. return message;
  213. }
  214. }