windows窗体(winform)中嵌入显示Excel工作表。
生活随笔
收集整理的這篇文章主要介紹了
windows窗体(winform)中嵌入显示Excel工作表。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
環境:c#2005,Excel2003.
1 實現后的效果如下圖:
(截圖)
2 實現代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Owc11;
namespace CityToProvince.GUILayer
{
??? public partial class frmExcel : Form
??? {
??????? public frmExcel()
??????? {
??????????? InitializeComponent();
??????? }
??????? private void buttonOpen_Click(object sender, EventArgs e)
??????? {
??????????? OpenFileDialog myOpenFileDialog = new OpenFileDialog();
??????????? myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls|xml格式表格(*.xml)|*.xml";
??????????? myOpenFileDialog.FilterIndex = 2;
??????????? myOpenFileDialog.Title = "選擇xml文件";
??????????? myOpenFileDialog.InitialDirectory = "c:\\";
??????????? myOpenFileDialog.RestoreDirectory = true;
??????????? DialogResult dr = myOpenFileDialog.ShowDialog();
??????????? if (dr == DialogResult.OK)
??????????? {
??????????????? try
??????????????? {
??????????????????? this.axSpreadsheetExcel.XMLURL = myOpenFileDialog.FileName;
??????????????? }
??????????????? catch
??????????????? {
??????????????????? MessageBox.Show("錯誤");
??????????????? }
??????????? }
??????? }
??????? private void buttonConvert_Click(object sender, EventArgs e)
??????? {//xls->xml and open
??????????? OpenFileDialog myOpenFileDialog = new OpenFileDialog();
??????????? myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls";
??????????? myOpenFileDialog.FilterIndex = 1;
??????????? myOpenFileDialog.Title = "選擇Excel文件";
??????????? myOpenFileDialog.InitialDirectory = "c:\\";
??????????? myOpenFileDialog.RestoreDirectory = true;
??????????? DialogResult dr = myOpenFileDialog.ShowDialog();
??????????? if (dr == DialogResult.OK)
??????????? {
??????????????? try
??????????????? {
??????????????????? ExcelProcess.ExcelProcess myExcel = new ExcelProcess.ExcelProcess();
??????????????????? myExcel.OpenFileName = myOpenFileDialog.FileName;
??????????????????? string saveXmlName = myOpenFileDialog.FileName.ToString().Replace("xls", "xml");
??????????????????? if (System.IO.File.Exists(saveXmlName))
??????????????????? {
??????????????????????? System.IO.File.Delete(saveXmlName);
??????????????????? }
??????????????????? myExcel.SaveFileName = saveXmlName;
??????????????????? myExcel.OpenExcelFile();
??????????????????? myExcel.SaveExcelAsXML();
??????????????????? myExcel.CloseExcelApplication();
??????????????? }
??????????????? catch
??????????????? {
??????????????????? MessageBox.Show("轉換錯誤");
??????????????? }
??????????? }
??????? }
??????? private void buttonClose_Click(object sender, EventArgs e)
??????? {
??????????? //this.axSpreadsheetExcel.XMLURL = null;
??????????? this.Close();
??????? }
??????? private void buttonConvertOpen_Click(object sender, EventArgs e)
??????? {
??????????? //xls->xml and open
??????????? OpenFileDialog myOpenFileDialog = new OpenFileDialog();
??????????? myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls";
??????????? myOpenFileDialog.FilterIndex = 1;
??????????? myOpenFileDialog.Title = "選擇Excel文件";
??????????? myOpenFileDialog.InitialDirectory = "c:\\";
??????????? myOpenFileDialog.RestoreDirectory = true;
??????????? DialogResult dr = myOpenFileDialog.ShowDialog();
??????????? if (dr == DialogResult.OK)
??????????? {
??????????????? try
??????????????? {
??????????????????? ExcelProcess.ExcelProcess myExcel = new ExcelProcess.ExcelProcess();
??????????????????? myExcel.OpenFileName = myOpenFileDialog.FileName;
??????????????????? string saveXmlName = myOpenFileDialog.FileName.ToString().Replace("xls", "xml");
??????????????????? if (System.IO.File.Exists(saveXmlName))
??????????????????? {
??????????????????????? System.IO.File.Delete(saveXmlName);
??????????????????? }
??????????????????? myExcel.SaveFileName = saveXmlName;
??????????????????? myExcel.OpenExcelFile();
??????????????????? myExcel.SaveExcelAsXML();
??????????????????? myExcel.CloseExcelApplication();
??????????????????? this.axSpreadsheetExcel.XMLURL = saveXmlName;
??????????????? }
??????????????? catch
??????????????? {
??????????????????? MessageBox.Show("轉換錯誤");
??????????????? }
??????????? }
??????? }
??????? private void buttonSave_Click(object sender, EventArgs e)
??????? {
??????????? SaveFileDialog mySaveFileDialog = new SaveFileDialog();
??????????? mySaveFileDialog.Title = "導出Excel文件的名稱";
??????????? mySaveFileDialog.Filter = "Excel文件|*.xls";
??????????? string exportExcelFile;
??????????? mySaveFileDialog.InitialDirectory = SysCommon.ShareData.ExportExcelPath;
??????????? DialogResult mydr = mySaveFileDialog.ShowDialog();
??????????? if (mydr == DialogResult.OK)
??????????? {
??????????????? exportExcelFile = mySaveFileDialog.FileName;
??????????????? this.axSpreadsheetExcel.Export(exportExcelFile, SheetExportActionEnum.ssExportActionNone,
??????????????????? SheetExportFormat.ssExportAsAppropriate);
??????????? }
??????? }
??? }
}
其中ExcelProcess是我寫的一個操作excel的類。
轉載:http://emanlee.cnblogs.com/
1 實現后的效果如下圖:
(截圖)
2 實現代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Owc11;
namespace CityToProvince.GUILayer
{
??? public partial class frmExcel : Form
??? {
??????? public frmExcel()
??????? {
??????????? InitializeComponent();
??????? }
??????? private void buttonOpen_Click(object sender, EventArgs e)
??????? {
??????????? OpenFileDialog myOpenFileDialog = new OpenFileDialog();
??????????? myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls|xml格式表格(*.xml)|*.xml";
??????????? myOpenFileDialog.FilterIndex = 2;
??????????? myOpenFileDialog.Title = "選擇xml文件";
??????????? myOpenFileDialog.InitialDirectory = "c:\\";
??????????? myOpenFileDialog.RestoreDirectory = true;
??????????? DialogResult dr = myOpenFileDialog.ShowDialog();
??????????? if (dr == DialogResult.OK)
??????????? {
??????????????? try
??????????????? {
??????????????????? this.axSpreadsheetExcel.XMLURL = myOpenFileDialog.FileName;
??????????????? }
??????????????? catch
??????????????? {
??????????????????? MessageBox.Show("錯誤");
??????????????? }
??????????? }
??????? }
??????? private void buttonConvert_Click(object sender, EventArgs e)
??????? {//xls->xml and open
??????????? OpenFileDialog myOpenFileDialog = new OpenFileDialog();
??????????? myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls";
??????????? myOpenFileDialog.FilterIndex = 1;
??????????? myOpenFileDialog.Title = "選擇Excel文件";
??????????? myOpenFileDialog.InitialDirectory = "c:\\";
??????????? myOpenFileDialog.RestoreDirectory = true;
??????????? DialogResult dr = myOpenFileDialog.ShowDialog();
??????????? if (dr == DialogResult.OK)
??????????? {
??????????????? try
??????????????? {
??????????????????? ExcelProcess.ExcelProcess myExcel = new ExcelProcess.ExcelProcess();
??????????????????? myExcel.OpenFileName = myOpenFileDialog.FileName;
??????????????????? string saveXmlName = myOpenFileDialog.FileName.ToString().Replace("xls", "xml");
??????????????????? if (System.IO.File.Exists(saveXmlName))
??????????????????? {
??????????????????????? System.IO.File.Delete(saveXmlName);
??????????????????? }
??????????????????? myExcel.SaveFileName = saveXmlName;
??????????????????? myExcel.OpenExcelFile();
??????????????????? myExcel.SaveExcelAsXML();
??????????????????? myExcel.CloseExcelApplication();
??????????????? }
??????????????? catch
??????????????? {
??????????????????? MessageBox.Show("轉換錯誤");
??????????????? }
??????????? }
??????? }
??????? private void buttonClose_Click(object sender, EventArgs e)
??????? {
??????????? //this.axSpreadsheetExcel.XMLURL = null;
??????????? this.Close();
??????? }
??????? private void buttonConvertOpen_Click(object sender, EventArgs e)
??????? {
??????????? //xls->xml and open
??????????? OpenFileDialog myOpenFileDialog = new OpenFileDialog();
??????????? myOpenFileDialog.Filter = "Excel文件(*.xls)|*.xls";
??????????? myOpenFileDialog.FilterIndex = 1;
??????????? myOpenFileDialog.Title = "選擇Excel文件";
??????????? myOpenFileDialog.InitialDirectory = "c:\\";
??????????? myOpenFileDialog.RestoreDirectory = true;
??????????? DialogResult dr = myOpenFileDialog.ShowDialog();
??????????? if (dr == DialogResult.OK)
??????????? {
??????????????? try
??????????????? {
??????????????????? ExcelProcess.ExcelProcess myExcel = new ExcelProcess.ExcelProcess();
??????????????????? myExcel.OpenFileName = myOpenFileDialog.FileName;
??????????????????? string saveXmlName = myOpenFileDialog.FileName.ToString().Replace("xls", "xml");
??????????????????? if (System.IO.File.Exists(saveXmlName))
??????????????????? {
??????????????????????? System.IO.File.Delete(saveXmlName);
??????????????????? }
??????????????????? myExcel.SaveFileName = saveXmlName;
??????????????????? myExcel.OpenExcelFile();
??????????????????? myExcel.SaveExcelAsXML();
??????????????????? myExcel.CloseExcelApplication();
??????????????????? this.axSpreadsheetExcel.XMLURL = saveXmlName;
??????????????? }
??????????????? catch
??????????????? {
??????????????????? MessageBox.Show("轉換錯誤");
??????????????? }
??????????? }
??????? }
??????? private void buttonSave_Click(object sender, EventArgs e)
??????? {
??????????? SaveFileDialog mySaveFileDialog = new SaveFileDialog();
??????????? mySaveFileDialog.Title = "導出Excel文件的名稱";
??????????? mySaveFileDialog.Filter = "Excel文件|*.xls";
??????????? string exportExcelFile;
??????????? mySaveFileDialog.InitialDirectory = SysCommon.ShareData.ExportExcelPath;
??????????? DialogResult mydr = mySaveFileDialog.ShowDialog();
??????????? if (mydr == DialogResult.OK)
??????????? {
??????????????? exportExcelFile = mySaveFileDialog.FileName;
??????????????? this.axSpreadsheetExcel.Export(exportExcelFile, SheetExportActionEnum.ssExportActionNone,
??????????????????? SheetExportFormat.ssExportAsAppropriate);
??????????? }
??????? }
??? }
}
其中ExcelProcess是我寫的一個操作excel的類。
轉載:http://emanlee.cnblogs.com/
總結
以上是生活随笔為你收集整理的windows窗体(winform)中嵌入显示Excel工作表。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java描述设计模式(22):策略模式
- 下一篇: JEECG(J2EE Code Gene