(2)接口易用
(3)文檔齊全
(4)容易集成和維護
基於 Spire.XLS 的格式轉換程序
下面是我編寫這個demo的過程,如果你感興趣可以繼續往下看。
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