基于ASP.NET批量导出Word文件并打包下载

服务器需要安装office 2003,以便在程序中引入Microsoft.Office.Interop.Word.dll,需要制作Word模板,在Word模板中插入标签,下面是具体实现代码

 

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;


namespace System.Web.Mvc
{
public class WordOp
{
private ApplicationClass WordApp;
private Document WordDoc;
private static bool isOpened = false;//判断word模板是否被占用

/// <summary>
/// 保存Word文档
/// </summary>
/// <param name="strFname">文件物理路径</param>
/// <param name="isReplace">是否替换</param>
public void SaveAs(string strFname, bool isReplace)
{
if (isReplace && File.Exists(strFname))
{
File.Delete(strFname);
}
object missing = Type.Missing;
object fileName = strFname;
WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

}

//定义一个Word.Application对象
public void activeWordApp()
{
WordApp = new ApplicationClass();
}

public void Quit()
{
object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit(ref missing, ref missing, ref missing);
isOpened = false;
}

/// <summary>
/// 按照先前设计好的模板新建Word文件
/// </summary>
/// <param name="strTempPath">模板物理路径</param>
public void OpenTempelete(string strTempPath)
{
object Missing = Type.Missing;
activeWordApp();
WordApp.Visible = false;
object oTemplate = (object)strTempPath;
try
{
WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
isOpened = true;
WordDoc.Activate();
}
catch (Exception e)
{
Quit();
throw new Exception(e.Message);
}
}

/// <summary>
/// 填充Word模板书签
/// </summary>
/// <param name="LabelId">Word模板书签Id</param>
/// <param name="Content">填充内容</param>
public void FillLable(string LabelId, string Content)
{
object bkmC = LabelId;
if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
{
//判断是否是显示照片的书签
if (LabelId != "img1" && LabelId != "img2" && LabelId != "img3" && LabelId != "img4")
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
WordApp.Selection.TypeText(Content);
}
else//照片书签
{
try
{
object missing = System.Reflection.Missing.Value;
InlineShape li = WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Range.InlineShapes
.AddPicture(Content, ref missing, ref missing, ref missing);
li.Width = 300;//设置照片的宽
li.Height = 150;//设置照片的高
}
catch { }
}
}
}

/// <summary>
/// 将文件打包并下载
/// </summary>
/// <param name="pSource">源路径</param>
/// <param name="pDestination">下载保存路径</param>
public void CreateRar(string pSource, string pDestination)
{
string _Source = pSource.ToString();
string _Destination = pDestination.ToString();
System.Diagnostics.Process _Process = new Diagnostics.Process();
_Process.StartInfo.FileName = "Winrar.exe";
_Process.StartInfo.CreateNoWindow = true;//true表示不显示黑框,false表示显示dos界面
_Process.StartInfo.Arguments = " a -r -epl " + _Destination + " " + _Source;
_Process.StartInfo.WorkingDirectory = _Source;//获取或设置要启动的进程的初始目录。
_Process.Start();
_Process.WaitForExit();
if (_Process.HasExited)
{
int iExitCode = _Process.ExitCode;
if (iExitCode == 0)
{
//压缩成功
}
else
{
//压缩失败
}
}
_Process.Close();
_Process.Dispose();
}
}
}

posted @ 2015-11-11 15:40  bin89  阅读(802)  评论(0)    收藏  举报