【发布时间】:2014-04-12 15:04:21
【问题描述】:
我根据一些教程编写了一些代码,以使用我的数据库表中的数据更新现有的 Word 文档。
我不断让程序崩溃并建议我可能有一个无限循环,但是我使用 for each 语句,当我调试时它告诉我我只有一条记录。无法理解问题出在哪里。
如果有人可以提供帮助,将不胜感激。代码如下
控制器
using System.Web.Mvc;
using Mojito.Models;
namespace Mojito.Controllers
{
public class KronosDesignDocumentController : Controller
{
private MojitoContext _db = new MojitoContext();
//
// GET: /KronosDesignDocument/
public ActionResult Index()
{
ViewBag.CustomerId = new SelectList(_db.Customers, "CustomerId", "CustomerName");
return View();
}
[HttpPost]
public ActionResult Load(int customerId)
{
var kronosDesignTemplate = new KronosDesignDocument.CreateDesignDoc(@"C:\Users\Craig Cocker\Documents\XML Files\Test_Design_Template.docx");
kronosDesignTemplate.CustomerDesignDocument();
ViewBag.CustomerId = new SelectList(_db.Customers, "CustomerId", "CustomerName");
ViewBag.Message = "Configuration has been loaded successfully";
return View("Index");
}
}
}
型号
using System.Collections.Generic;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Linq;
namespace Mojito.Models
{
public class KronosDesignDocument
{
public static MojitoContext _db = new MojitoContext();
public class CreateDesignDoc
{
private readonly string _xmlPath;
//private readonly int _customerId;
public CreateDesignDoc(string pathToXmlFiles)
{
_xmlPath = pathToXmlFiles;
}
public IEnumerable<Customer> CustomerDesignDocument()
{
WordprocessingDocument myDoc = WordprocessingDocument.Open(_xmlPath, true);
var docPart = myDoc.MainDocumentPart;
var doc = docPart.Document;
var table = new Table();
var tb = new TopBorder();
tb.Val = BorderValues.DashDotStroked;
tb.Size = 12;
var borders = new TableBorders();
borders.TopBorder = tb;
borders.LeftBorder = new LeftBorder() {Val = BorderValues.Single, Size = 12};
borders.RightBorder = new RightBorder() {Val = BorderValues.Single};
borders.BottomBorder = new BottomBorder() {Val = BorderValues.Single};
borders.InsideHorizontalBorder = new InsideHorizontalBorder() {Val = BorderValues.Single};
borders.InsideVerticalBorder = new InsideVerticalBorder() {Val = BorderValues.Single};
var props = new TableProperties();
props.Append(borders);
table.Append(props);
var customers = _db.Customers.ToList();
var customerCollection = new List<Customer>();
foreach (var c in customers)
{
var tr = new TableRow();
var customerName = c.CustomerName;
var tc = new TableCell();
var runProp = new RunProperties();
runProp.Append(new Bold());
runProp.Append(new Color() {Val = "FF0000"});
var run = new Run();
run.Append(runProp);
var t = new Text(customerName);
run.Append(t);
var justification = new Justification();
justification.Val = JustificationValues.Center;
var paraProps = new ParagraphProperties(justification);
var p = new Paragraph();
p.Append(paraProps);
p.Append(run);
tc.Append(p);
var tcp = new TableCellProperties();
var tcw = new TableCellWidth();
tcw.Type = TableWidthUnitValues.Dxa;
tcw.Width = "2000";
tcp.Append(tcw);
tcp.Append(tcp);
tr.Append(tc);
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
return customerCollection;
}
}
}
}
查看
@model Mojito.Models.KronosDesignDocument
@{
ViewBag.Title = "Kronos Design Document";
}
<h1>Load Kronos Data</h1>
<h2>@ViewBag.Message</h2>
@using (Html.BeginForm("Load", "KronosDesignDocument", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
<div>@Html.Partial("~/Views/Shared/_Customer.cshtml")</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" value="Create Design Document" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
【问题讨论】:
-
添加它崩溃的哪一行以及你得到的确切异常(内部异常,如果可用)是什么?
-
System.StackOverflowException 未处理 消息:DocumentFormat.OpenXml.dll 中发生“System.StackOverflowException”类型的未处理异常 它似乎返回到控制器,返回视图,然后只是崩溃。故障排除提示说确保您没有无限循环或无限递归。它没有让我选择查看内部异常
-
这条线
tcp.Append(tcp);正确吗?它自己附加了一个引用。这可能会导致堆栈溢出。 -
非常感谢 - 我已经找了好几个小时了。有时,只见树木不见森林。
标签: c# asp.net-mvc linq openxml-sdk