将数据从数据表导出到Excel工作表 [英] Exporting the data from data table in to Excel sheet

查看:89
本文介绍了将数据从数据表导出到Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想将数据表中的数据导出到Excel工作表中..数据正在正确导出,但数据未在单元格中排列.所有数据正在工作表的第一个单元格中填充..
我在这里发送我曾经使用过的代码..任何人都可以更正此代码..

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..

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();
                }



输出是这样的:
EnrolmentNumber Coursecode标记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

我想要的实际输出是:
入学人数课程代码标记
0813001 EE2402
0813002 EE2402
0813003 EE2402
0813004 EE2402



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

推荐答案

// 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 [ ^ ]





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


这篇关于将数据从数据表导出到Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆