Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Feb 11, 2020 4:08 pm

Hello,

I tried the following code to copy the gridview to a Word Document and keep getting the error that the index is out of range. NOt sure what I am doing wrong, can someone please assist me.

Section s = document.AddSection();
Spire.Doc.Table table = s.AddTable(true);

//Create Header and Data
String[] Header = { "Task", "# Completed", "Event", "# Participants", "Counselor" };
String[][] data = new String[2][];

for (int k = 0; k < 2; k++)
{
data[k] = new string[GridView1.Columns.Count];
for (int j = 0; j < GridView1.Columns.Count; j++)
{

data[k][j] = GridView1.Rows[k].Cells[j].Text.ToString();
}
}
//Add Cells
table.ResetCells(data.Length + 1, Header.Length);

//Header Row
Spire.Doc.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 < Header.Length; i++)
{
//Cell Alignment
Paragraph p = FRow.Cells[i].AddParagraph();
FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Data Format
TextRange TR = p.AppendText(Header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.TextColor = Color.Teal;
TR.CharacterFormat.Bold = true;
}

//Data Row
for (int r = 0; r < data.Length; r++)
{
Spire.Doc.TableRow DataRow = table.Rows[r + 1];

//Row Height
DataRow.Height = 20;

//C Represents Column.
for (int c = 0; c < data[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(data[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 11;
TR2.CharacterFormat.TextColor = Color.Brown;
}
}

sapper1sg2001
 
Posts: 1
Joined: Tue Dec 24, 2019 4:08 pm

Wed Feb 12, 2020 6:22 am

Hello,

Thanks for your inquiry.
I simulated your case with the latest Spire.Doc Pack(hot fix) Version:8.1.10 but didn't encounter any issue. Here I uploaded my testing project for your reference. You could directly run it on your side and let us know your testing result.
If there is still any issue, please provide a runnable project that could reproduce your issue for further investigation. You could send it to us via email ([email protected]).

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Feb 17, 2020 2:56 am

Hello,

Greetings from E-iceblue!
Have you tested my project? How is your issue now?
Any feedback will be greatly appreciated!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc