表格不规则,有合并单元格的情况
.net6 Spire.office 最新版本
Document doc = new Document();
doc.LoadFromFile(@"test.docx");
//Get the first section
Spire.Doc.Section se1 = doc.Sections[0];
Double[] widths = new double[3];//列宽
Double[] heights = new double[3];//行高
String[,] dataStr = new String[3, 3];//单元格文本
//Get the first table
Table original_Table = (Table)se1.Tables[0];
int count = original_Table.Rows.Count;
for (int i = 0; i < count; i++)
{
heights[i] = original_Table.Rows[i].Height;
int count1 = original_Table.Rows[i].Cells.Count;
for (int j = 0; j < count1; j++)
{
string text = original_Table.Rows[i].Cells[j].Paragraphs[0].Text;
dataStr[i, j] = text;
widths[j] = (Double)original_Table.Rows[i].Cells[j].Width;
}
}
//Create a PPT document
Presentation presentation = new Presentation();
//Load the document from disk
presentation.LoadFromFile(@"CreateTable.pptx");
//Add new table to PPT
ITable table = presentation.Slides[0].Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 90, widths, heights);
//Add data to table
for (int i = 0; i < count; i++)
{
int count1 = original_Table.Rows[i].Cells.Count;
for (int j = 0; j < count1; j++)
{
//Fill the table with data
table[j, i].TextFrame.Text = dataStr[i, j];
}
}
table.MergeCells(table[1, 2], table[2, 2], false);//合并单元格
//Save the document
presentation.SaveToFile(@"Output.pptx", Spire.Presentation.FileFormat.Pptx2010);