none
How to read data from Excel 2007 to RDLC Report by c# window RRS feed

  • Question

  • I work in windows form c# visual studio 2015
     
    I have file xlsx excel 2007 and i need to read data from Excel and show on Report Rdlc
     
    in windows form by using c#
     
    So that How can i Read data by c# from Excel and display in Rdlc windows form ? 
    • Moved by Kristin Xie Tuesday, January 24, 2017 8:19 AM Report Viewer related
    Tuesday, January 24, 2017 12:41 AM

All replies

  • What have you tried so far, please provide code so others can assist.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 24, 2017 3:19 AM
  • Hello,

    Using Report parameter you can write into RDLC from Excel.

    Below is the code for your ref.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    //Reference To Be added
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Runtime.InteropServices;
    using Microsoft.Reporting.WinForms; 
    
    namespace ReadFromExcelFile
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Excel.Application xlApp ;
                Excel.Workbook xlWorkBook ;
                Excel.Worksheet xlWorkSheet ;
                Excel.Range range ;
    
                string str;
                int rCnt ;
                int cCnt ;
                int rw = 0;
                int cl = 0;
    
                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Open(@"D:\MyExcelDoc.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    
                range = xlWorkSheet.UsedRange;
                rw = range.Rows.Count;
                cl = range.Columns.Count;
                string[] strArray = new string[3] ;
                for (rCnt = 1; rCnt  <= rw; rCnt++)
                {
                    for (cCnt = 1; cCnt  <= cl; cCnt++)
                    {
                        str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                        strArray[rCnt - 1] = str;
                    }
                }
    
                xlWorkBook.Close(true, null, null);
                xlApp.Quit();
    
                Marshal.ReleaseComObject(xlWorkSheet);
                Marshal.ReleaseComObject(xlWorkBook);
                Marshal.ReleaseComObject(xlApp);
    
                //Set to Report Parameters
                ReportParameter[] paramvar = new ReportParameter[3];
                paramvar[0] = new ReportParameter("ReportParameter1", strArray[0], false);
                paramvar[1] = new ReportParameter("ReportParameter2", strArray[1], false);
                paramvar[2] = new ReportParameter("ReportParameter3", strArray[2], false);
                this.reportViewer1.LocalReport.SetParameters(paramvar);
                this.reportViewer1.LocalReport.Refresh();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.reportViewer1.RefreshReport();
            }
        }
    }
    

    Tuesday, January 24, 2017 8:29 AM