1

I want to display two data tables one after another in the same excel worksheet in c#.I have tried like this.

var workBook = new XLWorkbook();
workBook.Worksheets.Add(dataSet.table1);
workBook.Worksheets.Add(dataSet.table2);

But it was creating two worksheets in the same excel. Anyone help me how can i do that?

sindhu jampani
  • 504
  • 3
  • 10
  • 30
  • 1
    Incidentally, you should [never use 2 dots with com objects](http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects) – Alex Jul 23 '14 at 05:55
  • Alex Can u please suggest me anything more than that how to achieve my requirement? – sindhu jampani Jul 23 '14 at 06:00
  • 1
    I don't know `XLWorkBook` class well enough to answer this but, logically `Worksheets.Add(..)` sounds exactly for adding new worksheets to workbook. I guess you should somehow get the worksheet you want to use first than add the `dataSet.table1-2` to that worksheet. – Tolga Evcimen Jul 23 '14 at 06:01

1 Answers1

3

Try to do the following:

workBook.Worksheets.Add(dataSet.table1.Merge(dataSet.table2));  

It would add 1 worksheet with merged data from the 2 tables.

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
InbalIta
  • 61
  • 8
  • Merge is asking for DataRow as its argument.Its not taking the table – sindhu jampani Jul 23 '14 at 06:33
  • 1
    As far as I can see in my VS, argument is DataTable... as I refer your 'dataset' variable of kind "DataSet". It will be posiible to work with a function that requieres DataRow as well: foreach DataRow in table2 Merge table 1 with DataRow And then add worksheet with table 1, containing all data – InbalIta Jul 23 '14 at 07:33
  • Thankyou.Do you have any idea of displaying one table below the another one,instead of merging both the tables? – sindhu jampani Jul 23 '14 at 09:46
  • Have only one idea not including merging: loop on all rows of table1 and write it to the worksheet and then do the same for table2. You can write data directly into a certain row in the worksheet or to certain cell. I think it's less efficient. Merging will probably give you bettr run time. – InbalIta Jul 23 '14 at 09:53