Пример #1
0
        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
0
        // 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();
        }