テーブルを含む単語テンプレートファイルがあり、11行と3列があります。最初のページの表には33個のセルがあります。これらのセルにいくつかのデータを入力しますが、レコードの数が66よりも多い場合は、33個のレコードを含む2つのファイルを作成しています。私はすべての66のレコードを含む単一のファイルを作成したいと2つの別々のファイルの代わりに1つのファイルに2つのページがあります。私はどうすればいいのですか?Word Interop C#:既存のページを使用して新しいページを挿入
以下は、1つのdocファイルを作成するために使用しているコードです。
private static string FillTemplate(List<ClientListItem> clients, string fileName)
{
//Filled document file name
var savedFileName = string.Empty;
//Search template file in current directory
var templateFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "templateFile.doc";
#region Generate file for labels
object missing = System.Type.Missing;
// Create Word application
WORD._Application app = new WORD.Application();
try
{
Print("Creating label document.");
// Create new file
WORD._Document doc = app.Documents.Open(templateFilePath);
//Print("Creating app selection object");
WORD.Selection selection = app.Selection;
//If the template file contains tables
if (selection.Tables.Count > 0)
{
//Use first table
//Table's are accessed with starting index as 1 not 0
var tableToUse = selection.Tables[1];
//Counter for number of parent caregivers inserted
var counter = 0;
//Number of parent caregivers
var numberOfParentCaregivers = clients.Count;
//Loop on each row
//Rows are accessed with starting index as 1 not 0
for (int rowIndex = 1; rowIndex <= tableToUse.Rows.Count; rowIndex++)
{
//Loop on each column
//Columns are accessed with starting index as 1 not 0
for (int columnIndex = 1; columnIndex <= tableToUse.Columns.Count; columnIndex++)
{
//If counter has reached to its limit
if (counter + 1 > numberOfParentCaregivers)
{
//Stop
break;
}
//If current column index is even
if (columnIndex % 2 == 0)
{
//Do not process it
//Why? Check template file for yourself
continue;
}
//Get parent caregiver to set
var parentCaregiver = clients[counter];
Print("Generating label to add to document.");
//Get label to add to document
var labelToAdd = string.Format("{0} {1},{6}{2},{6} {3}, {4} {5}", parentCaregiver.Parent1LastName, parentCaregiver.Parent1FirstName,
parentCaregiver.Parent1StreetAddress, parentCaregiver.Parent1City, parentCaregiver.Parent1State, parentCaregiver.Parent1Zip, Environment.NewLine);
//Print(string.Format("Adding label {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
//Get cell to set value
var tableCell = tableToUse.Cell(rowIndex, columnIndex);
//Set text in cell
tableCell.Range.Text = labelToAdd;
//Middle align text
tableCell.Range.ParagraphFormat.Alignment = WORD.WdParagraphAlignment.wdAlignParagraphCenter;
Print(string.Format("Label added {0} at {1}, {2} position.", labelToAdd, rowIndex, columnIndex));
counter++;
}
//If counter has reched to its limit
//i.e. no parent caregivers to process - all processed
if (counter + 1 > numberOfParentCaregivers)
{
//stop
break;
}
}
}
// Set file name to save
savedFileName = string.Format(@"{0}{1}{2}", Path.GetTempPath(), fileName, Path.GetExtension(templateFilePath));
object fname = savedFileName;
Print(string.Format("Saving new document at {0}", savedFileName));
// SaveAs new file
doc.SaveAs(ref fname, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
Print(string.Format("{0} saved successfully.", savedFileName));
app.Documents.Close(ref missing, ref missing, ref missing);
}
catch (Exception exc)
{
Print("Exception while generating label document");
Print(exc.ToString());
//Set file Name to empty string
savedFileName = string.Empty;
}
finally
{
// Close Word application
app.Quit(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(app);
app = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Return saved file name
return savedFileName;
}
テンプレートファイルに追加するレコード数に基づいて任意の数のページを作成できるように変更してください。
ありがとう、 Nikhil。
タイトルを変更して問題をより詳しく説明してください。この質問を見つけたいと思ったら、あなたは何をGoogleに入力しますか? –
ありがとうございました。 –
複数の文書を作成することもできます(2つは束です)。**この複数の文書を1つの文書に結合する**または**結合する**ことができます。あなたはこのキーワードでgoogleを試しましたか?私は多くの結果を得ました –