Hello,
Thanks for your inquiry!
Kindly note that if you want to add more rows in one table, you just need to change the value of the "
startIndex" or the "
endIndex" when you use the "
addnew" function I provided.
According to the project and the workbook "bill_consumables.xlsx" you sent to me last time. Such as export the data from
index 2 to 8 of the DataGrid shows when you are choosing the sheet "PUTAY Biosystems" to the "
doc" Document, and set the title name is “
title1". You can achieve it by using "
addnew(doc, this.dataGridView1, 2, 8, "title1");" in your Export2docClick function. Here I also attached my testing code.
- Code: Select all
void Export2docClick(object sender, EventArgs e)
{
// Modify to suit your machine:
string fileName = @"DocXExample.docx";
Document doc = new Document();
addnew(doc, this.dataGridView1, 2, 8, "title1");
doc.SaveToFile(fileName, Spire.Doc.FileFormat.Docx);
MessageBox.Show("Done");
Process.Start(fileName);
}
/**
* @doc target Document
* @datagrid the datagrid which provide data
* @startIndex the index number of the start row you want
* @endIndex the index number of the end row you want
* @title the title you want to set
**/
public void addnew(Document doc,DataGridView dataGrid, int startIndex, int endIndex, string title)
{
//add title
Section sec = doc.AddSection();
TextRange tr = sec.AddParagraph().AppendText(title);
tr.ApplyCharacterFormat(new Spire.Doc.Formatting.CharacterFormat(doc) { FontSize = 24f, Position = 15f});
Table table = sec.AddTable(true);
string[] headers = new string[dataGrid.ColumnCount];
for(int i = 0; i < dataGrid.ColumnCount; i++)
{
headers[i] = dataGrid.Columns[i].HeaderText;
}
string[][] datas = new string [endIndex + 1 - startIndex][];
int k = 0;
for (int i = startIndex; i <= endIndex; i++)
{
string[] data = new string[dataGrid.ColumnCount];
for(int j = 0; j < dataGrid.ColumnCount; j++)
{
data[j] = dataGrid.Rows[i].Cells[j].Value.ToString();
}
datas[k] = data;
k++;
}
//Add Cells
table.ResetCells(datas.Length + 1, headers.Length);
//Header Row
TableRow FRow = table.Rows[0];
FRow.IsHeader = true;
//Row Height
FRow.Height = 23;
//Header Format
FRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < headers.Length; i++)
{
//Cell Alignment
Paragraph p = FRow.Cells[i].AddParagraph();
FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
//Data Format
TextRange TR = p.AppendText(headers[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 14;
TR.CharacterFormat.TextColor = Color.Teal;
TR.CharacterFormat.Bold = true;
}
//Data Row
for (int r = 0; r < datas.Length; r++)
{
TableRow DataRow = table.Rows[r + 1];
//Row Height
DataRow.Height = 20;
//C Represents Column.
for (int c = 0; c < datas[r].Length; c++)
{
//Cell Alignment
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Fill Data in Rows
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(datas[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 12;
TR2.CharacterFormat.TextColor = Color.Brown;
}
}
sec.AddParagraph().AppendBreak(Spire.Doc.Documents.BreakType.PageBreak);
}
Hope you can understand. If you encounter have any questions, just feel free to contact us.
Sincerely,
Marcia
E-iceblue support team