//导出word文档(注:需制定导出模板 并在内容处填入书签)
//导出表格
public ActionResult ExportTZNoticeData(string id, string create_date)
{
try
{
string sPath = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "UploadFile";
sPath = sPath.Replace("\\bin\\", "\\");
string Path = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "Templet";
Path = Path.Replace("\\bin\\", "\\");
string templatePath = Path + "\\noticeTZ_data.doc"; //自己做好的word
WordDoc = new Document(templatePath);
string fileName = "";
var model = _rectifynoticedataRepository.Get(id);
if (model != null)
{
fileName = string.Format("{0}", model.enterprise_name + "整改通知书");
#region 替换书签
model.rectify_content = model.rectify_content.Substring(0, model.rectify_content.Length - 1);
var rectify_content = model.rectify_content.Split('|');
//文本域
DocumentBuilder builder = new DocumentBuilder(WordDoc);
builder.MoveToBookmark("rectify_content");
if (WordDoc.Range.Bookmarks["rectify_content"] != null)
{
foreach (var item in rectify_content)
{
builder.Write(HttpUtility.HtmlDecode(" " + item + ";"));
builder.Writeln();
}
}
FillLable("notice_number", model.notice_number);
FillLable("enterprise_name", model.enterprise_name);
FillLable("unit_name", model.unit_name);
FillLable("main_content", model.main_content);
if (!string.IsNullOrEmpty(model.remark))
FillLable("remark", model.remark);
FillLable("create_date", create_date);
#endregion
}
WordDoc.Save(sPath + "\\" + fileName + ".docx", Aspose.Words.SaveFormat.Docx);
return File(sPath + "\\" + fileName + ".docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileName + DateTime.Now.ToString("yyyy-MM-dd") + ".docx");
}
catch (Exception ex)
{
return Json(new { success = false, message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
public void FillLable(string LabelId, string Content)
{
object bkmC = LabelId;
if (WordDoc.Range.Bookmarks[LabelId] != null)
{
WordDoc.Range.Bookmarks[LabelId].Text = Content;
}
}
//表格中插入图片
public ActionResult ExportInspectRecord(string id)
{
try
{
string sPath = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "UploadFile";
sPath = sPath.Replace("\\bin\\", "\\");
string Path = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "Templet";
Path = Path.Replace("\\bin\\", "\\");
string templatePath = Path + "\\inspect_record.doc"; //自己做好的word
WordDoc = new Document(templatePath);
string fileName = "";
var model = _inspectrecordRepository.Get(id);
if (model != null)
{
fileName = string.Format("{0}", model.enterprise_name + "记录");
#region 替换书签
var inspect_item = model.inspect_item.Split('|').ToList();
DocumentBuilder builder = new DocumentBuilder(WordDoc);
builder.MoveToBookmark("inspect_content");
if (WordDoc.Range.Bookmarks["inspect_content"] != null)
{
foreach (var item in inspect_item)
{
builder.Write(HttpUtility.HtmlDecode((inspect_item.IndexOf(item) + 1) + "." + item + "。"));
builder.Writeln();
}
}
DateTime date = Convert.ToDateTime(model.inspect_date);
FillLable("enterprise", model.enterprise_name);
FillLable("year", date.Year.ToString());
FillLable("month", date.Month.ToString());
FillLable("day", date.Day.ToString());
var enterprise_sign = AliyunOSS.PutLocalFileBySignedUrl(bucketName, model.charge_sign);//查询存储在oss中的文件
(AliyunOSS帮助类可参考https://www.cnblogs.com/yyjspace/p/11613525.html)
//文件不存在时InsertImage会报错 所以加验证
if (AliyunOSS.DoesObjectExist(bucketName, model.charge_sign))
{
builder.MoveToBookmark("enterprise_sign");
builder.InsertImage(enterprise_sign, RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 100, 125, WrapType.Square);
}
#endregion
}
WordDoc.Save(sPath + "\\" + fileName + ".docx", Aspose.Words.SaveFormat.Docx);
return File(sPath + "\\" + fileName + ".docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileName + DateTime.Now.ToString("yyyy-MM-dd") + ".docx");
}
catch (Exception ex)
{
return Json(new { success = false, message = ex }, JsonRequestBehavior.AllowGet);
}
}