// BackgroundWorker 執行完成 private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Result != null) { ShowSaveFileDialogRe showSaveFileDialogRe = Fun.ShowSaveFileDialog(e.Result.ToString(), null); // 存檔按鈕 if (showSaveFileDialogRe.dialogResult == DialogResult.OK) { string finishedFileName = showSaveFileDialogRe.msg; SetOutputExcelFileName(finishedFileName); // 如果是覆蓋原檔案,必需要重新讀取ComboboxItem if (finishedFileName != null) { if (finishedFileName.Equals(bwGenExcelArgs.sourceFile)) { setComboboxItem(finishedFileName); } } } else if (showSaveFileDialogRe.dialogResult == DialogResult.Abort) { Fun.showMessageBox( string.Format("{0}", showSaveFileDialogRe.msg), "SaveAs error"); } } this.SetButtonStatus(true); }
// BackgroundWorker 更新ui private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e) { BwGenExcelReport state = e.UserState as BwGenExcelReport; if (state.reportType == 1) { SetProgressBar(state.begin, state.maximum, state.value); SetText(state.msg); } else { Fun.showMessageBox(state.msg, "Error"); } }
private void Form1_Load(object sender, EventArgs e) { string version = System.Windows.Forms.Application.ProductVersion; this.Text = String.Format("Excel add image (version {0})", version); fromFilePath.Text = ""; outputExcelFileName.Text = ""; // 程式執行路徑 string executingDirectory = Fun.getExecutingDirectory(); // 2018.7.31 增加 // Nancy 反應在WinXP上縮放圖片會出現"Parameter is not valid"錯誤,所以增加完整路徑指定範例圖片 exampleImagePath = String.Format(@"{0}\{1}", executingDirectory, "example.jpg"); // 讀取ini var parser = new FileIniDataParser(); IniData data = parser.ReadFile(String.Format(@"{0}\{1}", executingDirectory, "Config.ini")); bwGenExcelArgs.imageDirectory = data["ExcelAddImage"]["ImageDirectory"]; imagePriority = data["ExcelAddImage"]["ImagePriority"]; // 檢查圖片路徑存在否 if (!Directory.Exists(bwGenExcelArgs.imageDirectory)) { Fun.showMessageBox(String.Format("\"{0}\" does not exist.", bwGenExcelArgs.imageDirectory), "Error"); } label1ImageDirectory.Text = bwGenExcelArgs.imageDirectory; // 儲存範例圖片的高、寬 picExampleHeight = picExample.Height; picExampleWidth = picExample.Width; ShowExampleImage(exampleImagePath, picExampleWidth, picExampleHeight); // 取圖片副檔名順序 if (imagePriority.Equals("1")) { rbExt1.Checked = true; } else { rbExt2.Checked = true; } }
// 存檔視窗 private void RunShowSaveFileDialogRe(string tempFilename) { ShowSaveFileDialogRe showSaveFileDialogRe = Fun.ShowSaveFileDialog(tempFilename, saveAsFile); // 存檔按鈕 if (showSaveFileDialogRe.dialogResult == DialogResult.OK) { string finishedFileName = showSaveFileDialogRe.msg; SetOutputExcelFileName(finishedFileName); openExcel.Visible = true; } else if (showSaveFileDialogRe.dialogResult == DialogResult.Abort) { Fun.showMessageBox( string.Format("{0}", showSaveFileDialogRe.msg), "SaveAs error"); } }
// BackgroundWorker 執行完成 private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Result != null) { string finishedFileName = null; try { if (showSaveAsDialogBox) { RunShowSaveFileDialogRe(e.Result.ToString()); } else { if (saveAsFile.isDirectoryExists()) { finishedFileName = bwGenExcelArgs.outputFile; File.Copy(e.Result.ToString(), finishedFileName, true); SetOutputExcelFileName(finishedFileName); openExcel.Visible = true; } else { // 指定目錄不存在要跳出MessageBox Fun.showMessageBox( String.Format("\"{0}\" destination directory does not exist.", saveAsFile.getDirectoryName()), "SaveAs error"); RunShowSaveFileDialogRe(e.Result.ToString()); } } } catch (IOException ex) { Fun.showMessageBox( string.Format("{0}", ex.Message), "SaveAs error"); } } }
private void Main2_Load(object sender, EventArgs e) { string version = System.Windows.Forms.Application.ProductVersion; this.Text = String.Format("Excel add image by command line (version {0})", version); outputExcelFileName.Text = ""; openExcel.Visible = false; if (!(args.Length == 8)) { string msg = @" EXAMPLE: ExcelAddImage.exe ""D:\te\source_excel"" ""D:\te\output_excel"" Y B C Y 1 80 parameter 1 : Source excel file (excluded .xlsx) parameter 2 : Destination excel file (excluded .xlsx) parameter 3 : Y or N, Show the SaveAs Dialog Box parameter 4 : Image path column from source excel file parameter 5 : Add image column from source excel file parameter 6 : Y or N, Insert a blank column to add image parameter 7 : 1 or 2 (PCX or JPG), Get image priority parameter 8 : Image range of height from 30 to 100 "; Fun.showMessageBox(msg, "Error : Incorrect number of parameter"); Application.Exit(); } // 檢查圖片高度參數範圍,超過範圍跳出訊息並關閉程式 if (Int32.Parse(args[7]) != Fun.imageHeightRange(Int32.Parse(args[7]))) { Fun.showMessageBox( String.Format("Check image range of height from 30 to 100"), "Error"); Application.Exit(); } sourceFile = new FileInfo2(string.Format("{0}.xlsx", args[0])); saveAsFile = new FileInfo2(string.Format("{0}.xlsx", args[1])); if (!sourceFile.isFileExists()) { Fun.showMessageBox( String.Format("\"{0}\" source excel does not exist.", sourceFile.getFullName()), "SaveAs error"); Application.Exit(); } // 程式執行路徑 string executingDirectory = Fun.getExecutingDirectory(); // 讀取ini var parser = new FileIniDataParser(); IniData data = parser.ReadFile(String.Format(@"{0}\{1}", executingDirectory, "Config.ini")); imageDirectory = data["ExcelAddImage"]["ImageDirectory"]; imagePriority = data["ExcelAddImage"]["ImagePriority"]; // 檢查圖片路徑存在否 if (!Directory.Exists(imageDirectory)) { Fun.showMessageBox(String.Format("\"{0}\" does not exist.", imageDirectory), "Error"); } // 顯示SaveAs Dialog Box if (args[2].ToUpper().Equals("Y")) { showSaveAsDialogBox = true; } else { showSaveAsDialogBox = false; } // Excel新增圖片的column是否要insert一欄 if (args[5].ToUpper().Equals("Y")) { excelColumnInsert = true; } else { excelColumnInsert = false; } // BwGenExcel參數Object bwGenExcelArgs = new BwGenExcelArgs { sourceFile = sourceFile.getFullName(), outputFile = saveAsFile.getFullName(), excelImagePathColumnIdx = Fun.GetNumberFromExcelColumn(args[3]), imageHeight = Int32.Parse(args[7]), imagePriorityList = Fun.getExtPriorityList(Int32.Parse(args[6])), imageDirectory = imageDirectory, excelAddImageColumnIdx = Fun.GetNumberFromExcelColumn(args[4]), excelColumnInsert = excelColumnInsert }; // 線程產生Excel BwGenExcel bw_DoWork = new BwGenExcel(bwGenExcelArgs); bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork.DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); bw.RunWorkerAsync(); }
// 2.加入鞋圖 private void toExcel_Click(object sender, EventArgs e) { // 2秒內避免重複按出錯 DateTime localDateTime = DateTime.Now; double now = localDateTime.ToUniversalTime().Subtract( new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; if (now - buttonClickMilliseconds < 2000) { return; } else { buttonClickMilliseconds = now; } if (bwGenExcelArgs.sourceFile == null || (!(File.Exists(bwGenExcelArgs.sourceFile)))) { Fun.showMessageBox("Please choose a excel file", "Caution"); return; } bwGenExcelArgs.imageHeight = Fun.imageHeightRange(Math.Abs(trackBarImageHeight.Value)); // 取圖片副檔名順序存入LIST if (rbExt1.Checked) { bwGenExcelArgs.imagePriorityList = Fun.getExtPriorityList(1); } else { bwGenExcelArgs.imagePriorityList = Fun.getExtPriorityList(2); } // 取Excel圖片路徑的column bwGenExcelArgs.excelImagePathColumnIdx = Int32.Parse( (cbPathColumn.SelectedItem as ComboboxItem).Value.ToString() ); // Excel新增圖片的column bwGenExcelArgs.excelAddImageColumnIdx = Int32.Parse( (cbImageColumn.SelectedItem as ComboboxItem).Value.ToString() ); // Excel新增圖片的column是否要insert一欄 bwGenExcelArgs.excelColumnInsert = chKBoxInsColumn.Checked; // 線程產生Excel BwGenExcel bw_DoWork = new BwGenExcel(bwGenExcelArgs); bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.WorkerSupportsCancellation = true; bw.DoWork += new DoWorkEventHandler(bw_DoWork.DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); // disable button this.SetButtonStatus(false); bw.RunWorkerAsync(); }