[轉貼] 使用動態資料交換 (DDE) 將 EXCEL 檔案輸出至印表機進行列印

2012032717:54
出處:http://www.dotblogs.com.tw/chou/archive/2010/04/30/14939.aspx

一、簡介

在先前的文章 [Office][C#] NPOI、OpenXML SDK、OpenOffice.org SDK 寫入資料到 EXCEL 檔案 提到 NPOI 與 OpenXML SDK 雖然可以操作 EXCEL 檔案,但沒有提供列印文件到印表機的功能。此外,OpenOffice.org SDK 雖然可以做到這點,但是當 OpenOffice 與 Microsoft Excel 交互編輯同一 EXCEL 檔案後,某些設定會遺失,(例如字型設定、列印格式)。假如沒有安裝 Microsoft Office Excel 的電腦,想要將編輯好的 EXCEL 檔案輸出至印表機列印,有什麼方法可以使用?

在此提供一種作法是在電腦安裝 Microsoft Excel Viewer,並撰寫程式,透過動態資料交換(DDE)方式將編輯好的 EXCEL 檔案輸出至印表機列印。

 

動態資料交換(DDE) 是一種通訊協定,用來在 Microsoft Windows 程式之間交換資料。在 .NET 中,我們可以透過 NDde library 撰寫 DDE 功能。而 Microsoft Excel Viewer 是套免費軟體,可以開啟、檢視,和列印 Excel 活頁簿。在先前的文章中,已經透過簡單的範例讓大家了解如何使用 NPOI 或 OpenXML SDK 寫入資料到 EXCEL 檔案中,本文介紹如何使用動態資料交換 (DDE) 將 EXCEL 檔案輸出至印表機進行列印。

附註 : 本文程式為 Windows Forms (.NET Framework 3.5 專案),開發環境為 Windows XP SP3、Visual Studio 2008 SP1。

 

 

二、程式撰寫

1. NDde 下載與加入參考

(1) 連結到 NDde 進行下載。

 

(2) 下載並且解壓縮後,可以看到包含 dll 檔案、說明文件、範例程式碼、NDde 原始碼。

 

(3) 開啟專案,將位於 Binary 下的 NDde.dll 加入參考。

 

註 : 接著進行撰寫程式,撰寫前請先安裝一種可以列印 EXCEL 檔案的軟體如 Excel Viewer,並請記住路徑與檔名。

 

 

2. 撰寫程式碼列印 EXCEL 檔案

(1) 以下撰寫一個 function 來做列印 Excel 檔案,請參考程式碼與註解說明。

using System.IO;
using System.Diagnostics;

using NDde;
using NDde.Client;


/// <summary>
/// 列印 EXCEL 檔案
/// </summary>
/// <param name="sFilePath">檔案完整路徑與名稱</param>
private void printExcel(string sFilePath)
{
    // 1. 判斷檔案是否存在
    FileInfo fi = new FileInfo(sFilePath);
    if (fi.Exists == false) return;

    // 2. 要列印 EXCEL 檔案的程式位置
    //    Excel Viewer 在 C:\Program Files\Microsoft Office\Office12\XLVIEW.exe
    string evFilePath = @"C:\Program Files\Microsoft Office\Office12\XLVIEW.exe";

    // 3. 初始化 DdeClint 類別物件 ddeClient
    //    DdeClint(Server 名稱,string topic 名稱)
    DdeClient ddeClient = new DdeClient("excel", "system");

    Process process = null;
    do
    {
        try
        {
            // 4. DDE Client 進行連結
            if (ddeClient.IsConnected == false)
            {
                ddeClient.Connect();
            }
        }
        catch (DdeException)
        {
            // 5. 開啟 Excel Viewer
            ProcessStartInfo info = new ProcessStartInfo(evFilePath);
            info.WindowStyle = ProcessWindowStyle.Minimized;
            info.UseShellExecute = true;
            info.Arguments = sFilePath;
            process = Process.Start(info);
            process.WaitForInputIdle();
        }
    } while (ddeClient.IsConnected == false && process.HasExited == false);



    // 6. DDE 處理
    try
    {
        ddeClient.Execute(string.Format(@"[Open(""{0}\"")]", sFilePath), 60000);  // 開啟 EXCEL 檔案
        ddeClient.Execute("[Print()]", 60000);  // 列印 EXCEL 檔案
        ddeClient.Execute("[Close()]", 60000);  // 關閉 EXCEL 檔案
        process.Kill();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
 

(2) 與先前文章程式整合使用。

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;

#region NPOI
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
#endregion NPOI

using System.IO;
using System.Diagnostics;

#region NDde
using NDde;
using NDde.Client;
#endregion NDde


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

        /// <summary>
        /// 列印 EXCEL 檔案
        /// </summary>
        /// <param name="sFilePath">檔案完整路徑與名稱</param>
        private void printExcel(string sFilePath)
        {
            // 1. 判斷檔案是否存在
            FileInfo fi = new FileInfo(sFilePath);
            if (fi.Exists == false) return;

            // 2. 要列印 EXCEL 檔案的程式位置
            //    Excel Viewer 在 C:\Program Files\Microsoft Office\Office12\XLVIEW.exe
            string evFilePath = @"C:\Program Files\Microsoft Office\Office12\XLVIEW.exe";

            // 3. 初始化 DdeClint 類別物件 ddeClient
            //    DdeClint(Server 名稱,string topic 名稱)
            DdeClient ddeClient = new DdeClient("excel", "system");

            Process process = null;
            do
            {
                try
                {
                    // 4. DDE Client 進行連結
                    if (ddeClient.IsConnected == false)
                    {
                        ddeClient.Connect();
                    }
                }
                catch (DdeException)
                {
                    // 5. 開啟 Excel Viewer
                    ProcessStartInfo info = new ProcessStartInfo(evFilePath);
                    info.WindowStyle = ProcessWindowStyle.Minimized;
                    info.UseShellExecute = true;
                    info.Arguments = sFilePath;
                    process = Process.Start(info);
                    process.WaitForInputIdle();
                }
            } while (ddeClient.IsConnected == false && process.HasExited == false);



            // 6. DDE 處理
            try
            {
                ddeClient.Execute(string.Format(@"[Open(""{0}\"")]", sFilePath), 60000);  // 開啟 EXCEL 檔案
                ddeClient.Execute("[Print()]", 60000);  // 列印 EXCEL 檔案
                ddeClient.Execute("[Close()]", 60000);  // 關閉 EXCEL 檔案
                process.Kill();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnNPOI_Click(object sender, EventArgs e)
        {
            string sFilePath = @"C:\NPOI.xls";
            // 建立新的 Excel 工作簿
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();

            // 在 Excel 工作簿中建立工作表,名稱為 Sheet1
            HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");

            // 寫入資料到工作表中
            sheet1.CreateRow(1).CreateCell(1).SetCellValue("點部落");
            sheet1.CreateRow(2).CreateCell(1).SetCellValue("小歐ou");
 
            // 儲存檔案
            FileStream file = new FileStream(@"C:\NPOI.xls", FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();

            // 列印 EXCEL 檔案
            printExcel(sFilePath);
        }
    }
}

(3) 執行結果

a. 按下按鈕。

b. 產生 EXCEL 並且寫入資料。

c. 於工具列看到 Excel Viewer。

d. 輸出 EXCEL 檔案到印表機列印,Excel Viewer 關閉。

 

 

三、結語

本文介紹使用 DDE 將 EXCEL 檔案進行列印,對於想要使用 NPOI 或 OpenXML SDK 編輯 EXCEL 後自動列印到印表機的程式設計者提供完整範例與說明。

 

 

四、相關連結

renjin's blog - Printing PDF Documents using C#

F6 Team - 利用程式使用DDE(Dynamic Data Exchange)來打開PDF or ExcelViewer自動列印PDF or Excel檔案