根据Word模板文件导出简历

1、需求

有个招聘功能,前端应聘者录入简历表单。如下图:
图片

2、实现

新建Word模板文件,需要替换信息的地方插入域来代替。
图片

替换后的效果如下:
图片

3、编码

添加引用Aspose.Words.dll,添加方法。

public static void CreateJobRequestWord(DataTable JobRequest, bool IsSharpZip) {
            string fileName = "";
            var docStream = new MemoryStream();
            string uploadFilePath = WebUtil.Instance.AppPath() + AppSettingUtil.AppSettings["UploadFilePath"];
            string tempPath = HttpContext.Current.Server.MapPath(uploadFilePath + "WordTemplate/Temp.doc");
            string folderName = DateTime.Now.ToString("yyyyMMddHHmmssms");
            string outputPath = HttpContext.Current.Server.MapPath(uploadFilePath + "jobrequest/" + folderName);
            if (JobRequest.Rows.Count == 0) {
                return;
            }
            if (IsSharpZip) {
                fileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + ".zip";
            }
            else {
                fileName = JobRequest.Rows[0]["Name"].ToStr() + "_" + JobRequest.Rows[0]["IDCardNo"].ToStr() + "_" + JobRequest.Rows[0]["Whir_U_Jobs_JobRequest_PID"].ToStr() + ".doc";
            }
            //增加岗位名称
            JobRequest.Columns.Add("JobName", typeof(string));
            foreach (DataRow dr in JobRequest.Rows) {
                docStream = new MemoryStream();
                string name = dr["Name"].ToStr() + "_" + dr["IDCardNo"].ToStr() + "_" + dr["Whir_U_Jobs_JobRequest_PID"].ToStr() + ".doc";
                var JobName = DbHelper.CurrentDb.ExecuteScalar<string>("select JobName from Whir_U_Jobs where Whir_U_Jobs_PID=@0 and isdel=0", dr["JobID"]).ToStr();
                dr["JobName"] = JobName;

                int JobRequestId = dr["Whir_U_Jobs_JobRequest_PID"].ToInt(0);

                DataTable dtFamily = DbHelper.CurrentDb.Query("select * from Whir_U_Jobs_JobRequest_Family where isdel=0 and JobRequestId=@0", JobRequestId).Tables[0];
                dtFamily.TableName = "Family";

                DataTable dtLearning = DbHelper.CurrentDb.Query("select * from Whir_U_Jobs_JobRequest_learning where isdel=0 and JobRequestId=@0", JobRequestId).Tables[0];
                dtLearning.TableName = "Learning";

                DataTable dtSpecialty = DbHelper.CurrentDb.Query("select *,convert(varchar(10),WinningTime,21) as WinningTimeTemp from Whir_U_Jobs_JobRequest_Specialty where isdel=0 and JobRequestId=@0", JobRequestId).Tables[0];
                dtSpecialty.TableName = "Specialty";

                DataTable dtWorking = DbHelper.CurrentDb.Query("select *,StartEndDate as WorkStartEndDate,Duties as WorkDuties from Whir_U_Jobs_JobRequest_Working where isdel=0 and JobRequestId=@0", JobRequestId).Tables[0];
                dtWorking.TableName = "Working";


                //----------------------------------------------------------------------------------------------------
                var doc = new Document(tempPath);
                //头像
                DocumentBuilder builder = new DocumentBuilder(doc);
                builder.MoveToBookmark("Avatar");
                try {
                    builder.InsertImage(HttpContext.Current.Server.MapPath(uploadFilePath + dr["Avatar"].ToStr()), 100, 130);
                }
                catch (Exception ex) {

                }
                doc.MailMerge.Execute(dr);
                //渲染
                doc.MailMerge.ExecuteWithRegions(dtFamily);
                doc.MailMerge.ExecuteWithRegions(dtLearning);
                doc.MailMerge.ExecuteWithRegions(dtSpecialty);
                doc.MailMerge.ExecuteWithRegions(dtWorking);
                doc.Save(docStream, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(SaveFormat.Doc));
                if (IsSharpZip) {
                    string strFileName = outputPath + "\\" + name;
                    FileInfo info = new FileInfo(strFileName);
                    if (!Directory.Exists(info.DirectoryName)) {
                        Directory.CreateDirectory(info.DirectoryName);
                    }
                    doc.Save(strFileName);
                }
            }
            if (IsSharpZip) {
                SharpZipHelper szh = new SharpZipHelper();
                szh.CompressDirectory(HttpContext.Current.Server.MapPath(uploadFilePath + "jobrequest/" + folderName + "/"), HttpContext.Current.Server.MapPath(uploadFilePath + "jobrequest/" + fileName), 5, 4096);
                FileSystemHelper.DeleteFolder(HttpContext.Current.Server.MapPath(uploadFilePath + "jobrequest/" + folderName + "/"));
                HttpContext.Current.Response.Redirect(uploadFilePath + "jobrequest/" + fileName);
            }
            else {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
                HttpContext.Current.Response.ContentType = "application/ms-word";//设置输出文件类型为excel文件。
                HttpContext.Current.Response.BinaryWrite(docStream.ToArray());
                HttpContext.Current.Response.End();
            }
        }

4、效果

图片

猜你喜欢

转载自www.cnblogs.com/blogcore/p/12468404.html