Šioje trumpoje pamokoje aprašoma, kaip sujungti langelius Word naudojant C#. Jame yra IDE nustatymo detalės, veiksmų sąrašas, iš anksto nustatyta langelių sujungimo funkcija ir pavyzdinis kodas, rodantis kaip sujungti lenteles programoje Word naudojant C# naudojant šią iš anksto nustatytą funkciją. Šiai užduočiai atlikti nereikia jokio kito trečiosios šalies įrankio.
„Word“ lentelės langelių sujungimo veiksmai naudojant C#
- Nustatykite IDE naudoti Aspose.Words for .NET ląstelėms sujungti lentelėje
- Paskelbkite iš anksto nustatytą metodą MergeCells, skirtą naudoti jūsų programoje
- Įkelkite šaltinio Word failą į Document objektą, kuriame yra viena ar daugiau lentelių
- Pasiekite įkelto Word failo lentelę, skirtą langelių sujungimui
- Pasiekite pradinį tikslinio sujungimo diapazono cell
- Pasiekite baigiamąjį sujungimo diapazono langelį
- Iškvieskite MergeCells() metodą, nurodydami pradžios ir pabaigos langelius, ir išsaugokite dokumentą
Šie veiksmai padeda kaip sujungti langelius programoje Word naudojant C#. Pridėkite iš anksto nustatytą metodą savo projekte ir iškvieskite jį nurodydami pradžios ir pabaigos langelį, kurį norite sujungti. Tai pakeis šaltinio failą, todėl įkeltą Word failą galite išsaugoti kitu pavadinimu, naudodami sujungtus langelius.
Kodas, skirtas sujungti ląsteles „Word“ naudojant C#
using System; | |
using System.Drawing; | |
using Aspose.Words; | |
using Aspose.Words.Tables; | |
class Program | |
{ | |
static void MergeCells(Cell startCell, Cell endCell) | |
{ | |
Table parentTable = startCell.ParentRow.ParentTable; | |
// Find the start and end cell | |
Point startingCell = new Point(startCell.ParentRow.IndexOf(startCell), parentTable.IndexOf(startCell.ParentRow)); | |
Point endingCell = new Point(endCell.ParentRow.IndexOf(endCell), parentTable.IndexOf(endCell.ParentRow)); | |
// Create a range of cells | |
Rectangle mergeRange = new Rectangle(Math.Min(startingCell.X, endingCell.X),Math.Min(startingCell.Y, endingCell.Y), | |
Math.Abs(endingCell.X - startingCell.X) + 1, Math.Abs(endingCell.Y - startingCell.Y) + 1); | |
foreach (Row currentRow in parentTable.Rows) | |
{ | |
foreach (Cell currentCell in currentRow.Cells) | |
{ | |
Point currentPos = new Point(currentRow.IndexOf(currentCell), parentTable.IndexOf(currentRow)); | |
// Check if the current cell is inside the range | |
if (mergeRange.Contains(currentPos)) | |
{ | |
currentCell.CellFormat.HorizontalMerge = currentPos.X == mergeRange.X ? CellMerge.First : CellMerge.Previous; | |
currentCell.CellFormat.VerticalMerge = currentPos.Y == mergeRange.Y ? CellMerge.First : CellMerge.Previous; | |
} | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
Document doc = new Document("Table.docx"); | |
Table table = doc.FirstSection.Body.Tables[0]; | |
// Define starting and ending cells | |
Cell cellStartRange = table.Rows[0].Cells[0]; | |
Cell cellEndRange = table.Rows[1].Cells[1]; | |
// Merge all the cells | |
MergeCells(cellStartRange, cellEndRange); | |
doc.Save("Output.docx"); | |
} | |
} |
Šis kodas parodo kaip sujungti lenteles programoje Word naudojant C#. Pridėjome iš anksto nustatytą metodą MergeCells kartu su jo apibrėžimu ir tada, kur reikia, iškviečiame jį savo programoje. Galite pasirinkti skyrių, toje skiltyje esančių lentelių sąrašą ir pasiekti tikslinę lentelę naudodami jos rodyklę, kad pasirinktumėte pradžios ir pabaigos langelius, kuriuos norite sujungti. Galite pakartoti šį procesą, kad sujungtumėte tiek langelių, kiek reikia.
Mes išmokome kaip sujungti ląsteles Microsoft Word naudojant C#. Jei norite sujungti visus Word dokumentus, žr. straipsnį Kaip sujungti Word dokumentus naudojant C#.