| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data;
- using System.IO;
- using MSWord = Microsoft.Office.Interop.Word;
- /// <summary>
- ///DaoChuWord 的摘要说明
- /// </summary>
- public class ExportWord
- {
- public ExportWord()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
-
- // public DataTable dat = null;
- public void ProcessRequest(HttpContext context, string id, string name)
- {
- context.Response.ContentType = "text/plain";
- // GetDataTable(id);
- // CreateWord(name);
- //此处为了批量操作,把下面这个注掉
- //context.Response.End();
-
- }
- public string CreateWord(string name)
- {
- string message = "";
- Object Nothing = System.Reflection.Missing.Value;
- object filename = name; //文件保存路径
- //创建Word文档
- Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
- Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
- WordApp.Selection.PageSetup.LeftMargin = 50f;
- WordApp.Selection.PageSetup.RightMargin = 50f;
- WordApp.Selection.PageSetup.PageWidth = 650f; //页面宽度
- WordDoc.ActiveWindow.Selection.Font.Bold = 2;
- WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距
- //移动焦点并换行
- object count = 14;
- object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
- WordApp.Selection.ParagraphFormat.LineSpacing = 18f;
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- WordApp.Selection.TypeParagraph();//插入段落
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- WordApp.Selection.Font.Size = 18f;
- object missing = System.Reflection.Missing.Value;
- object WdLine2 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
- WordApp.Selection.Text = "安 阳 市 12345 政 务 服 务 热 线 交 办 单";
- WordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- WordApp.Selection.TypeParagraph();//插入段落
- WordApp.Selection.Font.Size = 10.5f;
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
- //文档中创建表格
- Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 18, 9, ref Nothing, ref Nothing);
- //设置表格样式
- for (int i = 1; i < 10; i ++)
- {
- if (i ==9)
- {
- newTable.Columns[i].Width = 80f;
- }
- else
- newTable.Columns[i].Width = 60f;
- }
- for (int i=1;i<19;i++)
- {
- newTable.Rows[i].Height = 30f;
- }
- //表格外线
- newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
- //表格内线
- newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
- //垂直居中
- object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
- object countjz = 1;
- WordApp.Selection.MoveEnd(ref unit, ref countjz);
- WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
- //填充表格内容 第一行
- newTable.Cell(1, 1).Range.Text = "事项编号";
- newTable.Cell(1, 1).Merge(newTable.Cell(2, 1));
- newTable.Cell(1, 2).Range.Text ="";//工单编号
- newTable.Cell(1, 2).Merge(newTable.Cell(2, 3));
- newTable.Cell(1, 3).Range.Text = "工单来源";
- newTable.Cell(1, 4).Range.Text = "";
- newTable.Cell(1, 5).Range.Text = "办理时限";
- newTable.Cell(1, 6).Range.Text = "";
- newTable.Cell(1, 7).Range.Text = "联系电话";
- newTable.Cell(2, 3).Range.Text = "签收时间";
- newTable.Cell(2, 4).Range.Text = "";
- newTable.Cell(2, 5).Range.Text = "办理时间";
- newTable.Cell(2, 6).Range.Text = "";
- newTable.Cell(2, 7).Range.Text = "来电人";
- newTable.Cell(3, 1).Range.Text = "事项区域";
- newTable.Cell(3, 2).Range.Text = "";
- newTable.Cell(3, 2).Merge(newTable.Cell(3, 9));
- //填充表格内容 第四行
- newTable.Cell(4, 1).Range.Text = "内容摘要";
- newTable.Cell(4, 1).Merge(newTable.Cell(7, 1));
- newTable.Cell(4, 2).Range.Text =" ";
- newTable.Cell(4, 2).Merge(newTable.Cell(7, 9));
- newTable.Cell(5, 1).Range.Text = "承办单位";
- newTable.Cell(5, 2).Range.Text = " ";
- newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));
- newTable.Cell(5, 3).Range.Text = "协办单位";
- newTable.Cell(5, 4).Range.Text = " ";
- newTable.Cell(5, 4).Merge(newTable.Cell(5, 7));
- newTable.Cell(6, 1).Range.Text = "调度意见";
- newTable.Cell(6, 1).Merge(newTable.Cell(8, 1));
- newTable.Cell(6, 2).Range.Text = " ";
- newTable.Cell(6, 2).Merge(newTable.Cell(8, 9));
- newTable.Cell(7, 1).Range.Text = "承办意见";
- newTable.Cell(7, 1).Merge(newTable.Cell(8, 1));
- newTable.Cell(7, 2).Range.Text = " ";
- newTable.Cell(7, 2).Merge(newTable.Cell(8, 9));
- newTable.Cell(8, 1).Range.Text = "领导批示";
- newTable.Cell(8, 1).Merge(newTable.Cell(9, 1));
- newTable.Cell(8, 2).Range.Text = "";
- newTable.Cell(8, 2).Merge(newTable.Cell(9, 9));
- newTable.Cell(9, 1).Range.Text = "处理结果";
- newTable.Cell(9, 1).Merge(newTable.Cell(12, 1));
- newTable.Cell(9, 2).Range.Text = " ";
- newTable.Cell(9, 2).Merge(newTable.Cell(12, 9));
- string FileName = "/Upload/Word/" + "/word.png";
- FileName = HttpContext.Current.Server.MapPath("..") + FileName;
- newTable.Cell(6, 2).Select();//选中一行
- 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 = 110f;//图片宽度
- WordDoc.Application.ActiveDocument.InlineShapes[1].Height =100f;//图片高
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
- //将图片设置浮动在文字上方
- WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;
- WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
- object unite = MSWord.WdUnits.wdStory;
- WordApp.Selection.EndKey(ref unite, ref Nothing); //将光标移动到文档末尾
- WordDoc.Content.InsertAfter("联系电话:" + ""+" "
- +" 审核员"+""+" " +" 调度员");
- //WdSaveFormat为Word 2003文档的保存格式
- object format = MSWord.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
- object path;
-
- path = "/Upload/Word/"+ "/工单.doc";
- path = HttpContext.Current.Server.MapPath("..") + path;
- if (File.Exists((string)path))
- {
- File.Delete((string)path);
- }
- //将wordDoc文档对象的内容保存为doc文档
- 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);
- WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
- WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
- return message;
- }
- }
|