究竟怎样呢?仅仅有亲測一下才干知道了。
点击“转换”button时,弹出保存文件对话框,待输入保存路径并确认后,调用 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; } } } }
假设项目有类似需求,像高速低成本集成类似功能,能够考虑Spire.Office
原文:http://www.cnblogs.com/lcchuguo/p/5118952.html