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 string CreateWord(DataTable dt, DataTable jbdata,string usercode)
      {
            string message = "";
            Object Nothing = System.Reflection.Missing.Value;
            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);
        //设置纸张为A4纸大小
        WordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;
              WordDoc.ActiveWindow.Selection.Font.Bold = 2;
             WordApp.Selection.ParagraphFormat.LineSpacing = 13f;//设置文档的行间距     
        WordDoc.PageSetup.LeftMargin = 45f;
        WordDoc.PageSetup.RightMargin  = 45f;
        //移动焦点并换行 
        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 = 11f;
              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, 8, ref Nothing, ref Nothing);
             //设置表格样式 
            for (int i = 1; i < 9; i ++)
            {
            if (i ==2)
            {
                newTable.Columns[i].Width = 75f;
            }
            else  if (i ==8)
            {
                newTable.Columns[i].Width = 80f;
            }
            else if (i ==4)
            {
                newTable.Columns[i].Width = 65f;
            }
            else if (i == 6)
            {
                newTable.Columns[i].Width = 65f;
            }
            else  
            newTable.Columns[i].Width = 60f;
            }
            for (int i=1;i<19;i++)
           {
            newTable.Rows[i].Height = 32f;
           }
        string user = "";
          if (jbdata.Rows.Count > 0)
             user = jbdata.Rows[0]["F_CreateUser"].ToString();
        //表格外线
        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 = dt.Rows[0]["F_WorkOrderId"].ToString();
        newTable.Cell(1, 2).Range.Font.Bold  = 0;
        newTable.Cell(1, 2).Merge(newTable.Cell(2, 2));
             newTable.Cell(1, 3).Range.Text = "工单来源";
             newTable.Cell(1, 4).Range.Text = dt.Rows[0]["SourceName"].ToString();
        newTable.Cell(1, 4).Range.Font.Bold = 0;
        newTable.Cell(1, 5).Range.Text = "办理时限";
        if (jbdata.Rows.Count > 0)
        {
            if (jbdata.Rows[0]["F_LimitTime"]!=null && jbdata.Rows[0]["F_LimitTime"].ToString()!="")
            newTable.Cell(1, 6).Range.Text =DateTime .Parse ( jbdata.Rows[0]["F_LimitTime"].ToString()).ToString ("yyyy-MM-dd");
            else
                newTable.Cell(1, 6).Range.Text = "";
        }
           
        else
            newTable.Cell(1, 6).Range.Text = "";
        newTable.Cell(1, 6).Range.Font.Bold = 0;
        newTable.Cell(1, 7).Range.Text = "联系电话";
           newTable.Cell(1, 8).Range.Text = dt.Rows[0]["F_CusPhone"].ToString();
        newTable.Cell(1, 8).Range.Font.Bold = 0;
        newTable.Cell(2, 3).Range.Text = "签收时间";
        if (jbdata.Rows.Count > 0)
        {
            if (jbdata.Rows[0]["F_SureTime"] != null && jbdata.Rows[0]["F_SureTime"].ToString() != "")
                newTable.Cell(2, 4).Range.Text = DateTime.Parse(jbdata.Rows[0]["F_SureTime"].ToString()).ToString("yyyy-MM-dd");
            else
                newTable.Cell(2, 4).Range.Text = "";
            
        }
        else
            newTable.Cell(2, 4).Range.Text = "";
        newTable.Cell(2, 4).Range.Font.Bold = 0;
        newTable.Cell(2, 5).Range.Text = "办理时间";
        if (dt.Rows[0]["F_CreateTime"] != null && dt.Rows[0]["F_CreateTime"].ToString() != "")
            newTable.Cell(2, 6).Range.Text = DateTime.Parse(dt.Rows[0]["F_CreateTime"].ToString()).ToString("yyyy-MM-dd");
        else  
        newTable.Cell(2, 6).Range.Text = dt.Rows[0]["F_CreateTime"].ToString();
        newTable.Cell(2, 6).Range.Font.Bold = 0;
        newTable.Cell(2, 7).Range.Text = "来电人";
        newTable.Cell(2, 8).Range.Text = dt.Rows[0]["F_CusName"].ToString();
        newTable.Cell(2,8).Range.Font.Bold = 0;
        newTable.Cell(3, 1).Range.Text = "事项区域";
        newTable.Cell(3, 2).Range.Text = dt.Rows[0]["F_SourceAddress"].ToString();
        newTable.Cell(3, 2).Merge(newTable.Cell(3, 8));
        newTable.Cell(3, 2).Range.Font.Bold = 0;
        newTable.Cell(4, 1).Range.Text = "内容摘要";
            newTable.Cell(4, 1).Merge(newTable.Cell(7, 1));
        if (dt.Rows[0]["F_Content"] != null && dt.Rows[0]["F_Content"].ToString() != "")
        {
            newTable.Cell(4, 2).Range.Text = dt.Rows[0]["F_Content"].ToString();
        }
        else
        {
            newTable.Cell(4, 2).Range.Text = dt.Rows[0]["F_ComContent"].ToString();
        }
        newTable.Cell(4, 2).Merge(newTable.Cell(7, 8));
        newTable.Cell(4, 2).Range.Font.Bold = 0;
        newTable.Cell(4, 1).Height = 130f;
        newTable.Cell(4, 2).Height = 130f;
        newTable.Cell(5, 1).Range.Text = "承办单位";
        if (jbdata.Rows.Count > 0)
            newTable.Cell(5, 2).Range.Text = jbdata.Rows[0]["DeptName"].ToString();
        newTable.Cell(5, 2).Merge(newTable.Cell(5, 4));
        newTable.Cell(5, 2).Range.Font.Bold = 0;
        newTable.Cell(5, 3).Range.Text = "协办单位";
        if (jbdata.Rows.Count > 0)
            newTable.Cell(5, 4).Range.Text = jbdata.Rows[0]["OtherDeptName"].ToString();
        newTable.Cell(5, 4).Merge(newTable.Cell(5, 6));
        newTable.Cell(5, 4).Range.Font.Bold = 0;
        newTable.Cell(6, 1).Range.Text = "调度意见";
        newTable.Cell(6, 1).Merge(newTable.Cell(8, 1));
        if (jbdata.Rows.Count > 0)
            newTable.Cell(6, 2).Range.Text = jbdata.Rows[0]["F_AssignedOpinion"].ToString();
        newTable.Cell(6, 2).Merge(newTable.Cell(8, 8));
        newTable.Cell(6, 2).Range.Font.Bold = 0;
        newTable.Cell(6, 1).Height = 90f;
        newTable.Cell(6, 2).Height = 90f;
        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, 8));
        newTable.Cell(7, 1).Height = 50f;
        newTable.Cell(7, 2).Height = 50f;
        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, 8));
        newTable.Cell(8, 1).Height = 50f;
        newTable.Cell(8, 2).Height = 50f;
        newTable.Cell(9, 1).Range.Text = "处理结果";
        newTable.Cell(9, 1).Merge(newTable.Cell(12, 1));
        newTable.Cell(9, 2).Range.Text = dt.Rows[0]["F_Result"].ToString();
        newTable.Cell(9, 2).Merge(newTable.Cell(12, 8));
        newTable.Cell(9, 2).Range.Font.Bold = 0;
        newTable.Cell(9, 1).Height = 140f;
        newTable.Cell(9, 2).Height = 140f;
        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("联系电话：" + "(0372)12345"+ "                " + "审核员:" + usercode + "         " + "       调度员:" + user);
        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 = 120f;//图片宽度 
       WordDoc.Application.ActiveDocument.InlineShapes[1].Height =120f;//图片高 
        WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape().IncrementLeft(300.0f);//图片位置
       // WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

       

       
        //将图片设置浮动在文字上方
        
     
       
        //WdSaveFormat为Word 2003文档的保存格式
        object format = MSWord.WdSaveFormat.wdFormatDocument;// office 2007就是wdFormatDocumentDefault
        object path;
       
        path = "/Upload/Word/"+ "/工单"+ dt.Rows[0]["F_WorkOrderId"].ToString() + ".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;
     }
}