私が働いている会社は、在庫をExcelシートに定期的にエクスポートする必要があります。ありがたいことに、それは合計で50k個未満ですが、現在どのように輸出しているのか、それは不合理な時間を取っています。項目は3つの異なる表に分割されていますが、brandIDをbrandNameに変換する、locationIDをlocationNameに変換するなどの情報を得るためにアイテムごとに複数の接続を開く必要があると私は考えます。3つのテーブルから1つのExcelシートにデータを一括エクスポート
すべてのコードは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の作成後、私は各項目テーブルを通過する3つの他のメソッドを呼び出します。彼らはほとんど同じなので、私はちょうどそれらの一方を紹介します:上記の方法の内
//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();
}
、私は意味のあるデータを文字列に変換するためのさまざまな方法への3つの呼び出しを行う必要があります。 1つはブランド、モデル、場所です。
私はこれをスピードアップする方法や一括エクスポートする方法について、あらゆる種類のアイデアを公開しています。
ありがとうございました!
更新
EPPlusをいじりの後、私の輸出時間がまだあまりにも時間がかかりました。私は、データベースの各行をどのように行って、一度に1つずつエクスポートするかと関係していると思います。まだそれを違う方法で見つけようとしています。
私は 'EPPlus'をお勧めします。アイデアが公開されているので、.NETライブラリを使用してExcelファイルを作成することができます.Excel runtime.Canを使用すると、datatableからExcelファイルを簡単に作成できます。 – ClearLogic
EPPLusは、このスレッドのnuget.see私の答えで利用可能です。データテーブルからExcelファイルを作成するためのサンプルコードが含まれています。 https://stackoverflow.com/questions/38272314/pasting-html-table-from-clipboard-to-excel-worksheet-with-vb-net-application/38272572#38272572 – ClearLogic
@ClearLogicありがとう、私は確かにそれに見る – TGills