5

Im trying to use the reportviewer provided by Visual Studio (windows form application) to create a report based on an excel spreadsheet. However I am struggling to find the correct way to read/access the spreadsheet.

When I try to create a new report I get the following window:

New report data source

I've tried to use the object option but havent had any luck

Question: How would I use an excel spreadsheet to create a report?

I had some luck with the following code which allowed me to process the file, but I cant find a way to tie it into the reportviewer:

Excel.Application ExcelObj = new Excel.Application();

this.openFileDialog1.FileName = "*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
    Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
        openFileDialog1.FileName, 0, true, 5,
        "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
        0, true);
    Excel.Sheets sheets = theWorkbook.Worksheets;
    Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
    for (int i = 1; i <= 10; i++)
    {
        Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
        System.Array myvalues = (System.Array)range.Cells.Value;
        string[] strArray = ConvertToStringArray(myvalues);
    }
}

Any advice/guidance is welcome

3
  • does the excel sheet come from external source, or you created it using your application ? Commented Feb 4, 2014 at 20:46
  • @DTs An external source, a file picker or something of the like will need to be used
    – RyanS
    Commented Feb 4, 2014 at 20:51
  • 1
    Ok, so I think that relationship Excel workesheet -> Report is not possible. What you need to do for sure is fill a DataTable with the excel data, then use this DataTable for the datasource of the Report. So, your problem comes down to two points : 1) Fill DataTable from the excel (this has nothing to do with the reports), 2) Use the DataTable as a report data source. Commented Feb 5, 2014 at 11:01

3 Answers 3

6

After much googling I managed to piece this together and get the excel sheet into a basic report.

This requires you to setup a dataset with the column names specified in the first row of the excel spreadsheet, and to also tie this dataset into a report. Then you can use the following to populate it:

[open file dialog code..]
try
{
    string path = this.openFileDialog1.FileName;
    if (Path.GetExtension(path) == ".xls")
    {
        oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
    }
    else if (Path.GetExtension(path) == ".xlsx")
    {
        oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
    }
    oledbConn.Open();

    DataSet ds = new DataSet();

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = oledbConn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT * FROM [sheet1$]";

    OleDbDataAdapter oleda = new OleDbDataAdapter();
    oleda = new OleDbDataAdapter(cmd);
    oleda.Fill(ds);
    ds.Tables[0].TableName = "DataTable1";

    this.DataTable1BindingSource.DataSource = ds;
    this.reportViewer1.RefreshReport();
}
catch (Exception ex)
{
}
finally
{
    oledbConn.Close();
}

I found these articles helpful:

Dyanmic report source

Loading excel into a dataset

2
  • Hi Ryan I think you should give the 50 points to one of the available answer and not to your solution. And is not that your code looks good or something but Adrian and me took the time to answer your question. And in your answer I think you dont need a dataset only the datatable, the dataset object is too heavy and you need to validate the data format.
    – Juan
    Commented Feb 11, 2014 at 1:59
  • 2
    If you can provide some code contribute something other than a SO link I would be more inclined. Perhaps show me and any future visitors how we would avoid the overhead of the dataset? ps. I cant actually award them to myself
    – RyanS
    Commented Feb 11, 2014 at 14:13
2

You can query the data as mention in this question.

Query excel sheet in c#

But I recommend try to create a process to obtain the data from the excel file and move to sql server or any formal database. In this process you can validate the data format and eliminate errors.

2
+25

For some parts of Visual Studio a spreadsheet is connected as a datasource by selecting Database. The ODBC drivers allow many sorts of database, including Excel spreadsheet files, to be used as a datasource.

The images shown in the question look very similar to those of the data source wizard available for Coded UI in Visual Studio 2010. That wizard is not provided in Visual Studio 2012 and people have to write the connection string manually. This page shows the connection strings used for Coded UI on Visual Studio 2013. This earlier page (dated March 2009) has a similar list of connection strings and it links to this 2005 page that gives several more connection strings.

Not the answer you're looking for? Browse other questions tagged or ask your own question.