Ejemplo n.º 1
0
        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;
            }
        }
Ejemplo n.º 2
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();
        }