学习,C#操作Word,Asp.net技术

本文介绍了一种使用JavaScript去除字符串中的空格的方法,并详细展示了如何利用C#和Microsoft Word Interop组件来创建、编辑及保存Word文档,包括添加页眉、插入文字和图片等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、js去除字符串中的空格

 function trim(strToTrim) {

            return strToTrim.replace(/^\s+|\s+$/g, "")

        } 

2、数据生成word

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using QCW_Project.IBLL;
using QCW_Project.BLL;
using QCW_Project.Entity;
using System.IO;
using System.Web.Hosting;

namespace QCW_WebSite.Models
{
    public class ProjectToWord
    {
        private Microsoft.Office.Interop.Word.Application myWord;
        private Microsoft.Office.Interop.Word._Document myDoc;


        public void CreateAWord()
        {
            //实例化word应用对象   
            this.myWord = new Microsoft.Office.Interop.Word.ApplicationClass();
            Object myNothing = System.Reflection.Missing.Value;

            this.myDoc = this.myWord.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
        }


               /// <summary>
        /// 添加页眉
        /// </summary>
        /// <param name="pPageHeader"></param>
        public void SetPageHeader(string pPageHeader)
        {
            this.myWord.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
            this.myWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
            this.myWord.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
            //设置中间对齐   
            this.myWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //跳出页眉设置   
            this.myWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

        }
        /// <summary>
        /// 插入文字
        /// </summary>
        /// <param name="pText">文本信息</param>
        /// <param name="pFontSize">字体大小</param>
        /// <param name="pFontColor">字体颜色</param>
        /// <param name="pFontBold">字体粗体</param>
        /// <param name="ptextAlignment">方向</param>
        public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
        {
            //设置字体样式以及方向   
            this.myWord.Application.Selection.Font.Size = pFontSize;
            this.myWord.Application.Selection.Font.Bold = pFontBold;
            this.myWord.Application.Selection.Font.Color = pFontColor;
            this.myWord.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
            this.myWord.Application.Selection.TypeText(pText);
        }
        /// <summary>
        /// 换行
        /// </summary>
        public void NewLine()
        {
            this.myWord.Application.Selection.TypeParagraph();
        }
        /// <summary>
        /// 换页
        /// </summary>
        public void NewPage()
        {
            object breakType = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
            this.myWord.Application.Selection.InsertBreak(ref breakType);
        }
        public void InsertPicture(string pPictureFileName)
        {
            object myNothing = System.Reflection.Missing.Value;
            //图片居中显示   
            this.myWord.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            this.myWord.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
        }
        public void SaveWord(string pFileName)
        {
            //if (NFS.ClassLib.FileHelper.IsExistFile(pFileName))
            //{
            //    NFS.ClassLib.FileHelper.DeleteFile(pFileName);
            //}

            object myNothing = System.Reflection.Missing.Value;
            object myFileName = pFileName;
            object myWordFormatDocument = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
            object myLockd = false;
            object myPassword = "";
            object myAddto = true;
            try
            {
                this.myDoc.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
                    ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
                    ref myNothing, ref myNothing, ref myNothing);
                //this.myWord.Visible = true;
                //this.myDoc.Activate();
                this.myDoc.Close(ref myNothing, ref myNothing, ref myNothing);

 

                this.myWord.Quit(ref myNothing, ref myNothing, ref myNothing);

            }
            catch
            {
                throw new Exception("导出word文档失败!");
            }


        }
        public string GetTempFileName()
        {
            return DateTime.Now.ToString("yyyyMMddhhmmssfff");
        }
      
    }
}

3、下载word

 

private void DownWord(string str)
        {
            FileInfo DownloadFile = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + "\\" + str);
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

            Response.WriteFile(DownloadFile.FullName);

            Response.Flush();


            Response.End();
            //return View();
        }

 

 

转载于:https://www.cnblogs.com/wyux6868/archive/2009/05/27/1490606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值