繁体   English   中英

将三个表中的数据批量导出到一个Excel工作表中

[英]Bulk Export Data From Three Tables Into One Excel Sheet

我正在工作的公司要求将其库存定期导出到excel表。 值得庆幸的是,它总共不到5万件物品,但是根据我目前的出口方式,这花费了很多时间。 这些项目分为三个不同的表,但我认为我的问题来自必须为每个项目打开多个连接以获取诸如将brandID转换为brandName或将locationID转换为locationName的信息。

请注意,所有代码均使用c#编写。 这是我用来开始导出的当前方法:

  public void exportAllItems()
    {
        exportTable = new System.Data.DataTable();

        exportTable.Columns.Add("Vendor", typeof(string));
        exportTable.Columns.Add("Store_ID", typeof(string));
        exportTable.Columns.Add("ItemNumber", typeof(string));
        exportTable.Columns.Add("Shipment_Date", typeof(string));
        exportTable.Columns.Add("Brand", typeof(string));
        exportTable.Columns.Add("Model", typeof(string));
        exportTable.Columns.Add("Club_Type", typeof(string));
        exportTable.Columns.Add("Shaft", typeof(string));
        exportTable.Columns.Add("Number_of_Clubs", typeof(string));
        exportTable.Columns.Add("Tradein_Price", typeof(double));
        exportTable.Columns.Add("Premium", typeof(double));
        exportTable.Columns.Add("WE PAY", typeof(double));
        exportTable.Columns.Add("QUANTITY", typeof(int));
        exportTable.Columns.Add("Ext'd Price", typeof(double));
        exportTable.Columns.Add("RetailPrice", typeof(double));
        exportTable.Columns.Add("Comments", typeof(string));
        exportTable.Columns.Add("Image", typeof(string));
        exportTable.Columns.Add("Club_Spec", typeof(string));
        exportTable.Columns.Add("Shaft_Spec", typeof(string));
        exportTable.Columns.Add("Shaft_Flex", typeof(string));
        exportTable.Columns.Add("Dexterity", typeof(string));
        exportTable.Columns.Add("Destination", typeof(string));
        exportTable.Columns.Add("Received", typeof(string));
        exportTable.Columns.Add("Paid", typeof(string));

        exportAllAdd_Clubs();
        exportAllAdd_Accessories();
        exportAllAdd_Clothing();

        DataColumnCollection dcCollection = exportTable.Columns;

        // Export Data into EXCEL Sheet
        Application ExcelApp = new Application();
        ExcelApp.Application.Workbooks.Add(Type.Missing);          

        for (int i = 1; i < exportTable.Rows.Count + 2; i++)
        {
            for (int j = 1; j < exportTable.Columns.Count + 1; j++)
            {
                if (i == 1)
                {
                    ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
                }
                else
                    ExcelApp.Cells[i, j] = exportTable.Rows[i - 2][j - 
1].ToString();
            }
        }
        //Get users profile, downloads folder path, and save to workstation
        string pathUser = 
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        string pathDownload = Path.Combine(pathUser, "Downloads");
        ExcelApp.ActiveWorkbook.SaveCopyAs(pathDownload + "\\TotalInventory-
" + DateTime.Now.ToString("d MMM yyyy") + ".xlsx");
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();
    }

由于公司的要求,excel工作表需要具有标题,并且信息与上面创建的数据表相同。 创建exportTable之后 ,我将调用通过每个项目表的其他三个方法。 我将只显示其中之一,因为它们基本相同:

//Puts the clubs in the export table
    public void exportAllAdd_Clubs()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "Select * from tbl_clubs";
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {             
            exportTable.Rows.Add("", 
(lm.locationName(Convert.ToInt32(reader["locationID"]))).ToString(), 
(Convert.ToInt32(reader["sku"])).ToString(),
                "", idu.brandType(Convert.ToInt32(reader["brandID"])), 
idu.modelType(Convert.ToInt32(reader["brandID"])), 
reader["clubType"].ToString(),
                reader["shaft"].ToString(), 
reader["numberOfClubs"].ToString(), 0, Convert.ToDouble(reader["premium"]), 
Convert.ToDouble(reader["cost"]),
                Convert.ToInt32(reader["quantity"]), 0, 
Convert.ToDouble(reader["price"]), reader["comments"].ToString(), "", 
reader["clubSpec"].ToString(),
                reader["shaftSpec"].ToString(), 
reader["shaftFlex"].ToString(), reader["dexterity"].ToString(), "", "", "");
        }
        conn.Close();
    }

在上述方法的内部,我必须对不同的方法进行三个调用,以将数据转换为有意义的字符串。 一个用于品牌,型号和位置。

对于如何加快此速度或正确进行批量出口,我持各种想法。

感谢您的时间!

更新

在弄乱了EPPlus之后,我的导出时间仍然过长。 我认为这与我遍历数据库中的每一行并一次导出它们有关。 仍在尝试找到一种不同的方法。

我发现了问题所在:

我没有对子查询进行一个大型查询,而是对每个项目进行了多个查询,这花费了太多时间。 记住我可以进行子查询并实施更改后,导出时间从30分钟以上减少到不到5秒。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM