您的位置:首页 > 编程语言 > C#

C# 导出word文档及批量导出word文档(4)

2015-06-27 17:19 459 查看
接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用word,提交打印机,把word文档的内容都打印出来。
批量导出word文档需要用到ICSharpCode.SharpZipLib.dll 插件,思路是,先单独导出所勾选的数据的单个文档,保存到生成的临时目录下,再把同一个的人相关文档合并一个word文档,并删除已经合并的那个文档(不是合并后生成的文档),以姓名和编号命名生成后的文档,最后将临时目录下的所有文件打包压缩下载,并删除临时目录和临时目录下的所有文件(都是生成在服务器上,所以若文件太大,可能会慢)。批量导出是用form来提交所传的参数,并下载。(因为懒,不解释代码了)
首先新建一个空文档,插入书签,书签名为“p”(随便定义的),命名文件名为“test.doc”,保存在项目目录下。
相关js代码为:

#region 批量下载文件前的验证
/// <summary>
/// 批量下载文件前的验证
/// </summary>
/// <param name="tempFile">模板</param>
/// <param name="ntid">学生主键</param>
/// <returns></returns>
public JsonResult BatchExportWord(string tempFile, string ntid = null)
{
bool isOK = false;
string msg = "";
string strpath = "Content/templates";
List<string> ls = new List<string>();
if (!string.IsNullOrEmpty(tempFile))
{
if (Path.GetExtension(tempFile) != ".doc")
tempFile = tempFile + ".doc";
isOK = WordFilePath.ExistFile(strpath, tempFile);
if (!isOK)
msg = "导出的模板不存在!";
else
{
if (!string.IsNullOrEmpty(ntid))
{
string[] strNTID = ntid.Split(',');
for (int i = 0; i < strNTID.Length; i++)
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add(GetPropertyName<Model.MajorCycle>(m => m.NTID), strNTID[i]);
IList<Model.MajorCycle> lsMCycle = new BLL.MajorCycle().GetModelList<Model.MajorCycle>(nvc);
if (lsMCycle.Count > 0)
ls.Add(string.Join(",", lsMCycle.Select(m => m.ID)));
}
}
}
}
else
{
msg = "导出的模板为空!";
}
return Json(new { isOK = isOK, msg = msg, ls = ls });
}
#endregion

#region 下载压缩包
/// <summary>
/// 下载压缩包
/// </summary>
/// <param name="tempFile">模板</param>
/// <param name="ntid">学生主键</param>
/// <param name="code">学生编号</param>
/// <param name="name">学生名称</param>
/// <param name="isAll">是否全部导出</param>
/// <param name="idName">请求按钮id名称</param>
/// <returns></returns>
public FileContentResult DownLoadWord(string tempFile, string ntid, string code, string name, string isAll, string idName)
{
CompressFileZip fileZip = new CompressFileZip();
string saveName = "", dirPath = "", GzipFileName = "", mid = "", stype = "", sequence = "";
Document doc = new Document();
var docStream = new MemoryStream();
if (!string.IsNullOrEmpty(ntid))
{
string[] strNTID = ntid.Split(',');
string[] strCode = code.Split(',');
string[] strName = name.Split(',');
for (int i = 0; i < strNTID.Length; i++)
{
if (i == 0) //第一次时创建临时文件夹
{
if (!string.IsNullOrEmpty(isAll) && isAll == "true")
GzipFileName = fileZip.FolderName(tempFile.Substring(0, tempFile.IndexOf("生") + 1), "");
else
GzipFileName = fileZip.FolderName(strName[i], strCode[i]);
dirPath = fileZip.CreateTempFolder(GzipFileName);
}
NameValueCollection nvc = new NameValueCollection();
nvc.Add(GetPropertyName<Model.MajorCycle>(m => m.NTID), strNTID[i]);
IList<Model.MajorCycle> lsMCycle = new BLL.MajorCycle().GetModelList<Model.MajorCycle>(nvc);
if (lsMCycle.Count > 0) //如果不以这方式,则没有学习记录的数据导出来会是一个空的压缩包,什么文件也没有
{
mid = string.Join(",", lsMCycle.Select(m => m.MajorID)); // 轮转科室id
stype = lsMCycle[0].StudentType.ToString().Trim(); // 学生类型
sequence = string.Join(",", lsMCycle.Select(m => m.Sequence)); // 轮转次数
}
string[] midArr = mid.Split(',');
string[] sequenceArr = sequence.Split(',');
mid = ""; sequence = "";
for (int j = 0; j < midArr.Length; j++)
{
saveName = strName[i] + "_" + strCode[i] + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
WordHelper wordhelper = new WordHelper(tempFile);
getWordInfo(wordhelper, tempFile, strNTID[i], stype, midArr[j], sequenceArr[j]);
fileZip.SaveFile(wordhelper.ExportDoc().ToArray(), saveName, dirPath);
}
if (!string.IsNullOrEmpty(isAll) && isAll == "true" && idName == "Export")
{
doc = new Document(WordFilePath.GetFilePath("Content/templates", "test.doc"));
InsertFile.InsertDoc(doc, "p", fileZip.GetFileList(GzipFileName, strName[i]), saveName);
}

}
if (idName == "Export")
fileZip.dlZipDir(Response, WordFilePath.GetFilePath("UploadFile", GzipFileName), GzipFileName); //压缩下载
}

if (idName == "Print") //打印
{
doc = new Document(WordFilePath.GetFilePath("Content/templates", "test.doc"));
InsertFile.InsertDoc(doc, "p", fileZip.GetFileList(GzipFileName));
doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
Directory.Delete(WordFilePath.GetFilePath("UploadFile", GzipFileName));
}
return base.File(docStream.ToArray(), "application/msword", GzipFileName);
}
#endregion


View Code
导出的效果如下图:

生成的临时文件夹:



同一人的首先生成单个文件



将两个文件合并成一个文件,以姓名和编号命名,同时删除合并前生成的那两个文件:



循环生成 下一个人的单个文档



合并文件,以姓名和编号命名,同时删除合并前的文件:



打包压缩合并后的这两个文件,并下载:





打印的效果如下图: 将所有人的有关文档都生成单个文件



最后合并成一个文件,并输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: