麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

c#操作word表格

2019-11-18 19:10:42
字體:
來源:轉載
供稿:網友

最近由于工作需要,做了一些關于c#操作Word文檔方面的工作.主要是對word中表格的操作,以下是部分代碼,關于操作不規則表格的.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.sessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

using System.Reflection;
using System.Runtime.InteropServices ;
using System.Threading;

public void MakeMyTable(DataTable DT,string strFilePath)
  {
  
   string strEnd   = this.txtEnd.Text.Trim().ToString();
   string strStart = this.txtStart.Text.Trim().ToString();
//生成文檔分頁中的起始和終止頁
   string strSign = "("+strStart + "-" + strEnd + ")";

   //殺掉所有word進程以保證速度
   //KillWordPRocess();

   object Nothing = System.Reflection.Missing.Value;
   object missing = System.Reflection.Missing.Value;
   object filename= strFilePath; 

   Word.application wordApp=new Word.ApplicationClass();
   Word.Document wordDoc=wordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);

   try
   {
    //生成過程中屏蔽返回按扭,不允許中途停止
    Button2.Enabled = false;
    #region 生成文檔
    //設置文檔寬度
    wordApp.Selection.PageSetup.LeftMargin  = wordApp.CentimetersToPoints(float.Parse("2"));
    wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11 ;
    wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2"));

    Object start = Type.Missing;
    Object end = Type.Missing;
    Object unit = Type.Missing;
    Object count = Type.Missing;
    wordDoc.Range(ref start, ref end).Delete(ref unit, ref count);


    object rng = Type.Missing;
    string strInfo = this.txtNameCh.Text.ToString()+"明細表"+strSign+"/r/n";
    start = 0;
    end = 0;
    wordDoc.Range(ref start, ref end).InsertBefore(strInfo);
    wordDoc.Range(ref start, ref end).Font.Name = "Verdana";
    wordDoc.Range(ref start, ref end).Font.Size = 20;
    wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

    start = 8;
    end = strInfo.Length;
    wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回車

    if(DT.Rows.Count>0)
    {
     //存在數據項
     //添加一個表格
     object missingValue = Type.Missing;
     object location = strInfo.Length; //注:若location超過已有字符的長度將會出錯。一定要比"明細表"串多一個字符
     Word.Range rng2 = wordDoc.Range(ref location, ref location);

     wordDoc.Tables.Add(rng2, 13, 6, ref missingValue, ref missingValue);
     wordDoc.Tables.Item(1).Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightAtLeast;
     wordDoc.Tables.Item(1).Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
     wordDoc.Tables.Item(1).Range.Font.Size = 10;
     wordDoc.Tables.Item(1).Range.Font.Name = "宋體";
     wordDoc.Tables.Item(1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
     wordDoc.Tables.Item(1).Range.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
     //設置表格樣式
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderLeft).Color = Word.WdColor.wdColorAutomatic;
    
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderTop).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderTop).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderTop).Color = Word.WdColor.wdColorAutomatic;

     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderBottom).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderBottom).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderBottom).Color = Word.WdColor.wdColorAutomatic;

     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderHorizontal).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderHorizontal).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderHorizontal).Color = Word.WdColor.wdColorAutomatic;

     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderVertical).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderVertical).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderVertical).Color = Word.WdColor.wdColorAutomatic;
    
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderRight).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderRight).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     wordDoc.Tables.Item(1).Borders.Item(Word.WdBorderType.wdBorderRight).Color = Word.WdColor.wdColorAutomatic;

     //     wordDoc.Tables.Item(k).Borders.Item(Word.WdBorderType.wdBorderDiagonalDown).LineStyle = Word.WdLineStyle.wdLineStyleSingle;
     //     wordDoc.Tables.Item(k).Borders.Item(Word.WdBorderType.wdBorderDiagonalDown).LineWidth = Word.WdLineWidth.wdLineWidth050pt;
     //     wordDoc.Tables.Item(k).Borders.Item(Word.WdBorderType.wdBorderDiagonalDown).Color = Word.WdColor.wdColorAutomatic;
     //第一行顯示
     wordDoc.Tables.Item(1).Cell(1,2).Merge(wordDoc.Tables.Item(1).Cell(1,3));
     wordDoc.Tables.Item(1).Cell(1,4).Merge(wordDoc.Tables.Item(1).Cell(1,5));

     //第二行顯示
     wordDoc.Tables.Item(1).Cell(2,5).Merge(wordDoc.Tables.Item(1).Cell(2,6));
     wordDoc.Tables.Item(1).Cell(1,4).Merge(wordDoc.Tables.Item(1).Cell(2,5));

     #region 插入數據行
     wordDoc.Tables.Item(1).Cell(1, 1).Range.Text = "cell11";
     //wordDoc.Tables.Item(k).Cell(1, 2).Range.Text = DT.Rows[i]["cell11"].ToString();


     ////******************
     wordDoc.Tables.Item(1).Cell(1, 3).Range.Text = "cell13";
     //wordDoc.Tables.Item(k).Cell(1, 4).Range.Text = DT.Rows[i]["cell13"].ToString();


     ////******************
     wordDoc.Tables.Item(1).Cell(2, 1).Range.Text = "cell21";
     //wordDoc.Tables.Item(k).Cell(2, 2).Range.Text = DT.Rows[i]["cell21"].ToString();


     ////******************
     wordDoc.Tables.Item(1).Cell(2, 3).Range.Text = "cell23";
     //wordDoc.Tables.Item(k).Cell(2, 4).Range.Text = DT.Rows[i]["cell23"].ToString();

     #endregion


     #region 第三行顯示
     wordDoc.Tables.Item(1).Cell(3,2).Merge(wordDoc.Tables.Item(1).Cell(3,3));
     wordDoc.Tables.Item(1).Cell(3,2).Merge(wordDoc.Tables.Item(1).Cell(3,3));

     ////******************
     wordDoc.Tables.Item(1).Cell(3, 1).Range.Text = "cell31";
    
    


     ////******************
     wordDoc.Tables.Item(1).Cell(3, 3).Range.Text = "cell33";
    

     #endregion


     #region 第五行顯示
     wordDoc.Tables.Item(1).Cell(5,2).Merge(wordDoc.Tables.Item(1).Cell(5,3));
     wordDoc.Tables.Item(1).Cell(5,2).Merge(wordDoc.Tables.Item(1).Cell(5,3));
     wordDoc.Tables.Item(1).Cell(5,2).Merge(wordDoc.Tables.Item(1).Cell(5,3));
     wordDoc.Tables.Item(1).Cell(5,2).Merge(wordDoc.Tables.Item(1).Cell(5,3));
     #endregion


     #region  第四行顯示

     ////******************
     wordDoc.Tables.Item(1).Cell(4, 1).Range.Text = "cell41";
    


     ////******************
     wordDoc.Tables.Item(1).Cell(4, 3).Range.Text = "cell43";
    


     ////******************
     wordDoc.Tables.Item(1).Cell(4, 5).Range.Text = "cell45";
    
     #endregion


     #region 第六行顯示
     wordDoc.Tables.Item(1).Cell(6,2).Merge(wordDoc.Tables.Item(1).Cell(6,3));
     wordDoc.Tables.Item(1).Cell(6,2).Merge(wordDoc.Tables.Item(1).Cell(6,3));
     wordDoc.Tables.Item(1).Cell(6,2).Merge(wordDoc.Tables.Item(1).Cell(6,3));
     wordDoc.Tables.Item(1).Cell(6,2).Merge(wordDoc.Tables.Item(1).Cell(6,3));


     ////******************
     wordDoc.Tables.Item(1).Cell(5, 1).Range.Text = "cell51";
    
     wordDoc.Tables.Item(1).Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;

 

     ////******************
     wordDoc.Tables.Item(1).Cell(6, 1).Range.Text = "cdll61";
    
     wordDoc.Tables.Item(1).Cell(6, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
     #endregion


     #region 第七行顯示
     wordDoc.Tables.Item(1).Cell(7,2).Merge(wordDoc.Tables.Item(1).Cell(7,3));
     wordDoc.Tables.Item(1).Cell(7,2).Merge(wordDoc.Tables.Item(1).Cell(7,3));
     wordDoc.Tables.Item(1).Cell(7,2).Merge(wordDoc.Tables.Item(1).Cell(7,3));
     wordDoc.Tables.Item(1).Cell(7,2).Merge(wordDoc.Tables.Item(1).Cell(7,3));

     ////******************
     wordDoc.Tables.Item(1).Cell(7, 1).Range.Text = "cell71";
    
     wordDoc.Tables.Item(1).Cell(7, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
     #endregion


     #region 第八行顯示
     wordDoc.Tables.Item(1).Cell(8,1).Merge(wordDoc.Tables.Item(1).Cell(8,2));
     wordDoc.Tables.Item(1).Cell(8,2).Merge(wordDoc.Tables.Item(1).Cell(8,3));
     wordDoc.Tables.Item(1).Cell(8,2).Merge(wordDoc.Tables.Item(1).Cell(8,3));
     wordDoc.Tables.Item(1).Cell(8,2).Merge(wordDoc.Tables.Item(1).Cell(8,3));

    
     #endregion


     #region 第九行顯示
     wordDoc.Tables.Item(1).Cell(9,1).Merge(wordDoc.Tables.Item(1).Cell(9,2));
     wordDoc.Tables.Item(1).Cell(9,3).Merge(wordDoc.Tables.Item(1).Cell(9,4));

     ////******************
     wordDoc.Tables.Item(1).Cell(9, 1).Range.Text = "cell91";
     //wordDoc.Tables.Item(k).Cell(9, 2).Range.Text =  (DT.Rows[i]["cell91"].ToString()=="1"?"有":"無");

 

     ////******************
     wordDoc.Tables.Item(1).Cell(9, 3).Range.Text = "cell93";
  
     #endregion


     #region 第十行顯示
     wordDoc.Tables.Item(1).Cell(10,1).Merge(wordDoc.Tables.Item(1).Cell(10,2));
     wordDoc.Tables.Item(1).Cell(10,3).Merge(wordDoc.Tables.Item(1).Cell(10,4));
     ////******************
     wordDoc.Tables.Item(1).Cell(10, 1).Range.Text = "cell101";
    


     ////******************
     wordDoc.Tables.Item(1).Cell(10, 3).Range.Text = "cdll103";
     //wordDoc.Tables.Item(k).Cell(10, 4).Range.Text = (DT.Rows[i]["Label"].ToString()=="1"?"有":"無");
     #endregion


     #region 第十一行顯示
     wordDoc.Tables.Item(1).Cell(11,1).Merge(wordDoc.Tables.Item(1).Cell(11,2));
     wordDoc.Tables.Item(1).Cell(11,3).Merge(wordDoc.Tables.Item(1).Cell(11,4));
     ////******************
     wordDoc.Tables.Item(1).Cell(11, 1).Range.Text = "cell111";
    


     ////******************
     wordDoc.Tables.Item(1).Cell(11, 3).Range.Text = "cell113";
  
     #endregion


     #region 第十二行顯示
     wordDoc.Tables.Item(1).Cell(12,1).Merge(wordDoc.Tables.Item(1).Cell(12,2));
     wordDoc.Tables.Item(1).Cell(12,3).Merge(wordDoc.Tables.Item(1).Cell(12,4));
     ////******************
     wordDoc.Tables.Item(1).Cell(12, 1).Range.Text = "cell121";
    

     ////******************
     wordDoc.Tables.Item(1).Cell(12, 3).Range.Text = "cell123";

     #endregion


     #region 第十三行顯示
     wordDoc.Tables.Item(1).Cell(13,1).Merge(wordDoc.Tables.Item(1).Cell(13,2));
     wordDoc.Tables.Item(1).Cell(13,3).Merge(wordDoc.Tables.Item(1).Cell(13,4));

     ////******************
     wordDoc.Tables.Item(1).Cell(13, 1).Range.Text = "cell131";
    

     ////******************
     wordDoc.Tables.Item(1).Cell(13, 3).Range.Text = "cell133";
 
     #endregion

     wordDoc.Tables.Item(1).Select();
     wordApp.Application.Selection.Cut();

     //重新成聲所有表

  
    

     for(int i = 0; i<=DT.Rows.Count-1;i++)
     {
      wordApp.Application.Selection.Paste();
      int k = i+1;
      #region    更新數據


      #region 插入數據行
     
      wordDoc.Tables.Item(k).Cell(1, 2).Range.Text = DT.Rows[i]["1"].ToString();


      ////******************
     
      wordDoc.Tables.Item(k).Cell(1, 4).Range.Text = DT.Rows[i]["2"].ToString();


      ////******************
      
      wordDoc.Tables.Item(k).Cell(2, 2).Range.Text = DT.Rows[i]["3"].ToString();


      ////******************
     
      wordDoc.Tables.Item(k).Cell(2, 4).Range.Text = DT.Rows[i]["4"].ToString();

      #endregion


      #region 第三行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(3, 2).Range.Text = DT.Rows[i]["5"].ToString();
      ////******************
     
      wordDoc.Tables.Item(k).Cell(3, 4).Range.Text = DT.Rows[i]["6"].ToString();

      #endregion


      #region 第五行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(5, 2).Range.Text = DT.Rows[i]["7"].ToString();
      wordDoc.Tables.Item(k).Cell(5, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
      #endregion

      #region  第四行顯示

      ////******************
     
      wordDoc.Tables.Item(k).Cell(4, 2).Range.Text = DT.Rows[i]["8"].ToString();


      ////******************
     
      wordDoc.Tables.Item(k).Cell(4, 4).Range.Text = DT.Rows[i]["9"].ToString();


      ////******************
     
      wordDoc.Tables.Item(k).Cell(4, 6).Range.Text = DT.Rows[i]["0"].ToString();
      #endregion


      #region 第六行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(6, 2).Range.Text = DT.Rows[i]["11"].ToString();
      wordDoc.Tables.Item(k).Cell(6, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
      #endregion


      #region 第七行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(7, 2).Range.Text = DT.Rows[i]["12"].ToString();
      wordDoc.Tables.Item(k).Cell(7, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
      #endregion


      #region 第八行顯示
      ////******************
     
      string strTechlevel = DT.Rows[i]["Level"].ToString();
      string returnTechlevel = "";
      switch(strTechlevel)
      {
       case "1":
        returnTechlevel = "Level1";
        break;
       case "2":
        returnTechlevel = "Level2";
        break;
       case "3":
        returnTechlevel = "Level3";
        break;
       case "0":
        returnTechlevel = "Level4";
        break;
       default:
        returnTechlevel = "Level5";
        break;
      }
      wordDoc.Tables.Item(k).Cell(8, 2).Range.Text = returnTechlevel;

      #endregion


      #region 第九行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(9, 2).Range.Text =  (DT.Rows[i]["14"].ToString()=="1"?"有":"無");

 

      ////******************
     
      wordDoc.Tables.Item(k).Cell(9, 4).Range.Text = (DT.Rows[i]["15"].ToString()=="1"?"是":"否");
      #endregion


      #region 第十行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(10, 2).Range.Text =  (DT.Rows[i]["16"].ToString()=="1"?"有":"無");


      ////******************
     
      wordDoc.Tables.Item(k).Cell(10, 4).Range.Text = (DT.Rows[i]["17"].ToString()=="1"?"有":"無");
      #endregion


      #region 第十一行顯示

      ////******************
     
      wordDoc.Tables.Item(k).Cell(11, 2).Range.Text =  (DT.Rows[i]["18"].ToString()=="1"?"是":"否");


      ////******************
     
      wordDoc.Tables.Item(k).Cell(11, 4).Range.Text = (DT.Rows[i]["19"].ToString()=="1"?"是":"否");
      #endregion


      #region 第十二行顯示
      ////******************
     
      wordDoc.Tables.Item(k).Cell(12, 2).Range.Text =  (DT.Rows[i]["20"].ToString()=="1"?"是":"否");

      ////******************
     
      wordDoc.Tables.Item(k).Cell(12, 4).Range.Text =  (DT.Rows[i]["21"].ToString()=="1"?"是":"否");
      #endregion


      #region 第十三行顯示
    
     
      wordDoc.Tables.Item(k).Cell(13, 2).Range.Text =  (DT.Rows[i]["22"].ToString()=="1"?"是":"否");

      ////******************
     
      wordDoc.Tables.Item(k).Cell(13, 4).Range.Text =  (DT.Rows[i]["23"].ToString()=="1"?"是":"否");
      #endregion

      #endregion

      //插入分頁
      if(i!=DT.Rows.Count-1)
      {
       object mymissing = System.Reflection.Missing.Value;
       object myunit = Word.WdUnits.wdStory;
       wordApp.Selection.EndKey(ref myunit,ref mymissing);

       object pBreak= (int)Word.WdBreakType.wdPageBreak;
       wordApp.Selection.InsertBreak( ref pBreak );
      }
     }
   

     wordDoc.SaveAs(ref filename, ref missing,ref missing, ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing, ref missing);
  
  
     wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
     wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

     if ( wordDoc != null )
     {
      System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
      wordDoc = null;
     }
     if ( wordApp != null )
     {
      System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
      wordApp = null;
     }
     GC.Collect();
     //KillWordProcess();
     string strUrl = "MakeWordFile.aspx?username="+Request.QueryString["username"].ToString();
     utility.ShowPopMessage("文檔生成完畢!",strUrl);
    }
    else
    {
     utility.ShowPopMessage("無任何數據!");
    }
    #endregion

  

   }
   catch
   {
    wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
    wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
    if ( wordDoc != null )
    {
     System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
     wordDoc = null;
    }
    if ( wordApp != null )
    {
     System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
     wordApp = null;
    }
    GC.Collect();
    utility.ShowPopMessage("文檔生成失?。?);

   }
  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产91久久精品一区二区 | 国产小视频在线观看 | 欧美xxxx精品另类 | 欧美女优一区 | 亚洲性爰 | 欧美成人三级视频 | 免费黄色在线 | 国产精品久久久久久久久岛 | 女人久久久www免费人成看片 | 人成久久 | 黄色片快播 | 午夜a狂野欧美一区二区 | 色婷婷久久久久久 | 欧美三级欧美成人高清www | 黄色片网站在线看 | 午夜天堂在线 | 色妇视频| 久久久久久久亚洲精品 | 欧美一级黄 | 色污视频| 性欧美暴力猛交69hd | 91精品国产日韩91久久久久久360 | 亚洲成人欧美 | 99视频在线观看视频 | 毛片视频大全 | 日本中文视频 | 欧美亚洲免费 | 一级大黄毛片 | 亚洲福利在线视频 | 激情视频免费看 | 97视频| 看全色黄大色黄大片女图片 | 国产亚洲高清视频 | a在线视频| 日本一区二区在线 | 久久国产精品免费视频 | 特一级黄色毛片 | 一本色道久久综合亚洲精品图片 | 中文字幕免费在线观看视频 | 在线播放黄色网址 | 久久色伦理资源站 |