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

在初始化WordHelper时,要获取模板的相对路径。获取文档的相对路径多个地方要用到,比如批量导出时要先保存文件到指定路径下,再压缩打包下载,所以专门写了个关于获取文档的相对路径的类。

  #region 获取文档的相对路径
public class WordFilePath
{
#region 返回文件带路径值
/// <summary>
/// 返回文件带路径值
/// </summary>
/// <param name="FilePth">文件所在文件夹名称</param>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static string FileWithFilePath(string FilePth, string FileName = null)
{
string strFile = "";
if (!string.IsNullOrEmpty(FilePth))
strFile += "~/" + FilePth + "/";
if (!string.IsNullOrEmpty(FileName))
strFile += FileName; return strFile;
}
#endregion #region 判断文件或文件夹是否存在
/// <summary>
/// 判断文件或文件夹是否存在
/// </summary>
/// <param name="FilePth">文件所在文件夹名称</param>
/// <param name="FileName">文件名</param>
/// <returns></returns>
public static bool ExistFile(string FilePth, string FileName = null)
{
return System.IO.File.Exists(GetFilePath(FilePth, FileName));
}
#endregion #region 获取文件及文件夹的相对路径
/// <summary>
/// 获取文件及文件夹的相对路径
/// </summary>
/// <param name="FilePth"></param>
/// <param name="FileName"></param>
/// <returns></returns>
public static string GetFilePath(string FilePth, string FileName = null)
{
string strPath = "";
strPath = FileWithFilePath(FilePth, FileName);
strPath = HttpContext.Current.Request.MapPath(strPath);
return strPath;
}
#endregion
}
#endregion

以上方法可以实现了单个word文档带图片的导出功能了,多个文档的生成也可以实现,只是还没有打包下载的代码。
     最后调用的方法如下,要先判断模板是否存在:

  string strpath = "Content/templates"; //模板所在的文件文件夹
string templateFile = "实习生学习记录表导出模板.doc";
if (WordFilePath.ExistFile(strpath, templateFile))
{
string saveName = WordHelper.SaveDocName(form["NTName"], form["CardNo"]); //保存的名称
saveName = HttpUtility.UrlEncode(saveName, Encoding.GetEncoding("utf-8"));
WordHelper wordhelper = new WordHelper(templateFile);
getWordInfo(wordhelper, templateFile, NTID, stype.ToString(), majorid.ToString(), sequence.ToString());
return base.File(wordhelper.ExportDoc().ToArray(), "application/msword", saveName);
}
else
{
return ShowRedirectMessage("导出的模板不存在!", strUrl);
}

getWordInfo方法是为将所需的信息拼凑成一个word文档,减少代码的重复性而提取出来的,如:

 #region 将所有信息拼凑成一个word文档
public void getWordInfo(WordHelper wordhelper, string tempFile, string ntid, string stype, string mid, string sequence)
{
if (tempFile.Contains("实习生学习记录表导出模板"))
{
Dictionary<string, string> dicWhere = new Dictionary<string, string>();
dicWhere.Add("NTID", ntid);
wordhelper.GetBasicInfo(typeof(BLL.NewTraineeInfo), dicWhere);
PrejobTrainWord(wordhelper, ntid);
TrainListWord(wordhelper, ntid, stype, mid);
AwardListWord(wordhelper, ntid, stype, mid);
ArrangeListWord(wordhelper, ntid, stype);
ScoreListWord(wordhelper, ntid, stype, mid, sequence);
EvalWord(wordhelper, ntid, stype, mid);
}
} #region 其他信息
//岗前培训
public void PrejobTrainWord(WordHelper wordhelper, string ntid)
{
Dictionary<string, string> dicWhere = new Dictionary<string, string>();
dicWhere.Add("NTID", ntid);
wordhelper.GetBasicInfo(typeof(BLL.PrejobTraining), dicWhere);
} //轮科安排表
public void ArrangeListWord(WordHelper wordhelper, string ntid, string stype)
{
Dictionary<string, string> dicWhere = new Dictionary<string, string>();
dicWhere = new Dictionary<string, string>();
dicWhere.Add("NTID", ntid);
dicWhere.Add("StudentType", stype.ToString());
wordhelper.GetTableList(typeof(BLL.MajorCycle), dicWhere, "ArrangeList");
}
    ArrangeList是之前在word模板里所设定的如下图,用for循环出列表,

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

getWordInfo里其它的方法类似这两个。

最后导出的word文档大致如下图所示:

C# 导出word文档及批量导出word文档(3)
C# 导出word文档及批量导出word文档(3)
上一篇:【Java】导出word文档之freemarker导出


下一篇:freemarker导出word文档——WordXML格式解析