Excel 模板:合并数据并保存工作簿
我是第一次尝试 Excel Interop,但在起步顺利后却遇到了困难。
我有一个 Excel 模板,其中包含一张评估表,另一张包含如何进行评估的指导说明。
我还有一个 XML 文件,其中包含正在评估的项目的详细信息。
我需要将项目名称、申请号和公司名称合并到第一张表格中,然后以文件名[申请号] - [项目名称].xlsx 保存该表格。
第一部分工作正常。我已经加载了 XML,代码正在将数据放入应有的表单中。
我的问题是保存部分。我找到了 .SaveAs 方法,它创建了几个文件......但它们无法打开。然后我收到 HRESULT 错误 0x800A03EC - 搜索网络并没有对此进行任何解释。有些东西告诉我 this.SaveAs() 指的是工作表而不是工作簿,但我只是猜测。
我希望我做了一些愚蠢的事情,而且这是一个很容易解决的问题。
这里是我的代码供参考,但就像我说的,我不确定它有多大用处。
private void MergeData()
{
doc.Load(@"C:\XML Data\source.xml");
XmlNodeList forms = doc.SelectNodes("//form1");
for (int i = 0; i < forms.Count; i++)
{
XmlNodeList nodes = forms[i].ChildNodes;
string refNo = nodes[0].InnerText.ToString();
string companyName = nodes[3].InnerText.ToString();
string title = nodes[1].InnerText.ToString();
this.Cells[9, 4] = title;
this.Cells[11, 4] = refNo;
this.Cells[14, 4] = companyName;
this.SaveAs(@"C:\Assessment Forms\" + refNo + " - " + title + ".xlsx");
}
}
有谁知道如何保存这些文件?
感谢您阅读
编辑 -
我找到了这篇文章
C# 和 Excel 互操作问题,保存 Excel 文件不顺利
并更改了代码以包含其建议
Excel.Application app = this.Application;
Excel.Workbook wb = app.Workbooks.Add(missing);
wb.SaveAs(@"C:\Assessment Forms\" + refNo + " - " + title + ".xlsx");
,但它正在做同样的事情。
我已经到了最后期限,所以我想我必须开始手动复制、粘贴和保存:(
I am on my first foray into Excel Interop and after a flying start have hit a wall.
I have an Excel template that contains one sheet which is an assessment form and another which contains guidance notes for how to carry out the assessment.
I also have an XML file with the details of the projects being assessed.
I need to merge the project title, application number and company name into the first sheet and then save the sheet with the filename [Application No] - [Project Title].xlsx.
The first part is working fine. I have Loaded the XML and the code is putting the data into the form where it should be.
My problem is the saving part. I found the .SaveAs method and it creates a couple of files... but they won't open. I then get a HRESULT error 0x800A03EC - Searching the web has explained nothing about this. Something is telling me that this.SaveAs() is referring to the worksheet rather than the work book but I am just guessing there.
I am hoping I have done something stupid and it is an easy fix.
For reference here is my code but like I say I am not sure how useful it is.
private void MergeData()
{
doc.Load(@"C:\XML Data\source.xml");
XmlNodeList forms = doc.SelectNodes("//form1");
for (int i = 0; i < forms.Count; i++)
{
XmlNodeList nodes = forms[i].ChildNodes;
string refNo = nodes[0].InnerText.ToString();
string companyName = nodes[3].InnerText.ToString();
string title = nodes[1].InnerText.ToString();
this.Cells[9, 4] = title;
this.Cells[11, 4] = refNo;
this.Cells[14, 4] = companyName;
this.SaveAs(@"C:\Assessment Forms\" + refNo + " - " + title + ".xlsx");
}
}
Does anyone know how to save these files?
Thanks for reading
EDIT--
I have found this article
C# and Excel Interop issue, Saving the excel file not smooth
and changed the code to include its suggestion
Excel.Application app = this.Application;
Excel.Workbook wb = app.Workbooks.Add(missing);
wb.SaveAs(@"C:\Assessment Forms\" + refNo + " - " + title + ".xlsx");
But it is doing the same.
I am on a deadline so think I am going to have to start copying, pasting and saving manually :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定
this
在这里是什么对象,但如果您正确地假设this
是一个工作表,请尝试this.Parent.SaveAs
或者如果 < code>this 原来是一个范围,尝试
this.Worksheet.Parent.SaveAs
Not sure what object
this
is here, but if you are correct in assumingthis
is a worksheet, trythis.Parent.SaveAs
Alternatively if
this
turns out to be a range, trythis.Worksheet.Parent.SaveAs