c# 生成word文档

protected void btnBill_Click(object sender, EventArgs e)
    {
        InitInfo();
        string message = "";
        try
        {
            Object Nothing = System.Reflection.Missing.Value;
            Directory.CreateDirectory("C:/Documents and Settings/All Users/桌面");  //创建文件所在目录
            string name = Request.QueryString["order"].ToString() + "-对账单.doc";
            object filename = "C://Documents and Settings//All Users//桌面//" + name;  //文件保存路径
            //创建Word文档
            _Application WordApp = new ApplicationClass();
            Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            WordDoc.Paragraphs.Last.Range.Font.Size = 18;
            WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            WordDoc.Paragraphs.Last.Range.Text = "客户对账单\n\r";
            WordDoc.Paragraphs.Last.Range.Font.Size = 12;
            
            WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
            WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            WordDoc.Paragraphs.Last.Range.Text = "至(To):" + dt.Rows[0]["customerfullName"].ToString() + "\n";
            WordDoc.Paragraphs.Last.Range.Text = "由(From):德国帝彩(中国)有限公司\n\n";
            WordDoc.Paragraphs.Last.Range.Bold = 2;
            WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            WordDoc.Paragraphs.Last.Range.Text = "应收获确认书\n";
            WordDoc.Paragraphs.Last.Range.Bold = 0;
            //移动焦点并换行
            object count = 14;
            object WdLine = WdUnits.wdLine;//换一行;
            WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
            WordApp.Selection.TypeParagraph();//插入段落
            //文档中创建表格
            Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, dt.Rows.Count + 1, 4, ref Nothing, ref Nothing);
            //设置表格样式
            newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
            newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            newTable.Columns[1].Width = 100f;
            newTable.Columns[2].Width = 100f;
            newTable.Columns[3].Width = 100f;
            newTable.Columns[4].Width = 100f;
            /*/填充表格内容
            newTable.Cell(1, 1).Range.Text = "应收获确认书";
            newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
            //合并单元格
            newTable.Cell(1, 1).Merge(newTable.Cell(1, 4));
            WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
            WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
            */
            //设置标题
            newTable.Cell(1, 1).Range.Text = "产品型号";
            newTable.Cell(1, 2).Range.Text = "产品规格";
            newTable.Cell(1, 3).Range.Text = "产品面积";
            newTable.Cell(1, 4).Range.Text = "产品金额";
            decimal m = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                newTable.Cell(i + 2, 1).Range.Text = dt.Rows[i]["ProductModel"].ToString();
                newTable.Cell(i + 2, 2).Range.Text = dt.Rows[i]["ply"].ToString() + "*" + dt.Rows[i]["width"].ToString() + "*" + dt.Rows[i]["height"].ToString();
                newTable.Cell(i + 2, 3).Range.Text = dt.Rows[i]["quantity"].ToString();
                decimal money = Convert.ToInt32(dt.Rows[i]["quantity"]) * Convert.ToDecimal(dt.Rows[i]["price"]); m += money;
                newTable.Cell(i + 2, 4).Range.Text = money.ToString();
            }
            /*  WordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
              WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
              newTable.Cell(dt.Rows.Count + 2, 1).Range.Text = "合计:" + m.ToString();
              newTable.Cell(dt.Rows.Count + 2, 1).Merge(newTable.Cell(dt.Rows.Count + 2, 4));
            //  WordDoc.Content.Tables[1].Rows.Add(ref Nothing);*/
            WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            WordDoc.Paragraphs.Last.Range.Font.Color = WdColor.wdColorRed;
            WordDoc.Paragraphs.Last.Range.Text = "合计:" + Convert.ToDouble(m).ToString() + "\n\n\t";
            WordDoc.Paragraphs.Last.Range.Font.Color = WdColor.wdColorBlack;
            WordDoc.Paragraphs.Last.Range.Text = "    " + txtDes.Value + "\n\n\n";
            WordDoc.Paragraphs.Last.Range.Bold = 2;
            WordDoc.Paragraphs.Last.Range.Text = selCard.Value + "\n\n";
            WordDoc.Paragraphs.Last.Range.Text = "请将汇款底单传真于我公司,谢谢!" + "\n\n\n";
            WordDoc.Paragraphs.Last.Range.Bold = 0;
            WordDoc.Paragraphs.Last.Range.Text = DateTime.Now.ToShortDateString();
            WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
            //文件保存
            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);
            message = name + "文档已以保存到桌面";
        }
        catch
        {
            message = "文件导出异常!";
        }
        ClientScript.RegisterStartupScript(GetType(), "", "alert('" + message + "')", true);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值