troubleshooting Question

Using C# to create and write to an Excel spreadsheet with multiple worksheets.

Avatar of rwheeler23
rwheeler23Flag for United States of America asked on
3 Comments1 Solution17 ViewsLast Modified:
I was reusing some C# code that creates and writes to an Excel spreadsheet. In this case the spreadsheet is going to consist of two worksheets. When I execute the code below, which only creates the column headings, the columns headings for the second worksheet overwrites the ones for the first worksheet and the second worksheet is not created.

        public static Boolean CreateExcelSpreadsheet()
            /* Initialize the Excel application object */
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            /* Check whether Excel is installed */
            if (xlApp == null)
                MessageBox.Show("Excel is not properly installed! Please install Excel or attempt a repair.");
                return (false);

            /* Create the new workbook */
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheetDetail;
            Excel.Worksheet xlWorkSheetDist;

            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp.Workbooks.Add(misValue);

            /* Write content to workbook using column headings */
            xlWorkSheetDetail = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheetDist = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            xlWorkSheetDetail.Cells[1, 1] = "Transaction Type";
            xlWorkSheetDetail.Cells[1, 2] = "Transaction Date";
            xlWorkSheetDetail.Cells[1, 3] = "Chk Nbr/Bldg";
            xlWorkSheetDetail.Cells[1, 4] = "EE Nbr/Dept";
            xlWorkSheetDetail.Cells[1, 5] = "Record ID";
            xlWorkSheetDetail.Cells[1, 6] = "Doc Amount";

            xlWorkSheetDist.Cells[1, 1] = "Transaction Type";
            xlWorkSheetDist.Cells[1, 2] = "Transaction Date";
            xlWorkSheetDist.Cells[1, 3] = "Chk Nbr/Bldg";
            xlWorkSheetDist.Cells[1, 4] = "EE Nbr/Dept";
            xlWorkSheetDist.Cells[1, 5] = "Record ID";
            xlWorkSheetDist.Cells[1, 6] = "Acct Description";
            xlWorkSheetDist.Cells[1, 7] = "GL Account";
            xlWorkSheetDist.Cells[1, 8] = "Debit Amount";
            xlWorkSheetDist.Cells[1, 9] = "Credit Amount";

            Model.xlApp = xlApp;
            Model.xlWorkBook = xlWorkBook;
            Model.xlWorkSheetDetail = xlWorkSheetDetail;
            Model.xlWorkSheetDist = xlWorkSheetDist;
            Model.misValue = misValue;

            return (true);

Freelance programmer / Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros