I try to export one gridview in asp.net to word document, but I had no luck. Please look at my code:
- Code: Select all
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using Spire.Doc.Documents;
using Spire.Doc;
using Spire.Doc.Fields;
using Spire.Doc.Formatting;
using System.Web.UI.WebControls;
- Code: Select all
Document doc = new Document();
//------
Section s = doc.AddSection();
Paragraph Paras = s.AddParagraph();
Paras.AppendText("Este é um bom exemplo");
Paras.AppendText("linha seguinte");
Paragraph lixo = s.AddParagraph();
lixo.AppendText("");
Spire.Doc.Table table = s.AddTable(true);
int k;
//Create Header and Data
String[] Header = { "Item", "Description", "Qty", "Unit Price", "Price" };
String[][] data = {
for (k = 0; k < 2; k++)
{
new String[] { GridView1.Rows[k].Cells[0].Text, GridView1.Rows[k].Cells[1].Text, GridView1.Rows[k].Cells[2].Text, GridView1.Rows[k].Cells[3].Text, GridView1.Rows[k].Cells[4].Text },
}
};
//Add Cells
table.ResetCells(data.Length + 1, Header.Length);
table.TableFormat.Borders.Horizontal.LineWidth = 0;
table.TableFormat.Borders.Vertical.LineWidth = 0;
//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 = 14;
//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 = 12;
//TR2.CharacterFormat.TextColor = Color.Brown;
}
}
//Save and Launch
doc.SaveToFile("c:\\editorialportugal\\WordTable.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("c:\\editorialportugal\\WordTable.docx");
It seems I can't use the following code:
- Code: Select all
String[][] data = {
for (k = 0; k < 2; k++)
{
new String[] { GridView1.Rows[k].Cells[0].Text, GridView1.Rows[k].Cells[1].Text, GridView1.Rows[k].Cells[2].Text, GridView1.Rows[k].Cells[3].Text, GridView1.Rows[k].Cells[4].Text },
}
};
Any help, please?
thank you