Silverlight / WPF - How to show this in PDF and Excel forms

Asked By Ramachandran on 26-Dec-11 08:15 AM
I am using Silverlight MVVM pattern
 I am having a Datagrid with 6 records

ItemID ItemNo ProductName

01     Itemno01  Fan
02     Itemno02  TV
03     Itemno03  Computer

and i am having a button and checkbox named PDF and Excel  below that Datagrid

On Selecting Excel and clicking that button i want to show that above Datagrid records in that Excel forms

On Selecting PDF and clicking that button i want to show that above Datagrid records in that PDF forms

Plse help me how to do that
dipa ahuja replied to Ramachandran on 26-Dec-11 08:42 AM
This is the simple example
 
Try this way:
 
Download the dll of itextSharp.dll
 
http://sourceforge.net/projects/itextsharp/
 
and try this code
 
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
 
private void btnExport_Click(object sender, EventArgs e)
{
   Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
   PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
 
   doc.Open();//Open Document to write
 
 
   Paragraph paragraph = new Paragraph("data Exported From DataGridview!");
 
   doc.Add(paragraph);
   doc.Add(t1);
   doc.Close(); //Close document
   //
   MessageBox.Show("PDF Created!");
}
 
 
 
 
 
Jitendra Faye replied to Ramachandran on 27-Dec-11 12:15 AM
Try this code-

I found once a C# solution which I ported to Silverlight. You can download it here: http://cid-dff01c5367c1f660.skydrive.live.com/self.aspx/Silverlight%20Stuff/SLExcelLibrary.rar


1. Build the solution

2. Reference the assembly in your Silverlight project

3. Create a Linq extenstion method like this:


public
static void
ExportToExcel(this DataGrid datagrid, Stream stream)

       
{
           
Workbook workbook = new Workbook();
           
Worksheet worksheet = new Worksheet("Sheet 1");

            datagrid
.UpdateLayout();
           
IList source = datagrid.ItemsSource as IList;

           
if (source == null)
               
return;

           
//Write column headers
           
for (int i = 0; i < datagrid.Columns.Count; i++)
                worksheet
.Cells[0, i] = new Cell(datagrid.Columns[i].Header.ToString());    

           
int rowIndex = 1; //Move up one row

           
int colIndex = 0;

           
foreach (Object data in datagrid.ItemsSource)
           
{
                datagrid
.ScrollIntoView(data, null);

               
for (int i = 0; i < datagrid.Columns.Count; i++)
               
{
                   
string content = string.Empty;
                   
FrameworkElement element = datagrid.Columns[i].GetCellContent(data);

                   
if (element != null)
                   
{
                       
if (element is TextBlock)
                       
{
                            content
= (element as TextBlock).Text;
                       
}
                       
else if (element is HyperlinkButton)
                       
{
                            content
= (element as HyperlinkButton).Content.ToString();
                       
}
                       
// Cast other possible controls
                   
}

                   
//Write cell content
                    worksheet
.Cells[rowIndex, colIndex] = new Cell(content);

                    colIndex
++;
               
}
               
                rowIndex
++;
                colIndex
= 0;
           
}

            datagrid
.ScrollIntoView(source[0], null);

            workbook
.Worksheets.Add(worksheet);
            workbook
.Save(stream);
       
}
 4. Add a button to your usercontrol and handle it's click event like this:

 
private void OnExportClicked(object sender, RoutedEventArgs e)
       
{
           
if (DataGrid1.ItemsSource != null)
           
{
               
SaveFileDialog dlg = new SaveFileDialog();
                dlg
.Filter = "Microsoft Excel (*.xls)|*.xls";
                dlg
.DefaultExt = "xls";

               
if (dlg.ShowDialog().Value)
               
{
                   
try
                   
{
                       
DataGrid1.ExportToExcel(dlg.OpenFile());
                   
}
                   
catch
                   
{
                       
MessageBox.Show("Error: Flushing of memory not possible.\nCheck whether you're trying to overwrite an open document.");
                   
}
               
}
           
}
       
}