我有这种“有效”的方法,但只给了我一行数据。我使用 EPPlust 循环遍历 Excel 文件并调用 Word 文档模板。该模板是一个 8.5 X 11 的文档,带有 21 个标签。我的目标是用 Excel 文件中的信息填充标签。我想我只是错过了推进下一个记录的命令。我检查了微软的定义,它给我留下了更多的问题......谷歌已经让我走到了这一步,但我仍然需要这个小块信息来完成这个应用程序。
谢谢,
public static void MergeLabels(string labelDoc)
{
FileInfo fi = new FileInfo(labelDoc);
string labelSave = labelDoc.Replace(".xlsx", ".docx");
string labelTemplet = fi.DirectoryName.ToString() + "\\StockLabelsMerge.docx";
using (ExcelPackage excelPackage = new ExcelPackage(fi))
{
var application = new Microsoft.Office.Interop.Word.Application();
var document = new Microsoft.Office.Interop.Word.Document();
document = application.Documents.Add(Template: labelTemplet);
foreach (var worksheetLoop in excelPackage.Workbook.Worksheets)
{
// this.comboBox1.Items.Add(worksheet.Name);
ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[worksheetLoop.Index];
int colCount = workSheet.Dimension.End.Column;
int rowCount = workSheet.Dimension.End.Row;
string specieSize = string.Empty;
string cabinet = string.Empty;
string jobNumber = string.Empty;
for (int row = 1; row <= rowCount; row++)
{
if (workSheet.Cells[row, 1].Value != null)
{
jobNumber = workSheet.Cells[row, 1].Value.ToString();
if (workSheet.Cells[row, 2].Value != null)
{
cabinet = workSheet.Cells[row, 2].Value.ToString();
if (workSheet.Cells[row, 3].Value != null)
{
specieSize = workSheet.Cells[row, 3].Value.ToString();
foreach (Microsoft.Office.Interop.Word.Field label in document.Fields)
{
if (label.Code.Text.Contains("F1"))
{
label.Select();
application.Selection.TypeText(jobNumber);
}
else if (label.Code.Text.Contains("NoName"))
{
label.Select();
application.Selection.TypeText(cabinet);
}
else if (label.Code.Text.Contains("NoName2"))
{
label.Select();
application.Selection.TypeText(specieSize);
}
//next record?
// label.Next.Result;
}
}
}
}
}
}
document.SaveAs2(FileName: labelSave);
}
}
如果有人知道缺少什么,甚至有一个好的建议,我将不胜感激。