wyux6868

 

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

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();
        }

 

 

posted on 2009-05-27 10:10  dream one minute  阅读(615)  评论(0编辑  收藏  举报

导航