Exporting grid to word


#1

hi

i have datagrid in my application i want to export the grid to word whose width is more than the page width. how to do it

please tell me it is urgent


#2

You can export the grid to a table in a word document but it is up to you to break it to several pages.

Please mind that Aspose.Words does not have pagination engine of its own and the only mean of controlling page layout from Aspose.Words is inserting a forced page break in the document by using DocumentBuilder.InsertBreak method. You have to calculate the dimensions of the table parts youself to make them fit on several adjacent pages.


#3

i have the grid datagrid1 please tell me the code snippet to extract it to word

thanks


#4

Well, it will be something like this:

// Create new document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Fill DataTable with dummy data.

DataTable table = new DataTable();

string firstname = "FirstName";

string lastname = "LastName";

table.Columns.Add(firstname);

table.Columns.Add(lastname);

for(int i = 0; i<3; i++)

{

DataRow datarow = table.NewRow();

table.Rows.Add(datarow);

datarow[0] = firstname + i;

datarow[1] = lastname + i;

}

// Bind DataGrid to DataTable.

dataGrid1.DataSource = table;

// Export DataGrid contents to word document.

for (int i = 0; i < dataGrid1.VisibleRowCount; i++)

{

for (int j = 0; j < dataGrid1.VisibleColumnCount; j++)

{

builder.InsertCell();

builder.Write(dataGrid1[i, j].ToString());

}

builder.EndRow();

}

// Save document.

string filename = Application.StartupPath + @"\testGridExport Out.doc";

doc.Save(filename);

// Open document in MS Word.

System.Diagnostics.Process.Start(filename);


#5

hi

i knew thiscode , i want code for how to place a grid whose width is greate than the page width

i'e how to compare the widths of page and datagrid and placing grid relavently in word .

by using layout (i.e landscape mode) can we achive it .if it is true give me code by using that

please it is urgent

thanq

mohanp


#6

To give you a general idea it should be something like this:

// Create new document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Fill DataTable with dummy data.

DataTable table = new DataTable();

string colName = "Column";

int colCount = 20;

int rowCount = 5;

for(int j = 0; j < colCount; j++)

{

table.Columns.Add(colName + j);

}

for(int i = 0; i < rowCount; i++)

{

DataRow datarow = table.NewRow();

table.Rows.Add(datarow);

for(int j = 0; j < colCount; j++)

{

datarow[j] = colName + i + j;

}

}

// Bind DataGrid to DataTable.

dataGrid1.DataSource = table;

// Calculating the expected grid width

double totalGridWidth = 0;

for (int j = 0; j < colCount; j++)

{

totalGridWidth += WordConvert.PixelToPoint(dataGrid1.GetCellBounds(0, j).Width);

}

// Calculating the useable page width

double useablePageWidth = builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin;

// inserting new section with Landscape page orientation if necessary.

if (totalGridWidth > useablePageWidth)

{

// if section already has any text in it than make a new section

if (builder.CurSection.Range.Text != "\f")

builder.InsertBreak(BreakType.SectionBreakContinuous);

builder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;

}

// change scale factor if necessary

double scale = 1.0;

useablePageWidth = builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin;

if (totalGridWidth > useablePageWidth)

{

scale = useablePageWidth / totalGridWidth;

}

// prohibit autofitting cell width to cell contents

builder.RowFormat.AllowAutoFit = false;

builder.StartTable();

// Export DataGrid contents to word document.

for (int i = 0; i < rowCount; i++)

{

for (int j = 0; j < colCount; j++)

{

Cell cell = builder.InsertCell();

if (j==0)

cell.ParentRow.RowFormat.Height = WordConvert.PixelToPoint(dataGrid1.GetCellBounds(i, j).Height);

builder.Write(dataGrid1[i, j].ToString());

cell.CellFormat.Width = WordConvert.PixelToPoint(dataGrid1.GetCellBounds(i, j).Width) * scale;

}

builder.EndRow();

}

builder.EndTable();

// if section already has any text in it than make a new section

if (builder.CurSection.GetText() != "\f")

builder.InsertBreak(BreakType.SectionBreakContinuous);

builder.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;

// Save document.

string filename = Application.StartupPath + @"\testGridExport Out.doc";

doc.Save(filename);

// Open document in MS Word.

System.Diagnostics.Process.Start(filename);

The final code could be more complex though.