MiBarcodeでバーコード画像を作成し、Excelに貼り付けて保存する

C#で容易に扱えるバーコードプログラムを探していたところ、コマンドラインから使える「MiBarcode」があったため、利用した時のメモ。
ダウンロード先: M&I - MiBarcode

■環境

■プログラムの仕様

  1. MiBarcodeでCode128のデータを作り、クリップボードへコピー
  2. クリップボードからExcelへと貼り付け

■実装方法

  • Windowsフォームで、Buttonコントロールを貼り付け。Clickイベントに以下のソースを記載。
  • MiBarcodeはプロジェクトフォルダに入れる。プロパティは「ビルドアクション:コンテンツ」「出力ディレクトリにコピー:常にコピー」としておく。
  • Excel2003をC#より扱うため、「Microsoft Excel 11.0 Object Library」をプロジェクトの参照に追加。

■所感

1. 動作スピード
  • MiBarcodeを表示→出力→MiBarcodeを閉じるという流れのため、どうしても動作スピードが遅くなった。
  • クリップボードへのコピーに時間がかかる時もあるようなので、プログラム中でSleepするなどの処理が必要となり、これも遅くなる原因。
  • GUIで操作するとかであれば、バーコードを簡単に作ることができたので、用途次第。
2. Code128における、データ文字列について
  • 大文字と小文字の混在では正しく出力されず。
  • すべて大文字でOKだった。
3. Code128における、ラベルコメントについて

■ソース

同じバーコードを3枚出力してみたコード。
gist にも追加してみました。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace MiBarcode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string excelPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\test.xls";
            string exePath = @".\Mibarcd.exe";

            Excel.Application app = new Excel.Application();
            Excel.Workbook workbook = app.Workbooks.Add();
            Excel.Worksheet sheet = (Excel.Worksheet)workbook.Sheets[1];


            //  大文字と小文字混在では正しく出力されないため、大文字のみにする
            string barcodeValue = "thinkAmi".ToUpper();

            //  Code128で、データ文字列あり、ビットマップ形式でコピー、サイズ2倍、出力したら終了
            string args = barcodeValue + " /C128 /CC1 /CBM /TN2 /EXIT";

            int rowIndex = 1;
            int printCount = 1;

            for (int i = 0; i < 3; i++)
            {
                System.Windows.Forms.Clipboard.Clear();

                //  A,D,G列に貼り付ける
                int mod = printCount % 3;
                switch (printCount % 3)
                {
                    case 1:
                        sheet.get_Range(Cell1: "A" + rowIndex.ToString()).Select();
                        break;

                    case 2:
                        sheet.get_Range(Cell1: "D" + rowIndex.ToString()).Select();
                        break;

                    case 0:
                        sheet.get_Range(Cell1: "G" + rowIndex.ToString()).Select();
                        break;

                    default:
                        break;
                }


                System.Diagnostics.Process.Start(exePath, args);

                //  バーコードがクリップボードに転写されたら、Excelへとペーストする
                //  10秒待っても転送されない場合は、処理しない
                for (int waitCount = 0; waitCount < 10; waitCount++)
                {
                    if (System.Windows.Forms.Clipboard.ContainsImage())
                    {
                        sheet.Paste();
                        break;
                    }
                    System.Threading.Thread.Sleep(1000);
                }

                printCount++;
                System.Threading.Thread.Sleep(1000);
            }

            //  保存するときはファイルフォーマットを指定すること
            //  指定しないと、正しいファイル定義ではない旨のエラーでExcelファイルを開けなくなる
            workbook.SaveAs(Filename: excelPath, FileFormat: 56);

            workbook.Close();
            app.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
            sheet = null;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            workbook = null;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
            app = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();


            MessageBox.Show("出力しました");
        }
    }
}