关于 c# 操作 world

把数据存放在datatable 中并循环取出来数据然后再保存在world中 

    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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值