【发布时间】:2017-07-20 14:28:21
【问题描述】:
我工作的公司要求定期将他们的库存导出到 Excel 表中。值得庆幸的是,它总共不到 50k 件,但就我目前的出口方式而言,这花费了荒谬的时间。这些项目分为三个不同的表,但我认为我的问题来自必须为每个项目打开多个连接以获取信息,例如将品牌 ID 转换为品牌名称,或将 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 之后,我的导出时间仍然太长了。我认为这与我如何遍历数据库中的每一行并一次导出它们有关。仍在尝试找到一种不同的方法。
【问题讨论】:
-
因为您对想法持开放态度,我会推荐“EPPlus”。它是创建 excel 文件的 .NET 库。不需要 Excel 运行时。可以非常轻松地从数据表创建 excel 文件。
-
EPPLus 在 nuget 上可用。请参阅我在此线程中的答案。包含从数据表创建 excel 文件的示例代码。 stackoverflow.com/questions/38272314/…
-
@ClearLogic 谢谢,我一定会调查的
标签: c# sql-server excel export