Spire.XLS for .NET 測評

     有一位朋友推薦了我 Spire.Office (官網:http://www.e-iceblue.com/) —— 基於.NET的辦公軟件庫,說不錯。到底如何呢?只有親測一下才能知道了。

前言
     網絡上流傳的 Office 庫很多,可謂“百家爭鳴”,但有時候選擇多了反而容易讓人產生困惑,難以抉擇。所以最能吸引程序員的 Office 庫一般都具有下面的優點:
(1)功能完整
(2)接口易用
(3)文檔齊全
(4)容易集成和維護

     下面是 Spire.Office 官方提供的組件,其覆蓋了一般的辦公軟件的開發需求,像 Word、Excel、ppt、Pdf等常見的電子文檔的操作類庫都可以在這裏找到。

圖 1  Spire.Office 提供的組件

     隨安裝包一起的幫助文檔,也很完整,唯一不足就是缺少相應的文字說明,不過也不影響使用。因爲官方的在線教程(http://www.e-iceblue.com/Tutorials.html)實在是詳細的“令人髮指”,下面是官方的關於 Spire.XLS 庫的在線教程(部分),光看目錄就已經很清楚其提供的豐富功能了,每個功能點都提供了詳盡的文檔說明和示例代碼(C#/VB.NET),我下面寫的demo程序也主要是參考了官方的在線教程。
   
圖 2  官方的在線教程

Spire.XLS 介紹
     Spire.Office 提供的組件很豐富,但個人精力有限,只能挑其中一個組建的部分功能進行測試。Excel在許多環境下都常用,這裏就以Spire.XLS組件來測試下。
先看看 Spire.XLS 爲我們提供了哪些功能:
(1)只需要 Spire.XLS 組件即可獨立地完成 Excel 文件的相關操作,不需要額外安裝 Micosoft Office 軟件。
(2)提供強大且高質量的 Excel 文件轉換,包括常見的 PDF、HTML、XML、CSV、Image等
(3)創建Excel報表
(4)可自由編輯Excel工作表
(5)可在運行時方便的操作Excel單元和計算引擎
(6)還提供了圖表、數據等方面的操作

基於 Spire.XLS 的格式轉換程序

     下面是我編寫的一個demo程序,可以完成 Excel 文檔的轉換功能,編寫這個程序的實際代碼不足20行,10分鐘就編寫好了,這足以驗證 Spire.XLS 的強大和易用。

圖 3  Spire.XLS庫轉換功能測試程序


圖 4  Spire.XLS 轉換Test.xls後生成的文件

 下面是我編寫這個demo的過程,如果你感興趣可以繼續往下看。

1、下載 Spire.XLS 庫,並安裝(下載地址:http://www.e-iceblue.com/Introduce/free-xls-component.html)。

2. 在 VS2010 中新建一個 Windows Form Application 項目。

圖 5  新建 C# Windows Form 程序

3. 在項目中添加對 Spire.XLS 庫的引用。

圖 6  添加對 Spire.XLS 庫的引用(1)


                                圖 7  添加對 Spire.XLS 庫的引用(2)


圖 8  新添加的引用

4. 設計窗體界面,如下(使用默認的改控件名)

圖 9  測試程序界面

5. 添加實現代碼,如下
    思路:點擊“瀏覽”按鈕時,彈出打開文件對話框選擇源文件。默認轉換的目標文件類型是PDF(由內部成員變量保存),切換不同目標文件類型時,更新內部標識目標文件類型的變量值。點擊“轉換”按鈕時,彈出保存文件對話框,待輸入保存路徑並確認後,調用 Spire.XLS 庫完成 Excel 文件格式的轉換,同時保存到目標路徑。下面是源代碼,由於代碼不多就直接貼出來吧。
using System;
using System.Windows.Forms;
using Spire.Xls;

namespace XSLTest
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 轉換的目標文件類型
        /// </summary>
        private Spire.Xls.FileFormat fileFormat = FileFormat.PDF;

        public Form1()
        {
            InitializeComponent();
            radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton3.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton4.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton5.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton6.CheckedChanged += new EventHandler(radioButton_CheckedChanged);

        }

        /// <summary>
        /// 選擇源文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Micosoft Excel 97/2000/XP/2003 文件(*.xls)|*.xls"
                + "|" + "Micosoft Excel 2007/2010 文件(*.xlsx)|*.xlsx";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fileDialog.FileName;
            }
        }

        /// <summary>
        /// 轉換源文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            // 源文件路徑
            string sourceFilePath = textBox1.Text;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            switch (fileFormat)
            {
                case FileFormat.Bitmap:
                    saveFileDialog.Filter = "Bitmap(*.bmp)|*.bmp";
                    break;
                case FileFormat.PDF:
                    saveFileDialog.Filter = "PDF Document(*.pdf)|*.pdf";
                    break;
                case FileFormat.ODS:
                    saveFileDialog.Filter = "OpenOffice Document Spreadsheet(*.ods)|*.ods";
                    break;
                case FileFormat.CSV:
                    saveFileDialog.Filter = "CSV(*.csv)|*.csv";
                    break;
                case FileFormat.XML:
                    saveFileDialog.Filter = "XML(*.xml)|*.xml";
                    break;
                case FileFormat.XPS:
                    saveFileDialog.Filter = "XPS(*.xps)|*.xps";
                    break;
                default:
                    break;
            }
            saveFileDialog.FilterIndex = 0;
            if (saveFileDialog.ShowDialog() != DialogResult.OK)
                return;
            // 轉換後的目標文件路徑
            string destFilePath = saveFileDialog.FileName;

            // 轉換
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(sourceFilePath);
            workbook.SaveToFile(destFilePath, fileFormat);</span>

            MessageBox.Show("轉換完成", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            System.Windows.Forms.RadioButton radioButton = sender as System.Windows.Forms.RadioButton;
            if (radioButton == null)
                return;
            switch (radioButton.Text.ToUpper())
            {
                case "BITMAP":
                    fileFormat = FileFormat.Bitmap;
                    break;
                case "PDF":
                    fileFormat = FileFormat.PDF;
                    break;
                case "ODS":
                    fileFormat = FileFormat.ODS;
                    break;
                case "XPS":
                    fileFormat = FileFormat.XPS;
                    break;
                case "XML":
                    fileFormat = FileFormat.XML;
                    break;
                case "CSV":
                    fileFormat = FileFormat.CSV;
                    break;
                default:
                    break;
            }
        }
    }
}

6 程序的發佈
      基於 Spire.XLS 的程序發佈很簡單,只需要將相應的dll(可以在Spire.XLS的安裝目錄下找到)文件拷貝到和可執行程序相同目錄。

圖 10  程序的發佈

最後附上源代碼和可執行的demo程序的下載地址:http://www.kuaipan.cn/file/id_123106879533606129.htm

總結
     上面的測試只是Spire.Office的“冰山一角”,更多強大易用的功能,可以參考官方在線教程,當然如果是你感興趣的話。
     關於電子文檔操作的類庫現在很多,但像Spire.Office這樣的功能齊全、接口易用、文檔豐富的類庫較少,Spire.Office後面有商業公司做支撐,相信然後還會更好。如果項目有類似需求,像快速低成本集成類似功能,可以考慮Spire.Office
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章