C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to fill values into Tables of Word File ?


Posted Date: 29 Sep 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: R O B I NMember Level: Gold    
Rating: Points: 10



This sample code shows how to fill values into Tables of MSWord File.

Eg If you have two table exist in word .doc file then you can fill this value

1)Firstly add the Namespace for Word Document in your file.....

2) Use the code as:

rivate void CreateWordTable()
{
// Clear the current contents.
Object start = Type.Missing;
Object end = Type.Missing;
Object unit = Type.Missing;
Object count = Type.Missing;
ThisDocument.Range(ref start, ref end).
Delete(ref unit, ref count);

// Move to start of document.
start = 0;
end = 0;
Word.Range rng =
ThisDocument.Range(ref start, ref end);

// Insert Title and paragraph marks.
rng.InsertBefore("Top Ten Products");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);

// Add the table.
Object defaultTableBehavior = Type.Missing;
Object autoFitBehavior = Type.Missing;
rng.Tables.Add(ThisDocument.Paragraphs[2].Range, 1, 2,
ref defaultTableBehavior, ref autoFitBehavior);

// Set variable to point to new table.
Word.Table tbl = ThisDocument.Tables[1];

// Format the table.
tbl.Range.Font.Size = 12;
tbl.Range.Font.Name = "Verdana";
tbl.Borders.InsideLineStyle =
Word.WdLineStyle.wdLineStyleSingle;
tbl.Borders.OutsideLineStyle =
Word.WdLineStyle.wdLineStyleDouble;

// Set the column widths.
tbl.Columns[1].SetWidth(
ThisApplication.InchesToPoints((float)2.5),
Word.WdRulerStyle.wdAdjustNone);
tbl.Columns[2].SetWidth(
ThisApplication.InchesToPoints((float)1.5),
Word.WdRulerStyle.wdAdjustNone);
tbl.Cell(1, 1).Range.Text = "Product Name";

// Right-align second column.
Word.Range rngCell = tbl.Cell(1, 2).Range;
rngCell.Text = "Unit Price";
rngCell.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphRight;
}





// C#
string strMDB =
"C:\\program files\\microsoft office\\" +
"Office11\\Samples\\Northwind.mdb";
string strSQL =
"SELECT TOP 10 ProductName, UnitPrice FROM Products " +
"ORDER BY UnitPrice DESC";



try
{
// Open the connection
cnn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strMDB);

// if your database is secured with user-level security,
// use a connection string like this:
// string strConnection =
// "Provider=Microsoft.Jet.OLEDB.4.0;" +
// "Data Source=" strMDB +
// "User ID=TheUserName;" +
// "Password=ThePassword" ;

cnn.Open();

// Open the Command and DataReader.
cmd = new OleDbCommand(strSQL, cnn);
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (sdr != null )
{
sdr.Close();
}
}



// C#
// Start on second row.
int intRow = 2;
Object beforeRow = Type.Missing;

// Retrieve the data and insert into new rows.
while (sdr.Read())
{
tbl.Rows.Add(ref beforeRow);
tbl.Cell(intRow, 1).Range.Text =
sdr[0].ToString();
tbl.Cell(intRow, 2).Range.Text =
String.Format("{0:c}", sdr[1]);
intRow += 1;
}
// Bold the column heads.
tbl.Rows[1].Range.Bold = 1;




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
How to fill values into Tables of Word File ?  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: include javascriptjs file in Project in asp.net
Previous Resource: Implementing Globalization and Localization in ASP.NET
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use