Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to export the data from data table in to Excel sheet.. the data is exporting correctily but the data is not arranging in cells.all data is filling in first cell of the sheet..
Here i am sending the code i had used..can any one correct this code..
XML
private void ExcelFileCreation(DataTable dt1)
        {


            Response.AddHeader("content-disposition", "inline; filename=\"Task Assignment.xls\"");
            Response.ContentType = "application/vnd.ms-excel";
            Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            Response.Write("\r\n");

                string sep = "";
                foreach (DataColumn dc in dt1.Columns)
                {
                Response.Write("<style>.text { mso-number-format:\\@; } </style>");
                Response.Write(sep + dc.ColumnName);
                sep = "\t";
                }
                Response.Write("\n");

                int i;
                foreach (DataRow dr in dt1.Rows)
                {
                sep = "";
                for (i = 0; i < dt1.Columns.Count; i++)
                {
                Response.Write("<style>.text { mso-number-format:\\@; } </style>");
                Response.Write(sep + dr[i].ToString());
                sep = "\t";
                }
                Response.Write("\n");
                }

                Response.End();
                }


output is coming like this:
EnrolmentNumber Coursecode Marks 0813001 EE2402 0813002 EE2402 0813003 EE2402 0813004 EE2402 0813005 EE2402 0813006 EE2402 0813007 EE2402 0813008 EE2402 0813009 EE2402 0813010 EE2402 0813011 EE2402 0813012 EE2402 0813013 EE2402 0813014 EE2402 0813015 EE2402 0813016 EE2402 0813017 EE2402 0813018 EE2402 0813019 EE2402 0813020 EE2402 0813021 EE2402 0813022 EE2402 0813023 EE2402 0813024 EE2402 0813025 EE2402 0813026

Actual output i want is:
EnrolmentNumber Coursecode Marks
0813001 EE2402
0813002 EE2402
0813003 EE2402
0813004 EE2402
Posted

1 solution

C#
// TO USE:
            // 1) include COM reference to Microsoft Excel Object library
            // add namespace...
            // 2) using Excel = Microsoft.Office.Interop.Excel;
            private static void Excel_FromDataTable(DataTable dt)
            {
                // Create an Excel object and add workbook...
                Excel.ApplicationClass excel = new Excel.ApplicationClass();
                Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???

                // Add column headings...
                int iCol = 0;
                foreach (DataColumn c in dt.Columns)
                {
                    iCol++;
                    excel.Cells[1, iCol] = c.ColumnName;
                }
                // for each row of data...
                int iRow = 0;
                foreach (DataRow r in dt.Rows)
                {
                    iRow++;

                    // add each row's cell data...
                    iCol = 0;
                    foreach (DataColumn c in dt.Columns)
                    {
                        iCol++;
                        excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
                    }
                }

                // Global missing reference for objects we are not defining...
                object missing = System.Reflection.Missing.Value;

                // If wanting to Save the workbook...
                workbook.SaveAs("MyExcelWorkBook.xls",
                    Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
                    false, false, Excel.XlSaveAsAccessMode.xlNoChange,
                    missing, missing, missing, missing, missing);

                // If wanting to make Excel visible and activate the worksheet...
                excel.Visible = true;
                Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;
                ((Excel._Worksheet)worksheet).Activate();

                // If wanting excel to shutdown...
                ((Excel._Application)excel).Quit();
            }




http://www.daniweb.com/software-development/csharp/threads/213015[^]
 
Share this answer
 

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