使用c#net导出到Excel

时间:2014-03-24 12:45:09

标签: c# oledb export-to-excel

任何人都可以为我提供正确的解决方案,解决为什么导出到Excel使用C#.net在导出时缺少某些列。它在导出期间在数据表中提供了一些列。我已经使用OLEDB Connection动态创建表和插入值。

我的代码:

public void DatatableToExcel(DataTable table, string strFilePath)
    {
        try
        {
            using (OleDbConnection OleConn = new OleDbConnection())
            {
                using (OleDbCommand OleCommand = new OleDbCommand())
                {
                    if (!Directory.Exists(Server.MapPath("..") + "\\ExcelFiles\\"))
                    {
                        Directory.CreateDirectory(Server.MapPath("..") + "\\ExcelFiles\\");
                    }
                    DirectoryInfo dir = new DirectoryInfo(Server.MapPath("..") + "\\ExcelFiles");
                    FileInfo[] FI = dir.GetFiles();

                    if (table.Rows.Count > 0)
                    {
                        //fileName = Server.MapPath("..") + "\\ExcelFiles\\WorkStackExport" + FI.Length + ".xls";    

                        fileName = Server.MapPath("..") + "\\ExcelFiles\\WorkstackExport" + "_" + System.Guid.NewGuid() + ".xls";// version 1.5

                        File.Delete(fileName);

                        OleConn.ConnectionString = "Data Source=" + fileName + ";Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;";
                        OleCommand.Connection = OleConn;
                        OleConn.Open();
                        //OleCommand.CommandText = "CREATE TABLE [WorkstackExport] ([Order No:] VarChar(100) NULL,[Estimate No:] VarChar(100) NULL,[Depot:] VarChar(100) NULL,[Region:] VarChar(100) NULL,[Exchange Name:] VarChar(100) NULL,[Job Category:] VarChar(100) NULL,[Work Title:] VarChar(100) NULL,[Job Pack Status:] VarChar(100) NULL,[Works Duration:] integer NULL,[Received Date:] DateTime NULL,[ARBD:] DateTime NULL,[Number of days to ARBD:] integer NULL,[RBD:] DateTime NULL,[SPD (Last DCR ARBD):] DateTime NULL,[Engg Complete Date:] DateTime NULL,[Dates Left to Close:] VarChar(100) NULL,[Completion Date:] DateTime NULL,[Latest Start Date:] DateTime NULL,[Notices Status:] Varchar(100) NULL,[Last SOLO Comment Date:] DateTime NULL,[Last SOLO Comment:] LongText NULL,[Last DCR Created Date:] DateTime NULL,[Last DCR Reason:] LongText NULL,[Total post-validation DCR Count:] integer NULL,[Location:] LongText NULL,[Order Notes:] LongText NULL,[Notice Category:] LongText NULL);";//version 1.5
                        // version  2.2 start
                        if (!((Dashlet_ID == 13 && DashletItemID == 35) || (Dashlet_ID == 13 && DashletItemID == 36) || (Dashlet_ID == 13 && DashletItemID == 37) || (Dashlet_ID == 13 && DashletItemID == 40) || (Dashlet_ID == 13 && DashletItemID == 41)))
                        {
                            OleCommand.CommandText = "CREATE TABLE [WorkstackExport] ([Order No:] VarChar(100) NULL,[Estimate No:] VarChar(100) NULL,[Depot:] VarChar(100) NULL,[Region:] VarChar(100) NULL,[Exchange Name:] VarChar(100) NULL,[Job Category:] VarChar(100) NULL,[Work Title:] VarChar(100) NULL,[Job Pack Status:] VarChar(100) NULL,[Works Duration:] integer NULL,[Received Date:] DateTime NULL,[ARBD:] DateTime NULL,[Number of days to ARBD:] integer NULL,[RBD:] DateTime NULL,[SPD (Last DCR ARBD):] DateTime NULL,[Engg Complete Date:] DateTime NULL,[Days Left to Close:] VarChar(100) NULL,[Closed Date:] DateTime NULL,[Latest Start Date:] DateTime NULL,[Latest Notice Date:] DateTime NULL,[Notices Status:] Varchar(250) NULL,[Last SOLO Comment Date:] DateTime NULL,[Last SOLO Comment:] LongText NULL,[Last DCR Created Date:] DateTime NULL,[Last DCR Reason:] LongText NULL,[Post validation DCR count:] integer NULL,[Location:] LongText NULL,[Order Notes:] LongText NULL,[Notice Category:] LongText NULL);";//version 1.5  // version 2.4  // version 2.7
                        }
                        else
                        {
                            OleCommand.CommandText = "CREATE TABLE [WorkstackExport] ([Order No:] VarChar(100) NULL,[Estimate No:] VarChar(100) NULL,[Depot:] VarChar(100) NULL,[Region:] VarChar(100) NULL,[Exchange Name:] VarChar(100) NULL,[Job Category:] VarChar(100) NULL,[Work Title:] VarChar(100) NULL,[Job Pack Status:] VarChar(100) NULL,[Works Duration:] integer NULL,[Supplier Authorised Value(£):] VarChar(100) NULL,[Received Date:] DateTime NULL,[ARBD:] DateTime NULL,[Number of days to ARBD:] integer NULL,[RBD:] DateTime NULL,[SPD (Last DCR ARBD):] DateTime NULL,[Engg Complete Date:] DateTime NULL,[Days Left to Close:] VarChar(100) NULL,[Closed Date:] DateTime NULL,[Latest Start Date:] DateTime NULL,[Latest Notice Date:] DateTime NULL,[Notices Status:] Varchar(250) NULL,[Last SOLO Comment Date:] DateTime NULL,[Last SOLO Comment:] LongText NULL,[Last DCR Created Date:] DateTime NULL,[Last DCR Reason:] LongText NULL,[Post validation DCR count:] integer NULL,[Location:] LongText NULL,[Order Notes:] LongText NULL,[Notice Category:] LongText NULL);";//version 1.5  // version 2.4  // version 2.7 // version  3.3 

                        }
                        // version  2.2 end
                        OleCommand.ExecuteNonQuery();

                        foreach (DataRow dr in table.Rows)
                        {

                            // start version 1.4 
                            OleDbCommand objcmd = new OleDbCommand();
                            objcmd.Connection = OleConn;

                            objcmd.CommandType = CommandType.Text;
                            // version  2.2 start           
                            string strcmd = string.Empty;
                            if (!((Dashlet_ID == 13 && DashletItemID == 35) || (Dashlet_ID == 13 && DashletItemID == 36) || (Dashlet_ID == 13 && DashletItemID == 37) || (Dashlet_ID == 13 && DashletItemID == 40) || (Dashlet_ID == 13 && DashletItemID == 41)))
                            {
                                strcmd = "INSERT INTO [WorkstackExport] ([Order No:],[Estimate No:],[Depot:],[Region:],[Exchange Name:],[Job Category:],[Work Title:],[Job Pack Status:],[Works Duration:],[Received Date:],[ARBD:],[Number of days to ARBD:],[RBD:],[SPD (Last DCR ARBD):],[Engg Complete Date:],[Days Left to Close:],[Closed Date:],[Latest Start Date:],[Latest Notice Date:],[Notices Status:],[Last SOLO Comment Date:],[Last SOLO Comment:],[Last DCR Created Date:],[Last DCR Reason:],[Post validation DCR count:],[Location:],[Order Notes:],[Notice Category:]) Values (@Order_No,@Estimate_No,@Depot,@RR_Description,@Exchange,@JobCategory_ID,@Work_Title,@Description,@Duration_Of_Works,@Received_Date,@New_RequiredBy_Date,@DaysToARBD,@Required_by_date,@Supplier_Promise_Date,@Engg_Complete_Date,@DaysToClose,@Completion_Date,@Latest_StartDate,@Latest_NoticeDate,@Notices_Status,@Last_SOLOCommentDate,@Last_SOLOComment,@Last_DCRCreatedDate,@Last_DCRReason,@Total_DCRCount,@Location,@Order_Notes,@Notice_Category)"; //version 1.5 // version 2.4 // version 2.7
                            }
                            else
                            {
                                strcmd = "INSERT INTO [WorkstackExport] ([Order No:],[Estimate No:],[Depot:],[Region:],[Exchange Name:],[Job Category:],[Work Title:],[Job Pack Status:],[Works Duration:],[Supplier Authorised Value(£):],[Received Date:],[ARBD:],[Number of days to ARBD:],[RBD:],[SPD (Last DCR ARBD):],[Engg Complete Date:],[Days Left to Close:],[Closed Date:],[Latest Start Date:],[Latest Notice Date:],[Notices Status:],[Last SOLO Comment Date:],[Last SOLO Comment:],[Last DCR Created Date:],[Last DCR Reason:],[Post validation DCR count:],[Location:],[Order Notes:],[Notice Category:]) Values (@Order_No,@Estimate_No,@Depot,@RR_Description,@Exchange,@JobCategory_ID,@Work_Title,@Description,@Duration_Of_Works,@AuthorizedValue,@Received_Date,@New_RequiredBy_Date,@DaysToARBD,@Required_by_date,@Supplier_Promise_Date,@Engg_Complete_Date,@DaysToClose,@Completion_Date,@Latest_StartDate,@Latest_NoticeDate,@Notices_Status,@Last_SOLOCommentDate,@Last_SOLOComment,@Last_DCRCreatedDate,@Last_DCRReason,@Total_DCRCount,@Location,@Order_Notes,@Notice_Category)"; //version 1.5 // version 2.4 // version 2.7
                            }

                            // version  2.2 end
                            //string strcmd = "INSERT INTO [WorkstackExport] ([Order No:],[Estimate No:],[Depot:],[Region:],[Exchange Name:],[Job Category:],[Work Title:],[Job Pack Status:],[Works Duration:],[Received Date:],[ARBD:],[Number of days to ARBD:],[RBD:],[SPD (Last DCR ARBD):],[Engg Complete Date:],[Dates Left to Close:],[Completion Date:],[Latest Start Date:],[Notices Status:],[Last SOLO Comment Date:],[Last SOLO Comment:],[Last DCR Created Date:],[Last DCR Reason:],[Total post-validation DCR Count:]) Values (@Order_No,@Estimate_No,@Depot,@RR_Description,@Exchange,@JobCategory_ID,@Work_Title,@Description,@Duration_Of_Works,@Received_Date,@New_RequiredBy_Date,@DaysToARBD,@Required_by_date,@Supplier_Promise_Date,@Engg_Complete_Date,@DaysToClose,@Completion_Date,@Latest_StartDate,@Notices_Status,@Last_SOLOCommentDate,@Last_SOLOComment,@Last_DCRCreatedDate,@Last_DCRReason,@Total_DCRCount)"; //version 1.5

                            objcmd = new OleDbCommand(strcmd, OleConn);

                            objcmd.Parameters.Add(new OleDbParameter("@Order_No", dr["Order_No"]));
                            objcmd.Parameters.Add(new OleDbParameter("@Estimate_No", dr["Estimate_No"]));
                            objcmd.Parameters.Add(new OleDbParameter("@Depot", dr["Depot"]));
                            objcmd.Parameters.Add(new OleDbParameter("@RR_Description", dr["RR_Description"]));
                            objcmd.Parameters.Add(new OleDbParameter("@Exchange", dr["Exchange"]));
                            objcmd.Parameters.Add(new OleDbParameter("@JobCategory_ID", dr["JobCategory_ID"]));
                            objcmd.Parameters.Add(new OleDbParameter("@Work_Title", dr["Work_Title"]));
                            objcmd.Parameters.Add(new OleDbParameter("@Description", dr["Description"]));
                            objcmd.Parameters.Add(new OleDbParameter("@Duration_Of_Works", dr["Duration_Of_Works"]));
                            if ((Dashlet_ID == 13 && DashletItemID == 35) || (Dashlet_ID == 13 && DashletItemID == 36) || (Dashlet_ID == 13 && DashletItemID == 37) || (Dashlet_ID == 13 && DashletItemID == 40) || (Dashlet_ID == 13 && DashletItemID == 41))
                            {
                                // version  3.3 start
                                string strAuthorisedValue = dr["Authorized_Value"].ToString();
                                strAuthorisedValue = GetNumberWithQ(strAuthorisedValue);
                                objcmd.Parameters.Add(new OleDbParameter("@AuthorizedValue", strAuthorisedValue));
                                // version  3.3 End
                            }
                            if (dr["Received_Date"].ToString() != "")
                            {
                                DateTime RD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Received_Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Received_Date", RD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Received_Date", dr["Received_Date"]));
                            }


                            if (dr["New_RequiredBy_Date"].ToString() != "")
                            {
                                DateTime NRBD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["New_RequiredBy_Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@New_RequiredBy_Date", NRBD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@New_RequiredBy_Date", dr["New_RequiredBy_Date"]));
                            }

                            objcmd.Parameters.Add(new OleDbParameter("@DaysToARBD", dr["DaysToARBD"]));

                            if (dr["Required_by_date"].ToString() != "")
                            {
                                DateTime RBD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Required_by_date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Required_by_date", RBD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Required_by_date", dr["Required_by_date"]));
                            }

                            if (dr["Supplier_Promise_Date"].ToString() != "")
                            {
                                DateTime SPD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Supplier_Promise_Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Supplier_Promise_Date", SPD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Supplier_Promise_Date", dr["Supplier_Promise_Date"]));
                            }

                            if (dr["Engg_Complete_Date"].ToString() != "")
                            {
                                DateTime ECD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Engg_Complete_Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Engg_Complete_Date", ECD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Engg_Complete_Date", dr["Engg_Complete_Date"]));
                            }

                            objcmd.Parameters.Add(new OleDbParameter("@DaysToClose", dr["DaysToClose"]));

                            if (dr["Completion_Date"].ToString() != "")
                            {
                                DateTime CD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Completion_Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Completion_Date", CD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Completion_Date", dr["Completion_Date"]));
                            }

                            if (dr["Latest Start Date"].ToString() != "")
                            {
                                DateTime LSD = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Latest Start Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Latest_StartDate", LSD));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Latest_StartDate", dr["Latest Start Date"]));
                            }

                            if (dr["Latest Notice Date"].ToString() != "")
                            {
                                DateTime LND = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Latest Notice Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Latest_NoticeDate", LND));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Latest_NoticeDate", dr["Latest Notice Date"]));
                            }


                            objcmd.Parameters.Add(new OleDbParameter("@Notices_Status", dr["Notices Status"]));

                            if (dr["Last SOLO Comment Date"].ToString() != "")
                            {
                                DateTime LSCDate = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Last SOLO Comment Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Last_SOLOCommentDate", LSCDate));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Last_SOLOCommentDate", dr["Last SOLO Comment Date"]));
                            }

                            objcmd.Parameters.Add(new OleDbParameter("@Last_SOLOComment", dr["Last SOLO Comment"]));

                            if (dr["Last DCR Created Date"].ToString() != "")
                            {
                                DateTime LDCDate = Convert.ToDateTime(cfs.setDate(Convert.ToDateTime(dr["Last DCR Created Date"])));
                                objcmd.Parameters.Add(new OleDbParameter("@Last_DCRCreatedDate", LDCDate));
                            }
                            else
                            {
                                objcmd.Parameters.Add(new OleDbParameter("@Last_DCRCreatedDate", dr["Last DCR Created Date"]));
                            }

                            objcmd.Parameters.Add(new OleDbParameter("@Last_DCRReason", dr["Last DCR Reason"]));

                            objcmd.Parameters.Add(new OleDbParameter("@Total_DCRCount", dr["Post Validation DCR Count"]));

                            objcmd.Parameters.Add(new OleDbParameter("@Location", dr["Location"]));

                            objcmd.Parameters.Add(new OleDbParameter("@Order_Notes", dr["Order Notes"]));

                            objcmd.Parameters.Add(new OleDbParameter("@Notice_Category", dr["Notice Category"]));

                            objcmd.ExecuteNonQuery();
                            objcmd.Dispose();

                            // end version 1.4
                        }

                        OleConn.Close();
                    }
                }
            }
            Download(fileName);


        }
        catch (Exception ex)
        {
            throw (ex);


        }
        finally
        {

        }

    }

0 个答案:

没有答案