How to export 2 datatables of a single dataset to 2
different worksheets of a single MSExcel file ?
Answer / Neha Verma
To export two data tables from a single dataset to separate worksheets in an Excel file using C#, you can utilize the Microsoft.Office.Interop.Excel library. First, create and format the workbook and worksheets as needed, then add each data table to its respective worksheet using the 'Range.Value' property. Here's a basic example:
```csharp
using Excel = Microsoft.Office.Interop.Excel;
// Create a new Excel application instance
var excelApp = new Excel.Application();
// Create a new workbook
var workbook = excelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
// Get the first worksheet
var worksheet1 = (Excel.Worksheet)workbook.ActiveSheet;
var dataTable1 = ...; // Your first data table
worksheet1.Cells[1, 1].Value2 = "Data Table 1";
worksheet1.Range(worksheet1.Cells[1, 1], worksheet1.Cells[dataTable1.Rows.Count + 1, dataTable1.Columns.Count]).Value2 = dataTable1.AsEnumerable();
// Get the second worksheet
var worksheet2 = (Excel.Worksheet)workbook.Sheets.Add(Missive.msoSheetDefault);
worksheet2.Cells[1, 1].Value2 = "Data Table 2";
worksheet2.Range(worksheet2.Cells[1, 1], worksheet2.Cells[dataTable2.Rows.Count + 1, dataTable2.Columns.Count]).Value2 = dataTable2.AsEnumerable();
// Save the workbook as an Excel file
workbook.SaveAs("path osavefile.xlsx");
// Close the Excel application and release resources
excelApp.Quit();
Marshal.ReleaseComObject(excelApp);
```
| Is This Answer Correct ? | 0 Yes | 0 No |
How to Link Different Data Sources Together?
How to Create a Treeview Menu in ASP.NET with C#?
Code for Reading and writing from a file in c#?
How to use ASP.NET 2.0's TreeView to Display Hierarchical Data?
Code for Working with Files under a Directory?
program for string reverse(eg:- i am boy -> boy am i)
8 Answers Black Pepper, Infosys, Mind Tree,
how to get the table names via c sharp and column names also?
IS Array list is generic or non generic
Can you declare an array of mixed Types?
How to export 2 datatables of a single dataset to 2 different worksheets of a single MSExcel file ?
<ul> <li id="Accommodation" runat="server" visible="false"><a href="../Elements/frmSearchAccommodation.aspx?submenuheader=1">Accommodation</a></li> <li id="Arts" runat="server" visible="false"><a href="../Elements/frmSearchProp.aspx?submenuheader=1">Arts And Props</a></li> <li id="Costume" runat="server" visible="false"><a href="../Elements/frmSearchCostume.aspx?submenuheader=1">Costume</a></li> <li id="Crew" runat="server" visible="false"><a href="../Elements/frmSearchCrew.aspx?submenuheader=1">Crew</a></li> <li id="Catering" runat="server" visible="false"><a href="../Elements/frmSearchCatering.aspx?submenuheader=1">Catering</a></li> <li id="Equipment" runat="server" visible="false"><a href="../Elements/frmSearchEquipment.aspx?submenuheader=1">Equipment</a></li> <li id="Film" runat="server" visible="false"><a href="../Elements/frmSearchFilmTapeStock.aspx?submenuheader=1">Film And Tape Stocks</a></li> <li id="Location" runat="server" visible="false"><a href="../Elements/frmSearchLocation.aspx?submenuheader=1">Location</a></li> <li id="Picture" runat="server" visible="false"><a href="../Elements/frmSearchPictureVehicle.aspx?submenuheader=1">Picture Vehicles</a></li> <li id="Production" runat="server" visible="false"><a href="../Elements/frmSearchProductionSupplies.aspx?submenuheader=1">Production Supplies</a></li> <li id="Post" runat="server" visible="false"><a href="../Elements/frmSearchPostProductionHouse.aspx?submenuheader=1">Post Production Houses</a></li> <li id="Rigging" runat="server" visible="false"><a href="../Elements/frmSearchRigging.aspx?submenuheader=1">Rigging</a></li> <li id="Stunt" runat="server" visible="false"><a href="../Elements/frmSearchStunt.aspx?submenuheader=1">Stunt</a></li> <li id="Studio" runat="server" visible="false"><a href="../Elements/frmSearchStudio.aspx?submenuheader=1">Studio</a></li> <li id="SFX" runat="server" visible="false"><a href="../Elements/frmSearchSFX.aspx?submenuheader=1">SFX</a></li> <li id="Talents" runat="server" visible="false"><a href="../Elements/frmSearchTalent.aspx?submenuheader=1">Talents</a></li> <li id="Transport" runat="server" visible="false"><a href="../Elements/frmSearchTransport.aspx?submenuheader=1">Transport</a></li> <li id="Venue" runat="server" visible="false"><a href="../Elements/frmSearchVenue.aspx?submenuheader=1">Venue</a></li> <li id="Other" runat="server" visible="false"><a href="../PageUnderConstruction.aspx?submenuheader=1">Other</a></li> </ul> I have this code in leftmenu.ascx page..now on leftmenu.ascx.cs page i want to access id values. but i dont want to check manually like Accommodation,Arts etc.. In cs file my code is like string str[]; //this array contains some values say 10 strings for(i=0;i<100;i++) { if(str[i]==id id.visible=true } i.e.i want to travel for each element of str and each value of id.if they match then id.visible=true how to do?
Code for IP Address Comparison and Conversion in C#?