關於 c# 操做 world

把數據存放在datatable 中並循環取出來數據而後再保存在world中 html

    protected void ExportToWord(DataSet Ads)
    {
        try
        {
          


            Object Nothing = System.Reflection.Missing.Value;
            //Directory.CreateDirectory("C:/CNSI"); //建立文件所在目錄
            //string name = "CNSI_" + "53asdf" + ".doc";
            //object filename = "C://CNSI//" + name; //文件保存路徑
            //建立Word文檔
            Word.Application WordApp = new Word.ApplicationClass();
            Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//設置文檔的行間距

            int k = 0;

            //文檔中建立表格
            for (int i = 0; i < 10; i++)
            {

                //表頭
                Word.Table newTableH = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 1, ref Nothing, ref Nothing);
                newTableH.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
                newTableH.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
                //填充表格內容
                newTableH.Cell(1, 1).Range.Text = "班組交接記錄表";
                newTableH.Cell(1, 1).Range.Bold = 2;//設置單元格中字體爲粗體
                WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
                k += 2;
                object countH = (object)k;
                object WdLineH = Word.WdUnits.wdLine;//換一行;
                WordApp.Selection.MoveDown(ref WdLineH, ref countH, ref Nothing);//移動焦點
                WordApp.Selection.TypeParagraph();//插入段落

                //時間
                Word.Table newTableT = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 1, ref Nothing, ref Nothing);
                newTableT.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
                newTableT.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
                WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;//水平居中
                //填充表格內容
                newTableT.Cell(1, 1).Range.Text = "2013年3月1日";
                newTableT.Cell(1, 1).Range.Bold = 1;//設置單元格中字體爲粗體
                k += 3;
                object countT = (object)k;
                object WdLineT = Word.WdUnits.wdLine;//換一行;
                WordApp.Selection.MoveDown(ref WdLineT, ref countT, ref Nothing);//移動焦點
                WordApp.Selection.TypeParagraph();//插入段落

                //主界面
                Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 20, 1, ref Nothing, ref Nothing);
                newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleDot;
                WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居中
                //填充表格內容
                newTable.Cell(1, 1).Range.Text = "崗位人員:";
                newTable.Cell(1, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(3, 1).Range.Text = "崗位班前交接內容:";
                newTable.Cell(3, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(5, 1).Range.Text = "本班生產運行狀況:";
                newTable.Cell(5, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(7, 1).Range.Text = "上級工做指令:";
                newTable.Cell(7, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(9, 1).Range.Text = "橫向聯繫記錄:";
                newTable.Cell(9, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(11, 1).Range.Text = "存在的問題及處理狀況:";
                newTable.Cell(11, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(13, 1).Range.Text = "下班注意事項:";
                newTable.Cell(13, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(15, 1).Range.Text = "班後交接內容:";
                newTable.Cell(15, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(16, 1).Range.Text = "工具用具是否無缺、齊全:";
                newTable.Cell(16, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(17, 1).Range.Text = "崗位衛生狀況是否合格:";
                newTable.Cell(17, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(18, 1).Range.Text = "崗位防禦用品是否無缺:";
                newTable.Cell(18, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(19, 1).Range.Text = "消防器材及設施是否無缺:";
                newTable.Cell(19, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                newTable.Cell(20, 1).Range.Text = "交班簽名:" + "****" + "接班簽名" + "***";
                newTable.Cell(20, 1).Range.Bold = 1;//設置單元格中字體爲粗體

                k += 25;
                object count = (object)k;
                object WdLine = Word.WdUnits.wdLine;//換一行;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移動焦點
                WordApp.Selection.TypeParagraph();//插入段落
                object breakPage = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
                WordApp.Selection.InsertBreak(ref breakPage);

            }

            //文件保存
            //WordDoc.SaveAs(ref filename, 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, ref Nothing);
            WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);//保存
            WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);//結束進程

        }
        catch (Exception ex)
        {
            StreamOperate.StreamWrite("交接班記錄查詢", "ExportToWord", ex.Message, ex.StackTrace);
        }





    }
Ads 爲從數據庫中取出來的數據,我這裏用了10個班組記錄作的例子。只需賦值就能夠。                

參考網址:
http://hi.baidu.com/ishlzjpduibcuyd/item/8637d50e428d4ceb359902b7

  http://m.blog.csdn.net/blog/huguoqun/9332177

          http://www.cnblogs.com/koolay/articles/1398110.html數據庫

相關文章
相關標籤/搜索