views:

10

answers:

0

I have used below code to convert my silver light data-grid value into excel sheet.

But it works only out of the browser,not in the browser.

link

please do check the link,i am using code from site only,i need to fix.i have to run in browser..

 private void btnconvert_Click(object sender, RoutedEventArgs e)
    {
        int rowIndex = 2;
        int coulmnIndex = 1;

        try
        {
            dynamic excel = AutomationFactory.CreateObject("Excel.Application");

            excel.workbooks.Add();

            dynamic sheet = excel.ActiveSheet;

            dynamic headerCell1 = sheet.Cells[1, 1];

            headerCell1.Value = "RoomsData";
            headerCell1.Font.Bold = true;

            headerCell1.Interior.Color = 0xc33b;

            dynamic headerCell2 = sheet.Cells[1, 2];
            headerCell2.Value = "Today";
            headerCell2.Font.Bold = true;
            headerCell2.Interior.Color = 0xc33b;

            dynamic headerCell3 = sheet.Cells[1, 4];

            headerCell3.Value = "WeektoDate";
            headerCell3.Font.Bold = true;
            headerCell3.Interior.Color = 0xc33b;



            sheet.Range(sheet.Cells(1, 1), sheet.Cells(2, 1)).Merge();
            sheet.Range(sheet.Cells(1, 2), sheet.Cells(1, 3)).Merge();
            sheet.Range(sheet.Cells(1, 4), sheet.Cells(1, 5)).Merge();


            string fullname = "Actual,LastYear,Budget,Forecast,Budget,Forecast,Actual,Budget,Forecast,LastYear,Actual,Budget,Forecast,LastYear";
             string[] words = fullname.Split(',');
             int i = 1;
             foreach (string word in words)
             {                
                 dynamic headerCell = sheet.Cells[rowIndex, coulmnIndex + i];
                 headerCell.Value = word;
                 headerCell.Font.Bold = true;
                 headerCell.Interior.Color = 0xFF00;
                 i++;
             }
            foreach (OccupancyDBCompleteReportReference.OccupancyDBReportClass customer in ReportDG.ItemsSource)
            {
                rowIndex++;

                //column 1
                dynamic Cell1 = sheet.Cells[rowIndex, 1];
                Cell1.Value = customer.RoomData;
                Cell1.Font.Color = 003399;


                //column 2
                dynamic Cell2 = sheet.Cells[rowIndex, 2];
                Cell2.Value = customer.ActualMonthToDate;
                Cell2.Font.Color = 003399;

                //column 3
                dynamic Cell3 = sheet.Cells[rowIndex, 3];
                Cell3.Value = customer.ActualWeekToDate;
                Cell3.Font.Color = 003399;

                //column 4
                dynamic Cell4 = sheet.Cells[rowIndex, 4];
                Cell4.Value = customer.BudgetMonth;
                Cell4.Font.Color = 003399;



            }
            excel.Visible = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error generating excel: " + ex.Message);
        }
    }