简体   繁体   English

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

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

The company that I am working for requires their inventory to be exported to an excel sheet on a regular basis. 我正在工作的公司要求将其库存定期导出到excel表。 Thankfully it is less than 50k items in total, but with how I am currently exporting, it is taking an absurd amount of time. 值得庆幸的是,它总共不到5万件物品,但是根据我目前的出口方式,这花费了很多时间。 The items are split into three different tables, but I think my issue comes from having to open multiple connections per item to get information such as converting brandID to brandName, or locationID to locationName. 这些项目分为三个不同的表,但我认为我的问题来自必须为每个项目打开多个连接以获取诸如将brandID转换为brandName或将locationID转换为locationName的信息。

Note that all code is writen in c#. 请注意,所有代码均使用c#编写。 Here is the current method that I am using to start the exporting: 这是我用来开始导出的当前方法:

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

Due to the requirements of the company, the excel sheet needs to have headers, and the same information as the datatable that is created above. 由于公司的要求,excel工作表需要具有标题,并且信息与上面创建的数据表相同。 After the creation of the exportTable , I call three other methods that go through each item table. 创建exportTable之后 ,我将调用通过每个项目表的其他三个方法。 I'll show just one of them because they are mostly the same: 我将只显示其中之一,因为它们基本相同:

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

Inside of the above method, I have to make three calls to different methods to convert the data to a meaningful string. 在上述方法的内部,我必须对不同的方法进行三个调用,以将数据转换为有意义的字符串。 One for the brand, model, and location. 一个用于品牌,型号和位置。

I am open to all sorts of ideas on how to speed this up or properly go about bulk exporting. 对于如何加快此速度或正确进行批量出口,我持各种想法。

Thank you for your time! 感谢您的时间!

Update 更新

After messing around with EPPlus, my export times were still taking way too long. 在弄乱了EPPlus之后,我的导出时间仍然过长。 I think it has to do with how I am going through each row in the database and exporting them one at a time. 我认为这与我遍历数据库中的每一行并一次导出它们有关。 Still trying to find a way to do it differently. 仍在尝试找到一种不同的方法。

I figured out what the problem was: 我发现了问题所在:

Instead of having one giant query with sub queries, I was doing multiple queries per item which was taking way too much time. 我没有对子查询进行一个大型查询,而是对每个项目进行了多个查询,这花费了太多时间。 After remembering that I could do sub queries, and implementing the changes, the export time dropped from over 30 minutes to just under 5 seconds. 记住我可以进行子查询并实施更改后,导出时间从30分钟以上减少到不到5秒。

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

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