Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am having an excel (multisheet) file and for each sheets there is one separate datatable and I want to write it in to the excel sheets.

For that I have written the following code and but its not working.
Workbook doc;

doc = excelApp.Workbooks.Open(strExcelFilePath);
                        SnapShot_ReporTgenerator_BackGroundWorker.ReportProgress(70); //"Creating file..."

                        Microsoft.Office.Interop.Excel.Sheets workSheets = doc.Worksheets;
                        int listCount = 1;
                        for (int i = 1; i <= list.Count; i++)
                        {
                              //code to create diffrent excel sheets
                            var xlsNewSheet = (Microsoft.Office.Interop.Excel.Worksheet)workSheets.Add(Missing.Value, workSheets[i], Missing.Value, Missing.Value);
                            
                            xlsNewSheet.Name = i + "of" + list.Count;
                            foreach (var sublist in list)
                            {
                                if (listCount == i)
                                {
                                    
                                    System.Data.DataTable table = TODataTable<string>(sublist);
                                    
                                    doc.Worksheets.Add(table, xlsNewSheet.Name); //code to add datatable to worksheets
                                    
                                }
                                listCount++;
                            }
                        }

                        doc.SaveAs(strExcelFilePath);

                        ForMat_SnapShotReport(doc);
                        excelApp.Visible = true;


I want to know what is wrong in my code.?
How to do it?
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jun-15 4:22am    
A database cannot be "inserted in Excel file", because this is beyond the Excel functionality. Excel is a spreadsheet, not a database at all. Yes, people far from software engineering often try to use it as a database tool.
—SA
Thakur JAI SINGH 9-Jun-15 5:05am    
@Sergey Alexandrovich Kryukov
so what is the solution?
Sergey Alexandrovich Kryukov 9-Jun-15 10:22am    
Solution for what? Before a solution, there should be a problem.
—SA
ramyajaya 10-Jun-15 1:52am    
Hope this helps http://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-datatable-to-excel-in-C-Sharp-using-interop/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900